Thread: Checking if I'm at end of file?

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    26

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

  2. #2
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    Code:
    char buff[BUFSIZ]; // BUFSIZ defined in stdio
    
    while(fgets(buff, sizeof (buff), inp1)){
        sscanf(buff,"%d %s %s %lf",&cAtom.num,cAtom.name,cAtom.symbol,&cAtom.weight);
    fgets returns NULL on end-of-file, when fgets fails you can assume that you've reached the end as this appears to be homework ( you don't need it to be bulletproof). sscanf performs the same operation as fscanf except it reads from buff instead of the file.
    p.s. What the alphabet would look like without q and r.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    26

    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

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    26
    Well.. I know how to read in the lines, I meant how will I check if I reach end of file with the new way? I don't understand how I can use the fgets to check if I'm at end of file if I will now need to use it 4 times to get the line?

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    26
    Ok I tried to add in what you said to do.

    With what I came up with I think it can't read the space between the weight and the next name.

    I came up with this:

    Code:
    while(fgets(buff, sizeof (buff), inp1)){
        sscanf(buff,"Number: %d",&cAtom.num);
        if ( fgets(buff, sizeof (buff), inp1) == NULL ) {
            printf( "Unexpected EOF reading name");
            break;
        }
        sscanf(buff,"Name: %s",cAtom.name);
        if ( fgets(buff, sizeof (buff), inp1) == NULL ) {
            printf( "Unexpected EOF reading symbol");
            break;
        }
    	sscanf(buff,"Symbol: %s",cAtom.symbol);
        if ( fgets(buff, sizeof (buff), inp1) == NULL ) {
            printf( "Unexpected EOF reading weight");
            break;
        }
    	sscanf(buff,"Weight: %lf",&cAtom.weight);
        if ( fgets(buff, sizeof (buff), inp1) == NULL ) {
            printf( "Unexpected EOF reading number");
            break;
        }
    }

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You'll have to "allow for" blank lines, or in other words, ignore them. You can easily detect them:

    1) test the first character of the array, if its \n, the line is empty:
    >>if (buff[0] == '\n')

    2) Again, check the first character, if its not one you're expecting, ignore the line:
    >>if (buff[0] == ' ')
    or
    >>switch (buff[0])

    3) you could assume that every nth line is blank.

    There are other ways as well (check out the return code from those sscanf()'s you're using )
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-06-2008, 02:43 PM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM