0

I have to make a program which will print the name and the score of a student found on a text file. The text file itself will follow this format: number of students (int) score name (of student number one) score name (of student number two)

So for example, the students.txt file you will see I am using for this program should be something like this: 2 45 George 23 John

The reading part of the program (for the structures) should be done using fread. I have tried what you will see below and the problem is that when I run the program the console just remains blank. Note that the program is not complete yet, since I am trying to learn how a few things work first (for example I am only trying to print the scores of the student just so I can see whether I know what I am doing) before doing it properly and I know some parts like checking for NULL return are missing. I am also wondering whether since I am using fread the txt file should be considered binary or not.

Here is what I have tried:

#include <stdio.h>
#include <stdlib.h>

#define FILENAME students.txt
#define MAX 50
typedef struct st {
    float score;
    char name[MAX];
} student;

void read(FILE *fp, student *p, int size);



int main()
{
    int number;
    student *ptr;
    FILE *ifp;

    ifp = fopen("FILENAME", "rb");

    fscanf(ifp, "%d", &number);

    ptr = (student *)malloc(number * sizeof(student));

    read(ifp, ptr, number);
}

void read(FILE *fp, student *p, int size)
{
    int num;

    fscanf(fp,"%d", &num);

    fread(p, sizeof(student), size, fp);

    for(int i=0; i<size; i++)
        printf("%f", p[i].score);
}
Johnny
  • 25
  • 5
  • `#define FILENAME students.txt` --> `#define FILENAME "students.txt"` , `ifp = fopen("FILENAME", "rb");` --> `ifp = fopen(FILENAME, "rb");` – David Ranieri Jan 23 '20 at 14:07
  • The file format described is not suitable for being read with `fread`. `fread` requires that you know the size of the elements but... you don't – Support Ukraine Jan 23 '20 at 14:10
  • As for the FILENAME, it is between " " in my book although I have seen it in the way you mentioned too. I will try these and hope it works thanks – Johnny Jan 23 '20 at 14:10
  • @4386427 you mean the elements of the structure? – Johnny Jan 23 '20 at 14:10
  • Yes, the file format you describe indicate a text format with variable length of names. You can't read that using `fread`. `fread` expects a number of items of fixed length. In other words - if the file is as you describe, `fread` is simply the wrong function to use... – Support Ukraine Jan 23 '20 at 14:12
  • @4386427 I came across a similar question here (which did not help eventually), asking about fread and structures and they were using a .txt file too so I thought it would be correct. – Johnny Jan 23 '20 at 14:15
  • 1
    Well, it's not important that the file has the extention`.txt` It's important what is inside the file... – Support Ukraine Jan 23 '20 at 14:16
  • Tip: Instead of reading a file, you should start with a program that **writes** a file using `fwrite`. Then try to open that file in a text editor and you'll see... – Support Ukraine Jan 23 '20 at 14:18
  • @Giannis Mixing `fscanf()` and `fread()` **can** be done, but you really have to know what you're doing as the `*scanf()` functions are **extremely** hard to use predictably and nowhere near robust enough for this kind of use in anything needing any reliability at all. I'd say this is one of those things that if you can't actually answer the question, you shouldn't try it. And if you can answer the question, you do it just to prove you can and then throw it away and never actually use it for anything important. – Andrew Henle Jan 23 '20 at 14:19
  • Using fread means you are reading 56 bytes every read, it doesn't care about the names or numbers, or any other content in your file. – Arrrow Jan 23 '20 at 14:20

1 Answers1

0

You are opening a file named "FILENAME", not "students.txt", which is why your command line is remaining blank. Change

#define FILENAME students.txt

into

#define FILENAME "students.txt"

and

fopen("FILENAME", "rb");

into

fopen(FILENAME, "r");

Furthermore, fread requires that the caller knows the exact size of the elements they are reading, but since the student names have a variable length, this is not the case. You should use fgets instead. You can read more about fgets here.

fread should only be used in the case of binary input/output, usually memory blocks (arrays or structs) that have a size known to the caller.

Arrrow
  • 542
  • 5
  • 21