用fputc在c的整数数组中写入文件 [英] Write a file in c of an array of integers with fputc

查看:187
本文介绍了用fputc在c的整数数组中写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,该程序读取文件并生成每个字节的整数数组,首先我用.txt文件证明,然后为每个字母及其对应的字节(例如ASCII)生成ASCII因为B是66,而二进制66是01000010,我的程序在.exe窗口中打印出这两个),但是我需要使用array(rest [x])的整数制作相同的文件,我可以制作一个新文件.txt,但是只有垃圾桶,或者我认为是,file.txt是新文件.txt的名称,并且只有垃圾桶,我想恢复UAM.txt中的文本,并写在数组rest [x]中的file.txt;

I'm writing a program that reads a file and generates an array of integers of each byte, first I prove with a .txt file and I generates the ASCII for each of the letters and its corresping byte(for example the ASCII for B is 66, and the binary of 66 is 01000010, my program print this two in the .exe window), but I need to make the same file with the integers of the array(rest[x]), I could make a new file .txt but it has only trash, or I think is it, file.txt is the name of the new file .txt and it only has trash, I want to recover the text that is in UAM.txt, and writte in file.txt from the array rest[x];

#include<stdio.h>

int main()
{

    int b1, b2, b3, b4, b5, b6, b7, b8;
    int x, i;
    char a, b;

    FILE *f1, *bin, *fp;

    b1 = 0x01; // = 0000 0001
    b2 = 0x02; // = 0000 0010
    b3 = 0x04; // = 0000 0100
    b4 = 0x08; // = 0000 1000
    b5 = 0x10; // = 0001 0000
    b6 = 0x20; // = 0010 0000
    b7 = 0x40; // = 0100 0000
    b8 = 0x80; // = 1000 0000

    int mask[8] = { b8, b7, b6, b5, b4, b3, b2, b1 };
    int rest[8];

    f1 = fopen("UAM.txt", "rb");
    fp = fopen("file.txt", "w+");

    while (!feof(f1))
    {
        a = getc(f1);
        printf("%d\n", a);

        for (i = 0; i <= 7; i++)
        {
            rest[i] = a & mask[i];
        }

        for (i = 0; i <= 7; i++)
        {
            rest[i] = rest[i] / mask[i];
        }

        for (x = 0; x <= 7; x++)
        {
            printf("%i", rest[x]);
            fputc(rest[x], fp);
        }

        fclose(fp);

        printf("\n");
    }

    return 0;
}

输入文件可以是任何文本,举个简单的例子,我将B字保存在UAM.txt中,在.exe窗口中,我获得了66和01000010,这是B的ASCII码(十进制和二进制),接下来是二进制数(即单词的字节)位于整数数组(rest [x])中.我需要再次将该二进制文件转换为字母B,并将该字母或任何文本保存在我名为file.txt的新文件中,此文件中必须再次是字母B或UAM中的任何文本.txt对不起我的英语不好!

the input file can be any text, for a easy example I save the word B in UAM.txt, and in the .exe window I obtained 66 and 01000010 which is the ASCII code for B in decimal and binary, next the binary number which is the byte of the word is located In an integer array(rest[x]). I need to convert this binary again to the letter B, and save this letter or any text in a new file that I named file.txt, in this file has to be again the letter B, or any text that it´s in UAM.txt sorry for my poor english!

任何帮助将不胜感激!

推荐答案

您应该使用mask数组从数组开始重建整数.

You should reconstruct the integer starting from your array using your mask array.

#include<stdio.h>

int main(void)
{

    int b1, b2, b3, b4, b5, b6, b7, b8;
    int x, i;
    char a, b;

    FILE *f1, *bin, *fp;

    b1 = 0x01; // = 0000 0001
    b2 = 0x02; // = 0000 0010
    b3 = 0x04; // = 0000 0100
    b4 = 0x08; // = 0000 1000
    b5 = 0x10; // = 0001 0000
    b6 = 0x20; // = 0010 0000
    b7 = 0x40; // = 0100 0000
    b8 = 0x80; // = 1000 0000

    int mask[8] = { b8, b7, b6, b5, b4, b3, b2, b1 };
    int rest[8];

    f1 = fopen("UAM.txt", "rb");
    fp = fopen("file.txt", "w+");

    while ((a = fgetc(f1)) != EOF)
    {
        printf("%d\n", a);

        for (i = 0; i <= 7; i++)
        {
            rest[i] = a & mask[i];
        }

        for (i = 0; i <= 7; i++)
        {
            rest[i] = rest[i] / mask[i];
        }

        a=0;
        for (x = 0; x <= 7; x++)
        {
            printf("%i", rest[x]);
            a += rest[x] * mask[x];
        }

        printf("\n%d\n", a);

        fputc(rest[x], fp);

        printf("\n");
    }

    fclose(f1);
    fclose(fp);

    return 0;
}

更好的方法可能是

#include<stdio.h>

int main(void)
{

    int x, i;
    char a;

    FILE *f1, *fp;

    int rest[8];

    f1 = fopen("UAM.txt", "rb");
    if ( f1 != NULL)
    {
        fp = fopen("file.txt", "w+");

        if ( fp != NULL)
        {
            while ((a = fgetc(f1)) != EOF)
            {
                printf("%d\n", a);

                for (i = 0; i < 8; i++)
                {
                    rest[i] = a & 0x01;

                    a>>=1;
                }

                a=0;
                for (x = 7; x >= 0; x--)
                {
                    printf("%i", rest[x]);

                    a += ((0x01u << x) * rest[x]);
                }

                fputc(rest[x], fp);

                printf("\n");
            }

            fclose(fp);
        }

        fclose(f1);
    }

    return 0;
}

这篇关于用fputc在c的整数数组中写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆