c - 使用 getc() 顺序读取文本文件并使用 fwrite() 写入 bin 文件无法按预期工作?

标签 c

我正在尝试用 C 语言制作一个简单的(不是真正的)程序,它利用文件生成随机的成分列表,这些成分将来将成为食谱。 我在所有相关函数的帮助下使用文本文件和二进制文件:getc()、fwrite()、fread()...

我所尝试的都是在两个函数的代码中显示的:

  • int Convert_txt_to_bin()
  • int print_ingredient_file(路径文件路径)

我保证,它们并没有那么复杂。 也很抱歉因休息而关闭:)

成分

typedef struct {
    char name[32];  // The name of the ingredient: "Mela", "Fusilli", "Passata di pomodoro";
    int type;
} ingredient;
int convert_txt_to_bin()
{
    FILE *txt, *bin;
    ingredient tmp;
    int i = 0, records_written = 0;
    char c;
    tmp.type = 0;

    txt = fopen(file_list_fruits_txt, "r");
    bin = fopen(file_list_fruits_bin, "wb");

    if (txt && bin)
    {
        while (1)
        {
            tmp.name[i] = getc(txt);
            c = tmp.name[i];
            if (tmp.name[i] == '\n' || tmp.name[i] == EOF)
            {
                tmp.name[i] = '\0';
                fwrite(&tmp, sizeof(ingredient), 1, bin);
                while (i >= 0) tmp.name[i--] = '#';
                records_written++;
                i++;
                printf("Record written successfully\n");

                if (c == EOF) break;
            }
            else i++;
        }
        fclose(txt);
        fclose(bin);
        printf(".bin file written correctly\n%d records saved\n", records_written);
        return records_written;
    }
    else
    {
        printf("ERROR: can't open file. Quitting...");
        system("PAUSE");
        fclose(txt);
        fclose(bin);
        exit(1);
    }
}
int print_ingredient_file(path file_path)
{
    FILE* bin;
    ingredient tmp;
    int i = 0;

    bin = fopen(file_path, "rb");

    while (fread(&tmp, sizeof(ingredient), 1, bin))
        printf("Ingredient number #%d\n\tName = %s\n\tType = %d\n", i++, tmp.name, tmp.type);

    fclose(bin);
}
fruits.txt (it's in italian)
Mela
Banana
Pera
Kiwi
Pesca
Albicocca
Mandarino
Uva
Ciliegia
Lampone
Fragola
Mora
Mirtillo
Fico
Nespola
Caco
Anguria
Melone
Melograno
Arancia
Mango
fruits.bin

21个水果中有19个已成功记录。 我不明白为什么 Pera(梨)和 Banana(猜猜看)没有正确保存并给出输出问题。

输出到标准输出:

Record written successfully
Record written successfully
Record written successfully
Record written successfully
Record written successfully
Record written successfully
Record written successfully
Record written successfully
Record written successfully
Record written successfully
Record written successfully
Record written successfully
Record written successfully
Record written successfully
Record written successfully
Record written successfully
Record written successfully
Record written successfully
Record written successfully
.bin file written correctly
19 records saved
Ingredient number #0    <------------------------------ Perana ???
Perana  Name = Mela
        Type = 0
Ingredient number #1
        Name = Kiwi
        Type = 0
Ingredient number #2
        Name = Pesca
        Type = 0
Ingredient number #3
        Name = Albicocca
        Type = 0
Ingredient number #4
        Name = Mandarino
        Type = 0
Ingredient number #5
        Name = Uva
        Type = 0
Ingredient number #6
        Name = Ciliegia
        Type = 0
Ingredient number #7
        Name = Lampone
        Type = 0
Ingredient number #8
        Name = Fragola
        Type = 0
Ingredient number #9
        Name = Mora
        Type = 0
Ingredient number #10
        Name = Mirtillo
        Type = 0
Ingredient number #11
        Name = Fico
        Type = 0
Ingredient number #12
        Name = Nespola
        Type = 0
Ingredient number #13
        Name = Caco
        Type = 0
Ingredient number #14
        Name = Anguria
        Type = 0
Ingredient number #15
        Name = Melone
        Type = 0
Ingredient number #16
        Name = Melograno
        Type = 0
Ingredient number #17
        Name = Arancia
        Type = 0
Ingredient number #18
        Name = Mango
        Type = 0
Premere un tasto per continuare . . .

最佳答案

评论中有一些很好的建议,但您报告的问题很可能不是编程问题,而是数据文件的问题。 BananaPera 叠加在打印 Mera 的行上强烈表明文件中的前两行没有正确地以换行符终止字符,而是带有回车符。

老实说,我现在不知道怎么会发生这种情况。二十年前,Apple 的操作系统使用 CR 行结尾,但当 Apple 在 FreeBSD 上重新构建其操作系统时,情况发生了变化。 Windows 仍然使用两个字符的 CR-LF 序列来指示换行符,但由于其中包含 \n,因此不应导致此特定问题。

所以我能建议的是你用十六进制编辑器检查你的输入文件(或者只是用 hd 查看它)并查看行末尾的字符代码是什么是。

关于c - 使用 getc() 顺序读取文本文件并使用 fwrite() 写入 bin 文件无法按预期工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56715099/

相关文章:

将 uint8 数组转换为结构数组而不是 memcpy

"_inout"参数可以是 "const"“吗?

c - 如何在预处理器时间内计算数组大小?

命令行参数 C

c - 使用 strtok 转储的段错误核心

c - SOCKS5 错误 400 Tor

c - 嵌套 do while 循环

c - pthread_create写回的时机

c - 这是做什么的?

c++ - 从哪里获得静态构建形式的 OpenAL?