Need EOF help trying to extract data from .dat file

This is a discussion on Need EOF help trying to extract data from .dat file within the C Programming forums, part of the General Programming Boards category; Hello all, I am trying to extract weather data from a (.dat) file. The contents of this space delimited file ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    1

    Need EOF help trying to extract data from .dat file

    Hello all,

    I am trying to extract weather data from a (.dat) file. The contents of this space delimited file look like this:

    2534 80 12 22 105 134 0
    2971 74 9 20 106 163 0
    2971 62 1 11 112 102 0
    ...
    ...

    My problem is that I don't know the code to loop to the EOF in my while loop. I know it exists just not how to do it in this case.

    Here is my code:

    Code:
    #include <stdio.h>
    
    #define arrSize 33
    
    int main()
    {
    	/* variable declarations */
    	int stationNumberInput;
    
    	int loop1;
    
    	int	stNum,
    		year,
    		month,
    		day,
    		hiTemp,
    		lowTemp,
    		rain;
    
    	int tempArray[arrSize] = {0};
    	int daysArray[arrSize] = {0};
      
    	double avg;
      
    	FILE *in;
    
    	/* prompt for inuput */
    	printf("Please enter station number: ");
    	scanf("%d", &stationNumberInput);
    
    	/* attempt to open the file */
    	in = fopen("weather.dat", "r");
    	if(in == NULL)
    	{
    	    printf("Error opening file. Program terminated.\n");
    	    return(1);
    	}
    
    	/* scan the file, store the temp, keep track of number of days */
    
    	while( ??? )
    	{		
    		fscanf(in, "%d %d %d %d %d %d %d", &stNum, &year, &month, &day, &hiTemp, &lowTemp, &rain);
    
    		if(stNum == stationNumberInput)
    		{
    			tempArray[year-48] += (hiTemp - 100);
    			daysArray[year-48] += 1;
    		}
    	}
    
    	/* print results */
    	printf("\tYear\tRainfall\n");
    	printf("\t----\t--------\n");
    		
    	for(loop1=0; loop1<arrSize; ++loop1)
    	{
    	    avg = tempArray[loop1] / (double)daysArray[loop1];
    	    printf("\t%d\t%f\n", loop1+48, avg);
    	}
    
    	fclose (in);
    
    	return 0;
    }
    Thanks in advance.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    From the manual:
    The fscanf() function returns EOF when the scanning is terminated by reaching the end of the input stream. Otherwise, it returns the number of input arguments for which values were successfully scanned and stored. When a file input error occurs, the errno global variable may be set.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Hammer told you the answer!

    You could always try

    Code:
    while(fscanf(in, "%d %d %d %d %d %d %d", &stNum, &year, &month, &day, &hiTemp, &lowTemp, &rain) != EOF)
    {
    
    
    }
    Mr. C.

  4. #4
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    i might be repeating hammer, but it's important to realize that EOF is an integer, not a byte. therefore, it's returned by the operating system when it detects that user program x is trying to access space outside the size of the file. while fscanf avoids ambiguity, using functions like feof() can cause confusion among newbies, who use a char to accept the data returned, and wonder why they aren't getting an EOF.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-22-2008, 12:19 AM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 04:53 AM
  3. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  4. Editing a data file
    By Strait in forum C++ Programming
    Replies: 7
    Last Post: 02-05-2005, 04:21 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 07:51 PM

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