Thread: Reading text file.

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    7

    Reading text file.

    Hello,
    I have a problem with finding the end of the text file. I created a piece of code to scan my txt file for the commands, but the problem is that for some reason one of the commands gets implemented twice. I checked everything and the problem is in this piece. It's like feof finds the end of the file not correctly. Could someone give me a couple of new ideas how to make a loop and to read everything in the file correctly? Thanks
    Code:
    int FileAccess( )
    {
    	int ret;
    	FILE *acc_file = NULL;
    	acc_file = fopen(notename, "r");
    	if (acc_file == NULL)                     //Checking if file exists
    	{
    		printf("Error opening file! Please check whether the file exists...\n\n");
    		return 1;
    	}
    	else
    	{
    		while (!feof(acc_file))
    		{
    			
    				fscanf(acc_file,"%s",str);
    				if (strcmp(str,"FL")==0)
    				{
    					fscanf(acc_file,"%d %d %d", &flood_colour.red,     &flood_colour.green, &flood_colour.blue);
    					FloodImage(flood_colour);
    				}
    				else if (strcmp(str,"LI")==0)
    				{	
    					fscanf(acc_file,"%d %d %d %d", &x_start, &y_start, &x_finish, &y_finish);
    					fscanf(acc_file,"%d %d %d", &line_colour.red, &line_colour.green, &line_colour.blue);
    					DrawLine(x_start, y_start, x_finish, y_finish, line_colour);
    				}
    				else if (strcmp(str,"CH")==0)
    				{ 
    					CheckpointImage( );
    					
    				}
    				else if (strcmp(str,"RE")==0)
    				{
    					RefreshImage( );
    				}
    				
    				else if (strcmp(str,"EX")==0)
    				{
    					break;
    				}
    		}
    		fclose(acc_file);
    		printf("File was successfully read...\n");
    		printf("All the tasks were executed...\n\n");
    		return 0;
    		
    	}
    }

  2. #2

  3. #3
    Novice
    Join Date
    Jul 2009
    Posts
    568
    I would suggest reading the documentation for fscan() and considering using that for a loop condition, if appropriate.

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    Thanks, I understood the root of the problem, but could you tell me which function can i use to scan and check a return code from it? fscanf seem doesnt work when i write smth like this
    while(fscanf(acc_file,"%s",str) != EOF), can't use fgets too, becuase there is limit in characters.

    Sry, fixed it. Thanks to everyone for the page
    Last edited by pe4enka; 04-30-2010 at 01:29 PM. Reason: addition of info

  5. #5
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Works well for me. What do you mean by "doesn't work"?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with finding word frequency in a text file.
    By aeolusaether in forum C Programming
    Replies: 15
    Last Post: 04-04-2010, 09:59 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. Reading Character at a time from a text file
    By Giania in forum C Programming
    Replies: 8
    Last Post: 02-25-2006, 03:17 PM