1
\$\begingroup\$

This program will read data in the form of:

Number Name

In each line of a text file, then it will write it to a binary file and print the resulting binary file.

I am getting a warning in line 73, but I don't think it's an issue as null is converted into 0, right?

while(((fread((char*)&student, 1, sizeof(student), binary_file)) != NULL))

I'm talking about this line above

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 50

void create_binary_file(const char *dest, const char *src);
void print_binary_file(const char *dest);

int main(void){
    const char ascii[MAX_SIZE] = "text.txt";
    char binary[MAX_SIZE];
    puts("Enter the name of the binary file");
    fgets(binary, sizeof(binary), stdin);
    binary[strlen(binary)-1] = '\0';

    create_binary_file(binary, ascii);
    print_binary_file(binary);

    return 0;
}

void create_binary_file(const char *dest, const char *src){
    struct student_tem{
        int ID;
        char name[MAX_SIZE];
    };

    struct student_tem student;

    FILE *text_file;
    FILE *binary_file;

    if((text_file = fopen(src, "r")) == NULL){
        perror(src);
        exit(EXIT_FAILURE);
    }

    if((binary_file = fopen(dest, "wb")) == NULL){
        perror(dest);
        exit(EXIT_FAILURE);
    }


    while(!feof(text_file)){
        if(1 != fscanf(text_file, "%i", &student.ID)){
            fprintf(stderr, "Error reading student number");
            exit(EXIT_FAILURE);
        }
        if(1 != fscanf(text_file, "%s", student.name)){
            fprintf(stderr, "Error reading student name");
            exit(EXIT_FAILURE);
        }
        fwrite(&student, 1, sizeof(student), binary_file);
    }

    fclose(text_file);
    fclose(binary_file);
}

void print_binary_file(const char *dest){
    struct student_tem{
        int ID;
        char name[MAX_SIZE];
    };

    struct student_tem student;
    FILE *binary_file;
    if((binary_file = fopen(dest, "rb")) == NULL){
        perror(dest);
        exit(EXIT_FAILURE);
    }

    while(((fread((char*)&student, 1, sizeof(student), binary_file)) != NULL)){
        printf("%i %s\n", student.ID, student.name);
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ Could you please at least leave us a comment at that line? It's a bit tedious to start counting or copy that stuff to an online IDE. Also improve your question title please. \$\endgroup\$ Apr 10, 2017 at 19:09
  • \$\begingroup\$ It's at the bottom in the last while loop. while(((fread((char*)&student, 1, sizeof(student), binary_file)) != NULL)) \$\endgroup\$
    – user127566
    Apr 10, 2017 at 19:39

1 Answer 1

3
\$\begingroup\$

I am getting a warning in line 73, but I don't think it's an issue as null is converted into 0, right?

 while(((fread((char*)&student, 1, sizeof(student), binary_file)) != NULL)){

Well, if you look at the fread() reference documentation, you see that the return value is a simple size_t, not a pointer. NULL is meant to be a void* pointer and may be probably implemented like #define NULL ((void*)0) according to this.

C is still a strongly statically typed language, a ((void*)0) isn't the same as a ((size_t)0) hence the warning. And you should fix that, it actually is an issue.


Another thing you should look into is

 while(!feof(text_file)){

Why is iostream::eof inside a loop condition considered wrong?

It's merely the same problem in plain C code. You should rather check the streams state from the results of the fscanf() operations.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.