0
#include <stdio.h>
int main()
{
    int i;
    char surname[30];
    char name[30], matricno[10];
    int A, B, C;
    FILE *file, *file1;
    /* Open an existing file */
    file = fopen("Broadsheett.txt","r");
    if(file == NULL){
        printf("Error: Unable to open a file");
    }
    file1 = fopen("newbroadsheet.txt", "w");
    fprintf(file1, "University of Nairaland\n");
    fprintf(file1, "Department of Programming\n");
    fprintf(file1, "================================\n");
    while (!feof(file)) {
        fscanf(file, "%s\t%s\t%d\t%d\n", &name, &matricno, &A, &B);
        fprintf(file1, "%s\t%s\t%d\t%d\n", name, matricno, A, B);
    }
}

The output file is not well formatted. I don't know where the enclosure came from.

Output file

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    Please copy paste the output instead of using an image. Also, correct your code. You have a couple of `wink`s in there. – Spikatrix Mar 13 '17 at 04:38
  • 3
    Note that [`while (!feof(file))` is always wrong](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) and your code is no exception. You should check the return value from `fscanf()`; things can go haywire if you don't. Don't use `&array` as an argument to `fscanf()` — use just `array` (or sometimes `&array[index]`). – Jonathan Leffler Mar 13 '17 at 05:09
  • 2
    You should show us a few lines of the input (5 would be ample) and the corresponding 'badly formatted output', explaining what you think should be different — so also describing the desired output. This would help you creating an MCVE ([MCVE]). We can help if you create an MCVE; we can't if you don't. And don't post pictures of text (text inputs or text outputs); embed the text directly in the question. Copy it in (making sure there are no tabs in it), then select it and use the **`{}`** button above the edit box to indent it as text. – Jonathan Leffler Mar 13 '17 at 05:14
  • 1
    Oh, and report errors on standard error (`stderr`), and make sure that you don't use the file stream after reporting that you failed to open it, and do make sure that you close the file. – Jonathan Leffler Mar 13 '17 at 05:16

1 Answers1

1

Your input file apparently has a header:

Name MatricNo A B

On the first iteration, fscanf successfully reads string variables name and matricno; however, reading the numbers fails as it sees A. So it prints name and matricno, being values Name and MaricNo and prints the value of integes A and B, which were not initialized and so are garbage.

On the second iteration, fscanf continues where it left off and reads strings A and B (last part of the header) but again reading the integers fails. So it now prints A and B and again the unitialized values of variables A and B.

Now everything is synchronized again and it prints the rest out fine.

So you should have checked the return value of fscanf. Also, while (feof)) is wrong as it tells you that a read operation encountered EOF. So it does not tell you whether there is something left in the file, which you assume feof does. Finally, passing an array to a function results in the compiler giving the address of the array to the function, so don't put & before name and matricno in the call to fscanf (the compiler apparently ignores it but may give a warning).

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
  • What do you suggest if while (feof)) has limitations? I'm new to C – biodun Adeniji Mar 13 '17 at 10:27
  • You can use `while(1) { if (fscanf(...)!=4 && feof(file)) break;` So if `fscanf` is unable to read _and now_ `feof` is true, exit the loop. Note that if `feof` is false, there was another error. But this example shows that `feof` rather tests a type of read error, namely "no more data in file", than that it tells you whether there is something left in the file (which it doesn't tell you). – Paul Ogilvie Mar 13 '17 at 11:48