fscanf and EOF

This is a discussion on fscanf and EOF within the C Programming forums, part of the General Programming Boards category; I have written a function that takes input from a text file called "fridge.txt" with its contents being: milk carton ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    5

    fscanf and EOF

    I have written a function that takes input from a text file called "fridge.txt" with its contents being:

    milk carton 4 2 .5
    bread loaf 2 2 1
    VB slab 20 40 10

    I can read it into an array fine, and print it fine but the for loop I use needs to stop when it reaches the end of the file i.e after it gets to "10". It is fine when I know how many lines there are in fridge.txt but it is dynamic so instead of having "i < 4", I need to use some EOF function so if there were 20 it would print out 20 lines not 4. I tried using fscanf(...) != EOF but it skips the first line, prints the second, skips the third, prints the fourth.

    Code:
    void PrintInventory()
    {
    
                    //Declare and initialise variables required
    	int i = 0;											
    	struct Inventory list[50];
    
                    //Declare inventoryList as a file
    	FILE *inventoryList;											//Open fridge.txt for reading
    	inventoryList = fopen("fridge.txt", "r");			
    	printf("CURRENT INVENTORY:\n\n");
    
                    //Read contents of fridge.txt into array List until eof
    	for(i = 0; i < 4; i++)											
    	{
    
    		fscanf(inventoryList,"%s %s %f %f %f", &list[i].productName, &list[i].measure, &list[i].currentAmount, &list[i].optimalAmount, &list[i].dangerousAmount);
    		
                      printf("Item Name: %s\nCurrent Amount: %.1f %s<s>\n\n", list[i].productName, list[i].currentAmount, list[i].measure);
    				
    	} 
    
    	
                    //Closes inventoryList for reading
                    fclose(inventoryList);									
    	system("pause");
    
    }
    Im not quite sure how to do this but any help would be greatly appreciated.

    .ZG.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    Code:
    while ( i < 50 &&
            fscanf(inventoryList,"%s %s %f %f %f", 
                &list[i].productName, &list[i].measure, &list[i].currentAmount,
                &list[i].optimalAmount, &list[i].dangerousAmount) == 4 ) {
        printf("Item Name: %s\nCurrent Amount: %.1f %s<s>\n\n", 
            list[i].productName, list[i].currentAmount, list[i].measure);
        i++;
    }
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    5
    Thanks you so so much. Being trying all weekend due to my lack of knowledge of return values of fscanf. So fscanf's return value is the amount of elements its reading in?

    .ZG.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > So fscanf's return value is the amount of elements its reading in?
    That's what it says in the manual
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Never find EOF after improperly formatted input
    By MALDATA in forum C Programming
    Replies: 5
    Last Post: 09-30-2008, 01:24 PM
  2. fwscanf not doing the same thing as fscanf
    By bling in forum C++ Programming
    Replies: 6
    Last Post: 08-19-2008, 03:09 AM
  3. Fscanf endless loop
    By krum in forum C Programming
    Replies: 3
    Last Post: 12-04-2007, 11:14 PM
  4. looping fscanf
    By kvadre in forum C Programming
    Replies: 3
    Last Post: 10-26-2005, 05:44 AM
  5. fscanf problems
    By QuincyEQ in forum C Programming
    Replies: 2
    Last Post: 07-29-2002, 03:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21