C: END OF FILE doesn't work for me

This is a discussion on C: END OF FILE doesn't work for me within the C Programming forums, part of the General Programming Boards category; Hi guys, I've googled around the web for some time now trying to find why my EOF doesn't work. I'm ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    3

    C: END OF FILE doesn't work for me

    Hi guys, I've googled around the web for some time now trying to find why my EOF doesn't work.

    I'm supposed to write a function, and I'm mostly curious to why EOF doesn't work.
    The program reads in the text-file in a different function.
    =================================================
    Code:
    int las_data(FILE *tsin, struct seriepost serie[])
    {
    	int a = 0;
    	int i = -1;
    	int c = 1;
    	float tal;
    	int antal_tal = 0;
    	int antal_tal_totalt = 0;
    	int loop_continue = 1;
    	int n;
    
    	/*
    	Totnr = Sum of all numbers
    	outnr = How many numbers that are over or below 5% of 200
    	medel = average
    	*/
    
    	serie[20].totnr = 0;
    	fscanf(tsin, "%f", &tal);
    	while (tal != EOF && a != 20 && loop_continue == 1)
    	{
    		a++;
    		serie[a].totnr = 0;
    		serie[a].outnr = 0;
    		serie[a].medel = 0;
    		antal_tal++;
    		while (tal != 0.0)
    		{
    			//Write in the number
    			//Check if number is 0
    			serie[a].totnr = serie[a].totnr + tal;
    			if (tal > 231 || tal < 209)
    			{
    				serie[a].outnr = serie[a].outnr + 1;
    			}
    
    			fscanf(tsin, "%f", &tal);
    			//printf("Serie%d Tal: %.2f\n",a,tal);
    			antal_tal++;
    			//Go back to while loop
    		}
    		if (tal == 0.0)
    		{
    			//Count average etc
    			//Removes 0 from average
    			antal_tal = antal_tal - 1;
    			if (antal_tal == 0)
    			{
    				antal_tal = 2;
    			}
    			serie[a].medel = serie[a].totnr / (float)antal_tal;
    			antal_tal_totalt = antal_tal_totalt + antal_tal;
    			antal_tal = 0;
    
    		}
    		fscanf(tsin, "%f", &tal);
    		loop_continue = (int)tal;
    		if (loop_continue != EOF) // THIS IS WHERE IT ONLY RETURNS 0
    		{
    			loop_continue = 1;
    		}
    		else
    		{
    			loop_continue = 0;
    		}
    			
    
    		//Jumps back to loop
    	}
    	while(c == 1) //Only for testing
    	{
    		scanf("%d", &c);
    	}
    	
    	return a;
    }
    =================================================
    I translated most of the comments in the program, the comments with //printf are just for testing.
    It always goes to 20.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    You might want to read up on the return value of fscanf.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    EOF isn't a good test for end of file, unfortunately. If the last line has a newline on it, you can wind up having repeat values for the last input.

    fscanf() will return the number of items it has scanned and stored into a variable. That should be your test to continue looping or not, instead of EOF.

    Every fscanf() needs to have it's return value checked to see if you've reached the EOF, instead of just one of them. (or is 0.0 a marker for the end of data?)

    How many data items are there, in the file? If there are 20, then of course, 20 is OK.

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    3
    This is the file scanned:
    Code:
        210.5    210.9    213.8    209.3    214.7    214.2    214.4    211.8
        213.9    213.6    214.5    214.3    213.2    215.5    210.9    212.3
        215.4    212.4    210.6    210.4    216.2    209.9    211.4    213.7
        213.9    209.2    210.4    211.8    215.8    216.4    216.1    209.6
        217.5    209.8    210.8    216.4    209.4    217.0    212.3    217.7
        216.5    214.4    217.2    215.5    217.6    211.6    211.8    213.7
        217.0    211.3    217.2    211.2    210.2    215.1    217.2    211.9
        216.8    217.5    212.1    217.5    212.9    217.2    211.0    215.2
        216.8    211.6    210.9    216.4    210.8    213.0    210.9    217.2
        217.3    216.2    213.4    209.2    215.9    212.1    210.5    211.3
        215.5    212.7    216.6    214.2    215.9    209.4    212.1    217.6
        213.2    213.5    217.6    214.6    211.1    209.6    213.6    213.7
        209.2    210.4    214.7    215.0    0.0
    
        207.3    211.7    208.0    209.4    208.2    209.7    211.7    211.1
        212.6    207.8    208.0    211.6    207.7    213.2    213.3    212.0
        214.5    214.4    211.9    209.3    208.0    207.4    213.6    214.4
        214.3    211.0    214.6    214.4    210.9    211.9    211.5    215.3
        209.2    215.2    213.4    214.7    209.1    209.9    213.5    213.5
        212.9    211.5    212.7    212.4    214.2    212.8    209.4    212.3
        209.2    210.5    210.2    212.7    208.7    208.6    214.2    213.4
        209.6    210.5    213.2    210.2    210.7    207.9    213.7    210.3
        210.0    213.7    211.0    210.6    212.8    209.4    209.1    214.8
        209.4    211.5    211.7    207.4    207.1    210.1    208.1    207.7
        211.8    208.3    208.4    210.5    212.3    211.9    213.5    214.2
        214.2    214.9    211.5    214.5    208.6    214.5    209.3    211.2
        212.3    210.9    209.4    208.5    210.2    212.2    211.2    211.2
        207.7    0.0
    
        215.0    213.2    212.0    213.1    212.0    215.1    210.6    209.9
        216.5    208.5    212.1    215.4    208.3    212.3    209.6    208.6
        215.9    208.1    212.2    208.1    213.7    214.2    215.9    209.2
        214.9    215.2    210.2    215.0    216.0    212.0    212.3    208.8
        216.4    215.5    215.3    213.4    210.6    215.5    213.3    212.1
        209.5    214.8    212.7    215.9    211.6    211.8    210.4    215.0
        209.4    208.2    212.2    209.8    214.9    215.3    210.2    214.8
        216.4    212.5    211.9    208.3    215.9    212.3    212.0    212.7
        212.1    212.8    211.3    210.0    214.5    213.6    211.1    211.0
        210.3    215.1    208.5    210.9    214.4    211.6    209.2    210.6
        210.3    213.3    211.9    209.4    210.3    210.4    208.3    210.3
        212.5    211.0    210.9    213.1    211.6    211.3    211.3    0.0
    
        208.6    214.5    208.4    213.4    214.1    212.2    213.4    217.0
        215.4    210.8    211.0    209.7    210.6    209.6    215.2    211.0
        218.2    215.9    208.3    209.7    211.5    214.7    213.6    216.8
        208.4    211.6    208.2    209.9    214.8    211.4    211.4    216.1
        212.9    213.7    217.3    216.9    216.8    211.4    215.0    211.8
        209.3    213.0    217.7    216.2    210.7    216.9    217.1    214.9
        214.0    217.7    214.7    214.7    209.6    208.9    208.4    209.7
        212.1    214.8    217.5    218.7    216.9    215.0    218.0    213.7
        217.9    211.0    216.3    211.0    210.7    211.1    211.1    209.9
        208.6    214.0    209.9    213.6    214.3    209.5    208.7    215.7
        209.8    218.6    218.4    209.3    209.7    212.6    210.3    213.9
        212.0    208.6    218.7    213.9    212.3    214.0    210.9    209.8
        217.9    216.0    215.1    218.4    0.0
    
        210.4    217.6    215.4    216.4    218.8    216.6    211.6    219.5
        222.4    215.6    219.8    214.7    210.4    209.2    219.9    210.2
        221.5    209.6    213.3    215.8    221.0    216.4    209.5    213.0
        209.8    212.7    219.7    217.1    211.2    217.9    216.7    212.0
        220.3    213.6    212.2    219.2    210.5    221.5    220.2    217.7
        218.7    215.3    222.5    211.8    210.3    208.7    208.8    213.6
        209.0    209.0    217.8    216.1    216.6    215.1    215.4    216.0
        212.0    211.5    214.1    212.3    219.9    217.7    219.5    218.7
        218.6    208.5    216.3    222.7    210.3    213.4    215.2    222.2
        217.7    218.8    222.6    208.8    209.8    218.0    218.6    219.8
        211.3    221.8    212.5    209.7    221.4    215.2    212.8    210.4
        218.5    219.5    214.2    209.4    209.8    210.8    217.9    211.3
        211.6    219.2    211.3    215.6    212.6    222.3    212.7    212.9
        0.0
    
        210.9    208.5    219.4    211.8    213.4    220.7    210.0    220.1
        209.7    208.3    214.0    220.3    210.9    220.0    212.9    224.1
        225.2    218.8    218.3    215.3    221.9    218.2    224.2    222.8
        224.4    215.8    227.4    217.1    208.7    213.2    210.3    227.1
        210.8    212.8    226.5    220.6    212.4    217.1    220.3    213.1
        217.1    210.2    221.8    219.6    215.0    214.0    222.2    208.2
        222.1    212.7    226.4    209.6    219.9    225.8    208.3    221.2
        209.6    214.7    220.3    209.4    219.2    209.4    216.2    208.3
        210.3    215.3    221.8    222.3    223.3    217.2    225.1    215.4
        210.6    221.4    215.5    216.0    215.5    214.0    223.6    225.1
        222.4    220.2    212.1    218.6    208.3    215.3    211.2    217.7
        218.6    219.6    222.1    217.5    225.7    219.4    214.9    211.9
        0.0
    
        210.6    220.4    229.0    215.5    210.5    229.1    222.4    228.4
        230.0    226.7    212.8    219.7    228.5    225.5    213.3    215.1
        230.6    222.5    216.3    222.2    219.5    227.1    216.9    230.9
        227.5    226.0    227.3    228.2    227.4    216.4    225.8    209.3
        223.0    228.7    230.9    224.7    221.6    227.3    225.8    228.4
        226.3    211.2    228.1    213.7    215.0    210.7    217.2    228.8
        208.7    213.4    225.7    221.8    227.4    231.1    229.0    211.1
        222.5    210.4    217.4    216.2    230.0    214.9    211.4    226.1
        228.2    226.5    227.6    225.8    230.7    212.6    218.4    225.4
        222.1    229.8    210.2    231.4    228.0    213.3    217.6    209.2
        231.4    221.1    215.1    215.4    212.9    220.3    213.0    211.8
        222.5    217.5    227.0    209.2    222.9    223.4    223.1    217.2
        218.7    223.3    226.1    225.7    215.7    226.3    0.0
    
        230.2    211.7    218.2    212.8    219.5    213.8    223.7    216.2
        210.4    228.0    224.0    228.9    214.7    211.2    210.1    229.4
        229.4    221.7    222.9    212.9    218.7    211.1    212.0    212.2
        212.9    222.7    223.1    210.2    221.3    220.8    230.6    216.0
        224.3    225.8    213.2    214.6    220.0    212.1    219.5    229.5
        224.9    211.5    213.0    220.3    214.7    223.8    231.8    219.1
        226.1    231.6    217.5    218.0    218.1    220.7    216.9    223.5
        212.9    227.9    211.3    221.5    214.5    215.3    219.7    214.0
        215.7    226.2    210.1    216.3    222.7    212.0    230.2    230.4
        220.4    218.0    216.6    228.6    218.8    216.1    213.5    217.1
        225.6    219.2    226.2    226.5    229.5    219.0    211.6    221.8
        228.2    215.8    213.4    230.5    219.3    226.8    226.9    229.5
        222.0    219.3    211.5    211.6    213.6    220.7    221.1    225.2
        213.9    231.8    219.9    221.7    216.6    226.7    228.7    224.5
        230.9    210.4    224.9    222.4    210.4    229.4    231.2    222.9
        0.0
    
        212.9    230.4    227.7    213.6    214.8    218.0    214.2    225.1
        221.9    218.6    223.1    231.7    222.5    224.2    221.0    229.7
        216.9    233.2    221.6    216.0    230.7    218.2    226.1    218.9
        228.0    215.7    213.8    226.7    228.5    232.3    219.0    229.9
        220.2    216.3    215.4    211.6    229.4    224.0    212.0    221.9
        215.2    216.0    224.5    227.6    229.0    225.7    227.4    218.5
        229.0    213.7    228.0    225.9    222.1    225.6    222.8    214.6
        224.3    224.1    212.0    225.1    212.4    225.1    220.2    226.9
        211.6    228.3    221.6    227.8    216.5    228.9    217.8    220.5
        232.7    213.1    223.3    213.1    215.2    232.8    215.7    223.2
        224.9    230.6    217.7    228.0    232.1    232.6    225.0    227.0
        213.5    212.0    222.1    221.4    226.7    231.9    213.6    233.0
        229.1    224.4    212.4    222.0    0.0
    
        228.3    228.0    213.5    218.8    220.2    217.2    228.4    215.7
        228.6    230.9    223.8    217.5    216.6    224.5    226.6    224.2
        223.0    234.5    223.8    224.9    215.7    228.3    225.7    233.6
        223.7    232.4    218.0    231.3    219.8    225.6    235.6    225.2
        231.3    221.0    226.8    228.0    233.1    235.3    221.6    226.7
        220.0    221.2    234.9    221.3    225.8    235.2    229.6    235.4
        227.5    230.8    233.0    234.3    226.9    232.2    222.0    220.0
        214.5    222.0    220.0    229.0    229.5    218.8    233.0    220.2
        230.0    225.1    230.5    217.1    224.6    217.7    214.2    233.8
        233.9    233.0    233.3    224.0    227.7    235.7    220.1    223.6
        226.7    216.0    234.6    217.2    230.3    215.9    217.0    225.9
        214.7    224.4    229.6    225.1    235.2    235.5    232.2    220.8
        235.8    220.9    224.5    217.5    0.0
    
        219.0    232.5    218.3    216.1    223.1    228.3    221.1    223.0
        234.9    215.4    226.0    220.3    233.2    217.5    216.9    222.2
        235.3    222.2    217.8    220.6    215.5    217.8    229.9    226.7
        229.3    234.2    227.8    220.5    227.2    224.8    217.4    236.8
        216.3    234.9    219.0    228.3    222.8    229.7    235.3    217.9
        224.2    219.3    236.7    231.6    218.5    223.2    236.7    221.3
        226.7    225.6    226.9    225.9    223.3    219.6    215.8    220.5
        235.5    228.6    221.0    232.1    230.8    235.5    218.7    228.5
        226.2    236.0    230.6    231.8    227.4    220.7    223.4    222.2
        216.2    224.8    237.3    226.4    237.3    237.3    228.0    234.4
        221.8    234.4    230.9    215.8    225.7    229.7    229.0    229.7
        231.1    232.5    231.3    231.6    221.4    223.3    227.7    216.4
        237.6    227.8    221.5    222.2    0.0
    
        230.2    230.4    220.1    236.1    225.0    226.5    225.8    222.4
        218.5    231.1    222.6    233.9    220.0    240.7    218.4    224.0
        232.4    226.2    239.6    218.7    232.1    232.2    223.4    220.7
        229.2    238.3    229.1    222.3    223.6    233.3    231.6    234.9
        236.9    233.7    224.3    236.2    226.0    227.5    227.3    231.4
        228.7    222.9    228.3    225.4    239.2    232.5    238.9    221.9
        219.4    223.4    226.7    219.8    238.6    221.5    234.3    218.1
        240.2    219.5    227.8    233.6    220.5    226.4    225.6    219.0
        237.3    236.3    238.8    239.1    218.1    233.0    219.1    230.4
        238.7    228.8    222.5    220.3    240.6    235.3    237.2    225.4
        238.8    221.3    219.4    236.4    228.7    220.1    230.4    230.7
        235.7    239.6    221.9    219.9    232.1    218.1    223.2    238.1
        239.3    240.8    235.8    220.6    0.0
    
        237.7    241.1    236.0    220.3    227.4    228.3    233.5    225.4
        234.3    232.8    226.5    240.8    224.4    241.2    240.8    232.8
        240.6    240.2    235.5    220.7    227.9    220.7    237.5    237.9
        241.7    223.1    220.9    230.6    227.7    228.2    230.4    235.4
        239.8    239.1    228.4    223.4    224.7    230.6    223.3    239.8
        229.3    232.3    239.7    237.5    241.7    230.1    235.8    222.9
        231.4    237.3    235.0    240.3    234.6    229.3    234.2    221.8
        231.4    234.6    235.1    236.1    226.7    220.0    228.3    226.7
        222.5    235.6    233.2    222.7    228.9    237.7    234.3    240.8
        220.5    227.9    223.0    226.5    220.9    231.7    241.3    233.8
        224.2    234.3    231.8    233.3    241.5    232.6    228.9    222.1
        238.6    232.0    241.2    220.9    240.6    242.5    235.3    220.0
        221.1    231.8    229.1    225.2    223.4    237.9    221.9    223.8
        228.3    241.0    237.8    231.4    227.0    241.5    241.8    229.8
        233.2    223.5    239.8    237.0    219.7    230.4    220.8    238.6
        0.0
    
        234.1    222.4    238.1    233.5    224.3    228.1    228.3    224.6
        240.4    223.2    231.0    241.3    224.8    231.4    241.2    227.8
        231.3    229.3    231.4    221.2    231.7    232.2    240.6    230.3
        242.2    237.7    239.8    222.4    242.6    234.1    239.7    222.0
        237.4    240.1    224.2    242.6    222.2    222.8    224.2    233.9
        230.3    220.9    242.0    235.9    235.3    228.0    237.2    241.1
        233.7    224.1    220.0    223.7    226.2    220.7    235.2    238.5
        226.1    232.9    223.8    231.0    230.5    241.9    228.2    233.8
        227.4    233.6    223.5    232.6    232.0    227.7    236.9    227.6
        227.0    231.0    233.0    224.2    221.6    227.2    232.6    226.6
        221.9    240.2    222.5    235.9    224.8    225.2    242.7    221.1
        223.0    229.7    221.1    238.8    230.1    241.5    229.8    241.5
        232.4    225.9    237.7    224.2    0.0
    
        228.2    245.2    230.9    242.2    237.8    232.6    235.6    225.8
        231.6    241.5    235.7    238.9    234.7    230.4    237.6    224.6
        241.6    242.3    231.2    224.4    242.4    244.5    229.0    226.1
        230.8    233.6    227.2    234.0    241.2    224.4    225.3    232.0
        222.8    223.6    228.4    222.3    231.8    237.5    240.6    232.9
        223.1    241.0    231.3    228.9    238.1    238.4    234.7    233.1
        235.4    237.5    226.2    226.4    232.0    233.7    227.6    238.4
        224.1    222.8    226.2    236.3    238.9    235.3    231.8    230.8
        233.3    234.7    231.1    244.7    224.5    235.5    235.7    223.5
        241.0    237.7    241.7    245.0    232.4    227.7    238.0    233.1
        240.2    230.0    232.9    243.9    232.8    224.7    239.8    237.8
        242.3    232.7    223.3    230.9    245.5    230.3    241.2    240.2
        226.0    230.8    233.3    228.7    0.0
    The function adds each number to serie[a].totnr,outnr,medel until it finds a 0.0
    then it goes out of the 2nd loop and back to the 1th loop, increases a by 1 and repeats.

    It works perfectly (of what I know) until it finishes the last box of numbers. (15 boxes of numbers).
    Last edited by LordDz; 08-12-2011 at 05:35 AM.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Code:
    fscanf(tsin, "%f", &tal);
    loop_continue = (int)tal;
    if (loop_continue != EOF) // THIS IS WHERE IT ONLY RETURNS 0
    I don't think you understand what EOF is. It's a macro used to signal end-of-file and error conditions as a return value. EOF is not a value you can get through input, which means you'll never see it as the result of a *scanf() conversion. In other words, tal will never be equal to EOF, but fscanf() might return EOF. However, a better way to check for success with the *scanf() functions is to count conversions. Since the number of conversions is the return value (or EOF, but that's irrelevant) you can check for the number of format specifiers. If the return value doesn't match the number of format specifiers, something unexpected happened, such as a stream error, conversion error, or end-of-file:
    Code:
    loop_continue = (fscanf(tsin, "%f", &tal) == 1);
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    3
    Quote Originally Posted by Prelude View Post
    I don't think you understand what EOF is. It's a macro used to signal end-of-file and error conditions as a return value. EOF is not a value you can get through input, which means you'll never see it as the result of a *scanf() conversion. In other words, tal will never be equal to EOF, but fscanf() might return EOF. However, a better way to check for success with the *scanf() functions is to count conversions. Since the number of conversions is the return value (or EOF, but that's irrelevant) you can check for the number of format specifiers. If the return value doesn't match the number of format specifiers, something unexpected happened, such as a stream error, conversion error, or end-of-file:
    Code:
    loop_continue = (fscanf(tsin, "%f", &tal) == 1);
    Thank you, just the kind of reply I needed.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by LordDz View Post
    Thank you, just the kind of reply I needed.
    Because when the first two people told you to go read what fscanf returns, and to use that, it wasn't good enough?


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by quzah View Post
    Because when the first two people told you to go read what fscanf returns, and to use that, it wasn't good enough?
    Quzah.
    Well, it wasn't "divine" knowledge with them......
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. first time compiling with external .c and .h file, doesn't work
    By Adam Rinkleff in forum C Programming
    Replies: 17
    Last Post: 06-21-2011, 05:38 PM
  2. Doesn't work to save my arrays to a text file!
    By zergling in forum C Programming
    Replies: 35
    Last Post: 10-23-2010, 09:27 PM
  3. .exe file doesn't work
    By sara12345 in forum C Programming
    Replies: 2
    Last Post: 12-27-2009, 11:18 AM
  4. binary file i/o -- why doesn't this work?
    By R.Stiltskin in forum C++ Programming
    Replies: 4
    Last Post: 01-29-2009, 08:39 AM
  5. Writing BMP file... doesn't work correctly
    By tin in forum Game Programming
    Replies: 3
    Last Post: 12-28-2005, 03:40 AM

Tags for this Thread


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