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);
}
}