-1

Everything seems okay and program is running but my report.txt file is empty. There is one warning - "passing argument 2 of 'fwrite' makes integer from pointer without a cast". What might be a problem? (Here is a bit of the code)

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;
}
TheArchitect
  • 1,160
  • 4
  • 15
  • 26
kukimukiku
  • 85
  • 8
  • 1
    Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine at which point your program stops behaving as intended? In particular, did you verify that your program reaches the line where you write to `fr` using `fwrite`? If you did not try this, then you probably want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Andreas Wenzel Oct 05 '20 at 20:28
  • 1
    The line `fwrite("%s %s", &s.name, &s.surname, fr);` looks wrong. Did you mean `fprintf( fr, "%s %s", s.name, s.surname );`? – Andreas Wenzel Oct 05 '20 at 20:37
  • You have `fwrite("%s %s", &s.name, &s.surname, fr);` but `fwrite()` takes `size_t` types for arguments 2 and 3, not pointers, so you should review how to use the function. – Weather Vane Oct 05 '20 at 20:38

2 Answers2

2

For your warning look here, fwrite isn't like printf you need to write it as:

fwrite(s.name, strlen(s.name), 1, db);
fwrite(s.surname, strlen(s.surname), 1, db);
DipStax
  • 343
  • 2
  • 12
1

your code:

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

my fix :

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

you used fwrite like fprintf

all write function dont get parameters like %s %i %d ..etc http://manpagesfr.free.fr/man/man3/fread.3.html (man are life ♥o♥)

also write didn't "write" the \0 if you didn't give it a string with a \0.

Halfa
  • 92
  • 10