c - 如何从二进制文件读取到字符串/数组和 c 中的 int?

标签 c arrays string binaryfiles

在这个程序中,我使用结构数组写入了一个二进制文件,并且我试图以这种方式将它的数据放入另一个结构数组:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct rec{
    char cim[60];
    int x;
}rec;            //my structure



 int main(){
     int i;
     FILE *ptr_myfile;
     rec ujtomb[25];    //I want to have the elements here after reading the file
     rec tomb[25]={{"Spectre",3521},
         {"The Hunger Games Mockingjay Part 2",6123},
         {"Star Wars The Force awakens",9736},
         {"The Thirty Three",2342},
         {"The man From UNCLE",4312},
         {"Creed",4123},
         {"Inside out",6584},
         {"The martian",6674},
         {"Ant-man",7352},
         {"Secret in their eyes",2345},
         {"The night before",4758},
         {"Love",1586},
         {"Legend",8576},
         {"Trainwreck",4628},
         {"Love the Coopers",6372},
         {"Maze runner The scorch trials",8750},
         {"No escape",5793},
         {"Terminator genisys",8451},
         {"Krampus",452},
         {"Bridge of spies",9581},
         {"By the sea",7582},
         {"The peanuts movie",3214},
         {"Heist",1346},
         {"Spotlight",7450},
         {"Jurassic world",5438}};      //I listed the elements of the array
 ptr_myfile=fopen("nezettseg.bin","wb");
 if (!ptr_myfile)
 {
 printf("Unable to open file!");
 return 1;
 }
 for (i=0;i<25;i++){     //here i write into nezettseg.bin
        fwrite(&tomb[i].cim, sizeof(rec), 1, ptr_myfile);
        fwrite(&tomb[i].x, sizeof(rec), 1, ptr_myfile);
 }
 fclose(ptr_myfile);

     FILE* fin = fopen("nezettseg.bin", "rb");   //opening the binary file
     for(i=0;i<25;i++){    //now I'm trying to write those movies and numbers into ujtomb
         fread(&ujtomb, sizeof(rec), 1, ptr_myfile);   
         printf("%s %d\n", ujtomb->cim, ujtomb->x);
     }
     fclose(fin);
     return 0;
}

我的代码有效,但输出如下所示:

Spectre 3521
\301
 0
The Hunger Games Mockingjay Part 2 6123
\353 0
Star Wars The Force awakens 9736
& 0
The Thirty Three 2342
&    0
The man From UNCLE 4312
\330 0
Creed 4123
 0
Inside out 6584
\270 0
The martian 6674
 0
Ant-man 7352
\270 0
Secret in their eyes 2345
)    0
The night before 4758
\226 0
Love 1586
2 0
Legend 8576
Program ended with exit code: 0

sooo 基本上它写了 25 行,但是\number 带有变量 'cim',而 0-s 带有 x。 你能帮我如何将这些标题和数字正确地放入 ujtomb 中吗?

最佳答案

for (i=0;i<25;i++){
    fwrite(&tomb[i].cim, sizeof(rec), 1, ptr_myfile);
    fwrite(&tomb[i].x, sizeof(rec), 1, ptr_myfile);
}

是不对的。您需要使用:

for (i=0;i<25;i++){
    fwrite(&tomb[i], sizeof(rec), 1, ptr_myfile);
}

请记住 tomb[i] 是一个 rec,而不是 tomb[i].cimtomb[i]。 x.

此外,

 for(i=0;i<25;i++){
     fread(&ujtomb, sizeof(rec), 1, ptr_myfile);   
     printf("%s %d\n", ujtomb->cim, ujtomb->x);
 }

是不对的。您需要使用:

 for(i=0;i<25;i++){
     fread(&ujtomb[i], sizeof(rec), 1, fin);
                 ^^^^^ Missing index.  ^^^^ Use the right FILE*

     printf("%s %d\n", ujtomb[i].cim, ujtomb[i].x);
                             ^^^^           ^^^^ Use the i-th element
 }

关于c - 如何从二进制文件读取到字符串/数组和 c 中的 int?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34051106/

相关文章:

c - UDP 客户端没有从 C 中的服务器接收任何内容

c 程序在结构中使用时挂起

javascript - array.push() 重复相同的值

javascript - 根据另一个数组对数组进行排序

arrays - 无法设置数组内字典的值 [Swift]

python - 将单引号替换为双引号 python pandas 数据框

检查字符串是否包含换行符

c - 为什么将文本分配给指针会将内存位置存储到文本?

c - 在 C 中返回一堆结构体的更好方法是什么?

java - 将从字符串派生的唯一数字转换为良好的哈希码