It's a small code, so I have correct the format for a better visibility in the forum.
Code:
#include
int main(){
char surname[10];
int name;
int matricno;
int mit801, mit802, total;
FILE *file, *file1;
/* Open an existing file */
file1 = fopen("newbroadsheet.txt", "w");
file = fopen("Broadsheett.txt", "r");
if ((file = fopen("Broadsheett.txt","r")) == NULL){
perror("Error: Unable to open a file");
}
else {
fprintf(file1, "University of Lagos\n");
fprintf(file1, "================================\n");
fprintf(file1, "Faculty of Sciences\n");
fprintf(file1, "================================\n");
}
do {
fscanf(file, "%s%d%d%d", name, &matricno, &mit801, &mit802);
fprintf(file1, "%s\t%d\t%d\t%d", name, matricno, mit801, mit802);
} while (!feof(file));
fclose(file);
fclose(file1);
return 0;
}
Line 1: i think that should be You open the file "Broadsheett.txt" two times (line 13 and 15).
And I think you have confused your self with the variables 'name' and 'surname'.
'name' is of type 'int' and 'surname' is a array of type 'char'.
Now look at your 'fscanf' and 'fprintf' functions.
And you should check if the file was opened successfully (both file).
I would write it this way:
Code:
…
if ((file = fopen("Broadsheett.txt", "r")) == NULL) {
perror("Error: Unable to open the file to read! exit.\n");
return 1;
}
if ((file1 = fopen("newbroadsheet.txt", "w")) == NULL) {
perror("Error: Unable to open the file to write! exit.\n");
fclose(file);
return 2;
}
…