Thread: File Management in C

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    20

    File Management in C

    The question:Why do i have to write if(c!=EOF) statement again in do-while block in the following code?

    The thing is that if i don't write the if(c!=EOF)again in the block,it outputs the last word again in the end.Can you tell me why that is happening?

    Code:
    #include<stdio.h>
    
    main()
    
    {
    	
    	FILE*pt;
    	char array[100];
    	int c;
    	pt=fopen("text.txt","r"); 
    	if(pt==NULL) printf("the file doesn't exist");
    	else {
    		do {
    			c=fscanf(pt,"%s",array);
    			if(c!=EOF)
    			printf("%s",array);
      	       } while(c!=EOF);
      	       
    	    }
    	    
    	fclose(pt);
    	
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Because you're not using EOF consistent with the way it actually works.

    Cprogramming.com FAQ > Why it's bad to use feof() to control a loop

    Checking for EOF works exactly the same way as feof() (that is to say: it doesn't).

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    20
    I didn't understand you.English is not my native langauge.So can you be clear?

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Read the link he has provided.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,398
    Your question is "why do I need to check if I've reached the end of the file before I go off assuming I haven't?"

    Here's a similar algorithm for walking through a doorway:

    1. Approach the door
    2. Walk through the door
    3. Check whether the door is open

    Hmm...
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User
    Join Date
    Dec 2010
    Posts
    20
    What does fscanf function return here?

    You are saying is it because of do-while statement?

    First do then check?

    I tried with normal While.It still has that problem.

  7. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Why do you keep refusing to read the material provided to you? The answers you seek are explicit in there.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM