Thread: problem with EOF

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    Greece
    Posts
    2

    problem with EOF

    Hi,i am writing a program which among the others,it has to store all the words from txt files in a hash table.the problem is that the program seems not to find the EOF in the first text,so it stops in that point.i 'm giving the specific part of the code.i will appreciate any help!!
    Code:
    	for(counter=0;counter<p;counter++)
    	{
    		while((c=fgetc(stream[counter]))!=feof(stream[counter]))
    		{
    			i=0;
    			while(c!='\0')
    			{
    				buffer[i]=c;
    				c=fgetc(stream[counter]);
    				printf("%c",buffer[i]);
    				i++;
    			}
    			buffer[i]=(char)"/0";
    		}
    	}

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    The feof function basically returns a yes(nonzero)/no(zero) integer value. The fgetc function returns an integer value cooresponding to the value of the character read from the file or the value EOF to indicate if the end-of-file has been reached. You don't need to call feof at all in your loop, just test the return value from the fgetc function for equality to EOF.

    Code:
    while( (c=fgetc(stream[counter])) != EOF)
    Make sure c is declared as an int and not a char.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Code:
    while((c=fgetc(stream[counter]))!=feof(stream[counter]))
    That line of code won't work. What happens if you read a 0 in from the file? Also, feof() returns non-zero when it reaches the end of the file. Why are you comparing that result to the return value of fgetc()?

    I think you meant to do something like:
    Code:
    while((c=fgetc(stream[counter])) && !feof(stream[counter]))
    which can be shortened to:
    Code:
    while((c=fgetc(stream[counter]))!=EOF)

  4. #4
    Registered User
    Join Date
    Jan 2006
    Location
    Greece
    Posts
    2
    thanks for the help,but still nothing !another idea????

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    replace
    Code:
    buffer[i]=(char)"/0";
    with
    Code:
    buffer[i]='\0';
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. small reference problem
    By DavidP in forum C++ Programming
    Replies: 6
    Last Post: 06-21-2004, 07:29 PM
  2. Problem with destructors.
    By Hulag in forum C++ Programming
    Replies: 7
    Last Post: 06-11-2004, 12:30 PM
  3. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM