【问题标题】:Write a file in c of an array of integers with fputc用 fputc 在 c 中写入一个整数数组的文件
【发布时间】:2016-12-12 06:56:49
【问题描述】:

我正在编写一个程序,它读取文件并生成每个字节的整数数组,首先我用 .txt 文件证明,然后为每个字母及其对应字节生成 ASCII(例如 ASCII对于 B 是 66,而 66 的二进制是 01000010,我的程序在 .exe 窗口中打印这两个),但我需要用数组的整数(rest [x])制作同一个文件,我可以制作一个新文件 .txt 但它只有垃圾,或者我认为是,file.txt 是新文件 .txt 的名称,它只有垃圾,我想恢复 UAM.txt 中的文本,并写入file.txt 来自数组 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 对不起我糟糕的英语!

任何帮助将不胜感激!

【问题讨论】:

  • fclose(fp); 在循环内部,这肯定是不好的。顺便说一句 - 发布输入文件和预期输出的简短示例。我的猜测是 fputc(rest[x], fp); 应该是 fputc(rest[x] + '0', fp); 但帽子只是一个猜测,因为我不清楚你真正想要什么。
  • int main() --> int main(void)
  • 输入文件可以是任何文本,举个简单的例子,我将单词 B 保存在 UAM.txt 中,在 .exe 窗口中我获得了 66 和 01000010,这是十进制 B 的 ASCII 码和二进制,接下来作为单词字节的二进制数位于整数数组(rest[x])中。我需要再次将此二进制文件转换为字母 B,并将此字母或任何文本保存在我命名为 file.txt 的新文件中,对不起我的英语不好!
  • 好吧,但是您想如何将数字存储到输出文件中呢?作为01000010 字符串?

标签: c arrays string


【解决方案1】:

你应该使用你的掩码数组从你的数组开始重建整数。

#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(rest[x], fp);对于 fputc(a, fp);这对我有用!非常感谢
【解决方案2】:

问题是因为使用了 fputc。它总是打印一个字符,因此 rest[x] 的值被转换为一个字符,然后写入文件。这就是为什么您会在文件中看到所有垃圾。

以下替换:

fprintf(fp,"%i", rest[x]); // %i here would print the expected value
//fputc(rest[x], fp);

另外,close 应该是在循环之外的。

需要注意的另一件事是,当您逐个字符读取文件末尾时,它也会转换结尾的“\n”和“\r”字符。

下面是工作程序:

#include <stdio.h>


int main() {

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

FILE *f1, *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]);
        fprintf(fp,"%i", rest[x]);
        //fputc(rest[x], fp);
    }
    fprintf(fp,"\n");
    printf("\n");
}
fclose(f1);
fclose(fp);

return 0;
}

【讨论】:

    【解决方案3】:

    问题 1:

    您在循环内调用fclose,这可能会导致您仍在尝试写入文件。将fclose 移到while 循环之外。

    问题 #2:

    while (!feof(f1))
    

    不是检查是否已到达文件结尾的正确方法。相反,您应该检查来自getc 的返回值,例如:

    while ((a = getc(f1)) != EOF)
    

    问题 #3:

    您无法将二进制值一一写入输出文件以重现原始字符。 fputc 写入一个完整的字符 - 一点也不。因此,您需要从二进制值重建原始字符,并且只调用fputconce。

    替代二进制计算

    您的代码正确计算了二进制表示(位值)。但是,您似乎使用了过于复杂的方法。考虑改用&gt;&gt;(又名右移)。喜欢:

        // Create the binary values
        for (i = 7; i >= 0; i--)
        {
          rest[i] = a & 1;
          a = a >> 1;
        }
    

    所以把它们放在一起,代码可能是:

    int main(void)
    {
    
        int i;
        char a;
        int t;
        FILE *f1, *fp;
        int rest[8];
    
        f1 = fopen("UAM.txt", "rb");   // TODO: check that fopen went well
        fp = fopen("file.txt", "w+");  // TODO: check that fopen went well
    
        while ((a = getc(f1)) != EOF)
        {
            printf("Char representation %c\n", a);
            printf("Decimal representation %d\n", a);
    
            // Create the binary values
            for (i = 7; i >= 0; i--)
            {
              rest[i] = a & 1;
              a = a >> 1;
            }
    
            printf("Binary representation: ");
            for (i = 0; i <= 7; i++)
            {
              printf("%d", rest[i]);
            }
            printf("\n");
    
            // Reconstruct original value
            t = 0;
            for (i = 0; i <= 7; i++)
            {
              t = t << 1;
              t = t | rest[i];
            }
    
            // Write reconstructed value to file
            fputc(t, fp);
        }
    
        fclose(fp);
        fclose(f1);
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2011-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-06
      相关资源
      最近更新 更多