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

如何在c中从二进制文件读取字符串/数组和int?,c,arrays,string,binaryfiles,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 *

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

#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;
}
所以它基本上写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);
}
 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
 }
(i=0;i)的

 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
 }