【问题标题】:Why my code doesn't write anything in file (txt)为什么我的代码没有在文件 (txt) 中写入任何内容
【发布时间】:2020-10-05 20:22:33
【问题描述】:

一切正常,程序正在运行,但我的 report.txt 文件为空。有一个警告 - “传递 'fwrite' 的参数 2 从指针中生成整数而不进行强制转换”。可能是什么问题? (这里是一些代码)

int main(int argc, char *argv[]) {
    FILE *fr = fopen("report.txt", "wb");
    FILE *db = NULL;
    
    if (argc > 1)  // open database file for reading, provide a parameter or use default "db.bin"
        db = fopen(argv[1], "rb");
    else
        db = fopen("db.bin", "rb");
        
    if (db) {                            
        Student students[1000];           // all the data goes here
        int size = 20;                    // how many students in database
        
        fread(&size, sizeof(int), 1, db); // reading data from file
        
        for (int i = 0; i < size; i++)        
            fread(&students[i], sizeof(Student), 1, db);            

        printf("%d records loaded succesfully\n", size);
        
        
        // MODIFY CODE BELOW
        
        int counterDemo = 0;             // for counting students
        for (int i = 0; i < size; ++i) { // process all the student records in database
            Student s = students[i];     // store data for each student in s
            
            if(s.load == 0) {            // *** first filter, conditions on the student
               printf("Name - %s, surname -  %s, course -  %d, average grade -  %f, number of courses %d\n ", 
                       s.name, s.surname, s.course, s.average, s.load);
                int anotherDemo = 0;               // for counting courses/grades
                for (int i = 0; i < s.load; ++i) { // process each course taken by the student
                     if(1) {                       // *** second filter, conditions on the course/grade
                        ++anotherDemo;             // counting courses
                        printf("Course name - %s, course grades -  %d\n ", 
                               s.courses[i], s.grades[i]);
                    }
                }
                printf("Languages - %s\n", s.languages);
                printf("\n");
                        
                if (anotherDemo == s.load)        // *** third filter, various other conditions            
                    ++counterDemo;                // counting studfents
                fwrite("%s %s", 
                       &s.name, &s.surname, fr);
            }
        }
        printf("Filter applied, %d students found\n", 
               counterDemo);                     // how many passed the filters
        fclose(db); 
    } else 
        printf("File db.bin not found, check current folder\n");
    
    return 0;
}

【问题讨论】:

  • 您是否尝试过在调试器中逐行运行代码,同时监控所有变量的值,以确定您的程序在哪个点停止按预期运行?特别是,您是否确认您的程序到达了您使用fwrite 写入fr 的行?如果您没有尝试过,那么您可能想阅读以下内容:What is a debugger and how can it help me diagnose problems? 您可能还想阅读以下内容:How to debug small programs?
  • fwrite("%s %s", &amp;s.name, &amp;s.surname, fr); 行看起来不对。你的意思是fprintf( fr, "%s %s", s.name, s.surname );
  • 您有 fwrite("%s %s", &amp;s.name, &amp;s.surname, fr);fwrite() 采用 size_t 类型作为参数 2 和 3,而不是指针,因此您应该查看如何使用该函数。

标签: c fopen fwrite


【解决方案1】:

对于你的警告看起来herefwrite 不像printf 你需要写成:

fwrite(s.name, strlen(s.name), 1, db);
fwrite(s.surname, strlen(s.surname), 1, db);

【讨论】:

  • 另一种方法是使用fprintf 而不是fwrite。这就是 OP 可能正在尝试做的事情。
【解决方案2】:

你的代码:

fwrite("%s %s", &s.name, &s.surname, fr);

我的修复:

fprintf(fr, "%s %s", s.name, s.surname);

你像 fprintf 一样使用 fwrite

所有写函数都没有得到像 %s %i %d ..etc 这样的参数 http://manpagesfr.free.fr/man/man3/fread.3.html(人是生命♥o♥)

如果你没有给它一个带 \0 的字符串,也不会“写入”\0。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-07
    • 1970-01-01
    • 1970-01-01
    • 2019-11-09
    • 2017-04-27
    相关资源
    最近更新 更多