Checking if I'm at end of file?
Ok the book I'm using does not explain how to use EOF at all!
My project is to read from two text files, and then combine what I get into a binary file. So far I can read from one file, then put that into the binary file, but I have no idea how to check if I'm at the end of the file.
Right now I've been testing it with only 1 line in my file "atoms1.txt"
The line looks like this.
11 Sodium Na 22.99
Here is my code(basically my while(!EOF) was a complete guess):
Code:
#include <stdio.h>
#define STRMAX 20
typedef struct{
int num;
char name[STRMAX];
char symbol[STRMAX];
double weight;
} atom_t;
void readCOMBINE(FILE *inp1, FILE *inp2, FILE *outp);
int main(){
FILE *inp1, *inp2, *outp;
inp1 = fopen("atoms1.txt", "r");
inp2 = fopen("atoms2.txt", "r");
outp = fopen("combined.bin", "wb");
readCOMBINE(inp1, inp2, outp);
fclose(inp1);
fclose(inp2);
fclose(outp);
return 0;
}
void readCOMBINE(FILE *inp1, FILE *inp2, FILE *outp){
atom_t cAtom;
while(!EOF){
fscanf(inp1,"%d %s %s %lf",&cAtom.num,cAtom.name,cAtom.symbol,&cAtom.weight);
fwrite(&cAtom.num, sizeof ( int ), 1, outp);
fwrite(cAtom.name, sizeof ( char ), STRMAX, outp);
fwrite(cAtom.symbol, sizeof ( char ), STRMAX, outp);
fwrite(&cAtom.weight, sizeof( double ), 1, outp);
}
}
Thanks, but I have another question
This actually isn't for homework, more of trying to teach myself how to program kind of thing...
Well I used what you gave me and it worked perfectly now, but how would I be able to convert what you gave me if I change how the text file looks.
Right now it was like:
11 Sodium Na 22.99
If I change my file to store the data like..
Number: 11
Name: Sodium
Symbol: Na
Weight: 22.99
Number: 20
Name: Calcium
Symbol: Ca
Weight: 40.08