0
struct patient getPatient(FILE *fptr)
{
    char fileInput[200];
    int noOfEntries = 0, n;
    while (!feof(fptr))
    {
        fscanf(fptr, "%499[^\n]*s", fileInput);

        for (n = 0; n < FILENAME; n++ )
        {
        printf("%500c\n", fileInput);
        }
    }
}

This is where the file is being read, and I open it in another function.

FILE *openFile(void)
{
    FILE *fptr;
    char filename[FILENAME];
    printf("Enter filename: ");
    scanf("%s", filename);
    if (!(fptr = fopen(filename, "r")))
    {
        printf("Can't open file %s", filename);
        exit(1);
    }
    return fptr;
}

And they are both called here.

FILE *fptr = openFile();
getPatient(fptr);

It's either being opened incorrectly or not read correctly, I am reading it to a struct.

struct patient
{
    char entryLine[500];
};

EDIT: The new code, this block is more self-contained than last time I think.

void showPatientDetails(char fileName[70])
{
    char fileInput[500];
    int n;
    FILE *fptr;
    if(!(fptr = fopen(fileName, "r")))
    {
        printf("\n\nUnable to open files! \n\n");
        exit(1);
    }
    while(fgets(fileInput, sizeof fileInput, fptr))
    {
        fscanf(fptr, "%499[^\n]", fileInput);
        printf("%500c\n", fileInput);
//      for (n = 0; n < FILENAME; n++ )
//        {
//            printf("%500c\n", fileInput);
//        }
    }
}

I've tried two methods, one we did yesterday and another one that my teacher used for something else.

  • 1
    Who or what text suggested `while (!feof(fptr))`? – chux - Reinstate Monica Dec 17 '19 at 13:26
  • 5
    Please see [Why is `while ( !feof (file) )` always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong), that's not how `feof()` is supposed to be used. – unwind Dec 17 '19 at 13:27
  • 3
    `char fileInput[200];` is not long enough to read 499 characters. – Ian Abbott Dec 17 '19 at 13:32
  • Sorry, I was taught by my teacher to use feof(), I'll try and implement the proper method. – Nathaniel King Dec 17 '19 at 13:36
  • @NathanielKing You _can_ use it of course. You just need to do it correctly ;) Usually, it is unneeded, however, since the f*()-functions already signal EOF themselves. – Ctx Dec 17 '19 at 13:38
  • 1
    Remember that `*scanf` returns the number of input items read and assigned - you’ll want to check that to make sure the read succeeded, although you should use the `fgets` method user3121023 shows. – John Bode Dec 17 '19 at 13:40
  • 1
    @NathanielKing Did your teacher for real teach you to use `feof` as a condition in a `while` loop? – klutt Dec 17 '19 at 14:07
  • @klutt yeah, I haven't seen him use another method. We are using ANSI standard 99 and he is like 70 years old. – Nathaniel King Dec 17 '19 at 14:09
  • regarding: `fscanf(fptr, "%499[^\n]*s", fileInput);` The array `fileInput[]` is declared as: `char fileInput[200];` which is only 200 bytes long, so this can easily result in a buffer overflow and undefined behavior. Suggest: `fscanf(fptr, "%199[^\n]*s", fileInput);` Also, since `%[^\n], the next char to be read from `stdin` will be '\n', so the `*s` characters will never be matched. Suggest removing the `*s` – user3629249 Dec 18 '19 at 05:21
  • Re: the edit - you shouldn’t be using `fgets` *and* `fscanf` in the same loop on the same file, just use `fgets` - `while fgets( fileInput, sizeof fileInput, fptr )) { printf( “%s”, fileInput ); }`. – John Bode Dec 19 '19 at 13:10

1 Answers1

3

You seem to suffer from the magically common misconception that %[] somehow works as a modifier for a subsequent s. It really doesn't, it is its own conversion specifier.

So your conversion will fail unless the linefeed(s) are followed by an asterisk and an s.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • Please could you explain further on linefeed. I was taught `%[]` by my lecturer so thats the only way I know off. I am reading from the file per line. – Nathaniel King Dec 17 '19 at 13:39
  • 1
    @NathanielKing: Basically just remove the `*s` from the conversion specifier - `%[^\n]`. You will want to make sure the size of your array matches up with the number of characters you’re trying to read. Right now you’re trying to write 500 characters to a 200-element array - that won’t work. You really are better off using `fgets` for this kind of thing. – John Bode Dec 17 '19 at 13:44
  • @JohnBode Ok thanks, I changed what you recommended and tried to play around printing the file but it doesn't appear to be printing it at all anymore. Should I paste my whole code? – Nathaniel King Dec 17 '19 at 14:54
  • @JohnBode it does work thanks. I was opening a file that had no text..... – Nathaniel King Dec 17 '19 at 15:00
  • @NathanielKing: Heh. I've been programming professionally for 30 years, and that kind of mistake *still* bites me in the ass on occasion. I've just spent two days troubleshooting an issue that came down to a typo in a configuration file. It was especially fun because that typo was only present in our production environment, not our dev and testing environments, so the non-prod versions were working *just fine* while the production version face-planted. That was a fun couple of days. – John Bode Dec 17 '19 at 15:48
  • @JohnBode you reckon you will be able to help with a similar thing? I am opening another file trying to print it. I keep going back and forth with my working code and your advice but I still have the same problem as I originally had. – Nathaniel King Dec 18 '19 at 22:11