Question

Right so I have created a program that will get content from a line in some different files and sum them, and print the sum onto the screen, and then go ahead and get the next line, as shown below.

#include<stdio.h>
#include<conio.h>
void addscores(void);

FILE *faa;
FILE *fab;
FILE *fac;

int DataCount=0;
int total=0;

struct store3
{
    int def3;
}stock3[128];

int main()
{  
    addscores();      
}
void addscores(void)
{
    do
    {
    total=0;
    faa=fopen("Test 01 Scores.txt","r");
    fscanf(faa,"%d", &stock3[DataCount].def3);
    fclose(faa);

    total=total+stock3[DataCount].def3;

    fab=fopen("Test 02 Scores.txt","r");
    fscanf(fab,"%d", &stock3[DataCount].def3);
    fclose(fab);

    total=total+stock3[DataCount].def3;

    fac=fopen("Test 03 Scores.txt","r");
    fscanf(fac,"%d", &stock3[DataCount].def3);
    fclose(fac);
    total=total+stock3[DataCount].def3;
    printf("Total = %d\n",total);
    DataCount=DataCount+1;
    getch();
    }
    while(!feof(faa));

getch();
}

However, a problem has arisen, the program will not read the 2nd, 3rd....nth line of the file after completing the first loop, so the program will just print the same sum of the first line.

Ignoring the infinite loop (It can be fixed later) how can I go about fixing this?

Side note: Please excuse my infamous use of while(!EOF)

Contents in files:

1
2
3
4
5

Expected output

3
6
9
12
15

real output

3
3
3
3
3
3
3
3
3
3
3
3
...
...

Thanks, it's fixed :D

Was it helpful?

Solution

Since you are opening and closing the files in each iteration, you always read the first line of each file. This is the reason for both problems: You get the infinite loop because you never reach EOF and you always sum up the entries in the first lines. You should open all files before loop entry and close them afterwards. You should also check if at least one file has reached EOF (not only the first one) to catch the case when they have different legths and move the check to the beginning of the loop (if the files are empty).

OTHER TIPS

The problem is that you close the file and reopen it at each loop.

Look at "filedescriptors" and fseek function. Try to understand it.

If you have some question you are welcome! :)

As the other have said, simply open/close once, outside the loop.

faa=fopen("Test 01 Scores.txt","r");
fab=fopen("Test 02 Scores.txt","r");
fac=fopen("Test 03 Scores.txt","r");
do {
  total=0;

  fscanf(faa,"%d", &stock3[DataCount].def3);
  total=total+stock3[DataCount].def3;

  fscanf(fab,"%d", &stock3[DataCount].def3);
  total=total+stock3[DataCount].def3;

  fscanf(fac,"%d", &stock3[DataCount].def3);
  total=total+stock3[DataCount].def3;

  printf("Total = %d\n",total);
  DataCount=DataCount+1;
  getch();
} while(!feof(faa) && !feof(ab) && !(feof(ac));
fclose(faa);
fclose(fab);
fclose(fac);

Another problem is that this loop will only exit once fa is done - i.e. the first file. What do you want to do if the other files still have input? They may be all the same length, but it is wise to not always assume that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top