C 读取二进制文件并显示条目

C 读取二进制文件并显示条目,c,struct,fwrite,fread,C,Struct,Fwrite,Fread,我可能需要你的帮助来解决这个问题。。。 我正在学习C,并发现了一个问题,如何正确读取二进制文件 我填充了一个结构数组,然后写在二进制文件上并尝试读取它,但这并不是什么都不显示 代码在pastebin中 fread中的第一个参数应该是一个指针fread(void*,…),并且candidate已经声明为指针,它不应该有引用运算符。正确的用法是: while(fread(candidate,sizeof(Candidate),1,fp) == 1) {...} 请注意,候选者 有时您会看到一个

我可能需要你的帮助来解决这个问题。。。 我正在学习C,并发现了一个问题,如何正确读取二进制文件

我填充了一个结构数组,然后写在二进制文件上并尝试读取它,但这并不是什么都不显示

代码在pastebin中

fread
中的第一个参数应该是一个指针
fread(void*,…)
,并且
candidate
已经声明为指针,它不应该有引用运算符。正确的用法是:

while(fread(candidate,sizeof(Candidate),1,fp) == 1) {...}
请注意,
候选者


有时您会看到一个引用运算符,但情况不同,如下所示:

Candidate cand;
while(fread(&cand,sizeof(cand),1,fp) == 1) {...}
这是一种更简单的方法,因为
cand
不需要分配
malloc
,也不需要
free

不必要的函数会导致更多错误:

FILE * openFile(char filename[100], char filemode[3]) {
    FILE *p = fopen(filename, filemode);
    if (!p) {
        printf("Error to open %s file. \nThe program will be closed.",filename);
        exit(1);
    }
}
这个函数应该返回一个文件指针。函数参数也可以简单地写为
const char*filename
const char*filemode
。例如:

FILE * openFile(const char* filename, const char* filemode) {
    FILE *p = fopen(filename, filemode);
    if (!p) printf("Error to open %s file. \nThe program will be closed.",filename);
    return p;
}
我会完全去掉这个函数,因为它基本上是无用的。只需使用
fopen
。完成后,确保用
fclose
关闭手柄


这是一个输入错误,应该是
grade[counter].testGrade
。建议编译程序时将警告级别设置为最大,或至少设置为4级。编译器将告诉您有关输入错误、错误和警告的信息。您必须能够编译零错误和零警告的程序。以下是一个简单的版本:

void listAllCandidates(char* candidateFilename)
{
    FILE *fin = fopen(candidateFilename, "rb");
    Candidate cand;
    while(fread(&cand, sizeof(cand), 1, fin))
        printf("%s, %s, %d, %f\n", cand.name, cand.document, cand.id, cand.testGrade);
    fclose(fin);
}

int main() 
{
    char candidateFilename[] = "file.bin";

    FILE *fout = fopen(candidateFilename, "ab");
    Candidate cand;

    strcpy(cand.name, "name1");
    strcpy(cand.document, "document1");
    cand.id = 1;
    cand.testGrade = 1.1f;
    fwrite(&cand, sizeof(cand), 1, fout);

    strcpy(cand.name, "name2");
    strcpy(cand.document, "document2");
    cand.id = 2;
    cand.testGrade = 2.2f;
    fwrite(&cand, sizeof(cand), 1, fout);

    fclose(fout);

    listAllCandidates(candidateFilename);

    printf("\n");
    return 0;
}

更多细节。更多细节。它会显示菜单吗?调试器…………显示菜单,我可以添加一些候选项,但不能列出它们。谢谢,它工作起来很有魅力。。。
FILE * openFile(char filename[100], char filemode[3]) {
    FILE *p = fopen(filename, filemode);
    if (!p) {
        printf("Error to open %s file. \nThe program will be closed.",filename);
        exit(1);
    }
}
FILE * openFile(const char* filename, const char* filemode) {
    FILE *p = fopen(filename, filemode);
    if (!p) printf("Error to open %s file. \nThe program will be closed.",filename);
    return p;
}
grade[counter].testGrate = testGrade;
                      ^
void listAllCandidates(char* candidateFilename)
{
    FILE *fin = fopen(candidateFilename, "rb");
    Candidate cand;
    while(fread(&cand, sizeof(cand), 1, fin))
        printf("%s, %s, %d, %f\n", cand.name, cand.document, cand.id, cand.testGrade);
    fclose(fin);
}

int main() 
{
    char candidateFilename[] = "file.bin";

    FILE *fout = fopen(candidateFilename, "ab");
    Candidate cand;

    strcpy(cand.name, "name1");
    strcpy(cand.document, "document1");
    cand.id = 1;
    cand.testGrade = 1.1f;
    fwrite(&cand, sizeof(cand), 1, fout);

    strcpy(cand.name, "name2");
    strcpy(cand.document, "document2");
    cand.id = 2;
    cand.testGrade = 2.2f;
    fwrite(&cand, sizeof(cand), 1, fout);

    fclose(fout);

    listAllCandidates(candidateFilename);

    printf("\n");
    return 0;
}