Thread: end of file

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    24

    end of file

    I was writing a program to read text in a file and store the words.

    MY inputTextFile was aw.txt
    Code:
    first line of file
    this is last
    my program to read the file
    Code:
    #include <stdio.h>
    int main(void){
    	FILE *fp;
    	char readInLine[200];
    	int i=0;
    	fp=fopen("aw.txt","r"); //open file
    
    	while(fgets(readInLine,200,fp)!=NULL) {   //read each line of file
    		i=0;printf("\n\nCurrent Line:&#37;s",readInLine);
    		printf("\n------------");
    
    		while(readInLine[i]!='\0'){ //run till end of line
    			if(readInLine[i]==' '){   //if space found
    				printf("\nfound a space");
    			} 
    			else if(readInLine[i]=='\n'){   //if end of line found
    				printf("\nfound a end Line");
    			}
    			else{
    			}
    			i++;		
    		}
    		
    	}
    }
    The program worked fine for the first line.found 3 spaces,the found end of the line.
    for the second line it found 2 spaces. MY QUESTION WAS HOW DO I DETECT END OF LINE HAS BEEN DETECTED FOR THE LAST LINE ?

    I was not able to store the word "last".How do i do that? readInLine[i]=="WHAT " ???
    Hope you guys understand my problem and can help me out.thanx

  2. #2
    Registered User
    Join Date
    Dec 2005
    Posts
    136

    Smile

    I hope this will work as if the end of line has been found before end of file(EOF).
    What you say?
    S_ccess is waiting for u. Go Ahead, put u there.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    When you exit the while loop - the readInLine array still contains last line read (if any was read at all)
    just check if the end line symbol is present in the array at this moment
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    When the while loop exits, you should check what feof() and ferror() return. If only feof() returns true, then you successfully reached the end of the file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 01:30 PM
  4. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 08:26 PM
  5. checking for end of file
    By linucksrox in forum C Programming
    Replies: 7
    Last Post: 06-01-2004, 01:41 AM