Like Tree1Likes
  • 1 Post By brewbuck

End of File (eof)

This is a discussion on End of File (eof) within the C Programming forums, part of the General Programming Boards category; Basically I am reading in from a file and I am wondering how i can stop my while loop when ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    16

    End of File (eof)

    Basically I am reading in from a file and I am wondering how i can stop my while loop when i reach the end of my file. I looked up eof and even on the cprogramming.com website i read about eof and i just cant figure out how to use it. is eof a build in value? Basically atm I am using scanner to input ints and then char arrays and then once it reaches end of file i want it to stop. any tips.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Post your code.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    16
    I have no idea if this will work yet (rough draft) can only compile on school computers. was mostly wondering just how to implement eof in general....but here is code

    Code:
    /*	Calvin Moss
    	CSCI 112
    	LAB 08 3/20/2012
    	SCAN-TRON-3,000,000
    */
    #include <stdio.h>
    #include "Proto.h"
    #include <string.h>
    
    
    void fgetAnswers()
    {
    	int numAnswers = 0;
    	char answers[];
    	int missedQuestions[numAnswers];//position in array = quesiton number, value in that possition = numbert of people that missed it
    	int k;
    	for(k = 0; k<numAnswers; k++)
    	{
    		missedQuestions[k] = 0;
    	}
    	scanf("%d %s", &numAnswers, &answers[]);
    	boolean endOfFile = false;	
    	printf("Exam Report\n");
    	int i;
    	int j;
    	printf("Question  ");
    	for(i = 0; i<numAnswers; i++)
    	{
    		printf(" %d", i+1);
    	}
    	printf("\nAnswer    ");
    	for (j = 0; j<numAnswers; j++)
    	{
    		printf("%c", answers[j]);
    	}
    	prinft("\n ID   Score(%)\n");
    
    
    	while (endOfFile == false)
    	{
    		int iD;
    		char studentAnswers[numAnswers];
    		scanf("%d %s", &iD, &studentAnswers[]);
    		int numCorrect=0;
    		for(int i = 0; i<numAnswers;i++)
    		{
    			
    			if(studentAnswers[i] == answers[i])
    			{
    				numCorrect++;
    			}else 
    			{
    				missedQuestions[i] = i+1;
    			}
    		}
    		int score;
    		score = (numCorrect*100)/(numAnswers*100);
    		printf("%d %6d\n", iD, score);
    
    
    	}
    
    
    	printf("Question  ");
    	for(i = 0; i<numAnswers; i++)
    	{
    		printf(" %d", i+1);
    	}
    	printf("\nMissed by ");
    	for(i = 0; i < numAnswers; i++)
    	{
    		printf(" %c", missedQuestions[i]);
    	}
    
    
    
    
    
    
    
    
    
    
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,118
    Read your notes/documentation for scanf. Notice that scanf returns the number of items scanned. How can you use that to determine when to stop scanning?

    By the way, &studentAnswers[] looks wrong. I expect either studentAnswers or &studentAnswers[0].
    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

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    If using fgetc() for input, check whether the return value of fgetc() is equal to EOF.

    If using fscanf() for input, check whether the return value of fscanf() is less than the number of conversion operations you asked for (the number of specifiers in the format string).

    If using fread() for input, check whether the return value of fread() is less than the value of the nitems parameter (third parameter) of fread().

    Whatever you do, forget about the feof() function, it is incredibly error-prone. It is almost always a mistake to put a feof() check in the condition of a while loop. That's because EOF can only be correctly detected AFTER the first failed read operation. Thus, if you write a loop like this:

    Code:
    while (!feof(stream))
    {
        ...
    }
    It is possible that, although you may be at the end of file (i.e. the next read operation would fail), nevertheless feof() returns false and you enter the loop when you should not enter the loop.

    Example of a CORRECT loop for fgetc() based input:

    Code:
    int ch = fgetc(stream);
    while (ch != EOF)
    {
        ... do something with ch
        ch = fgetc(stream);
    }
    
    /* Or a less readable but commonly used alternative */
    
    int ch;
    while ((ch = fgetc(stream)) != EOF)
    {
        ... do something with ch
    }
    
    /* Or my personal preference, which some people find weird */
    
    int ch;
    for (ch = fgetc(stream); ch != EOF; ch = fgetc(stream))
    {
        ... do something with ch
    }
    Example of a CORRECT loop for fscanf() based input:

    Code:
    int a, b, c;
    while (fscanf(stream, "%d %d %d", &a, &b, &c) == 3)
    {
        ... do something with a,b,c
    }
    Example of a CORRECT loop for fread() based input:

    Code:
    char buffer[...];
    while (fread(buffer, size, nitems, stream) == nitems)
    {
        ... do something with the data in the buffer
    }
    Salem likes this.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    #1
    Code:
    int numAnswers = 0;
    ...
    int missedQuestions[numAnswers];
    int k;
    for(k = 0; k<numAnswers; k++)
    {
        missedQuestions[k] = 0;
    }
    For loop does nothing, array has 0 elements.




    #2
    Code:
    char answers[];
    ...
    scanf("%d %s", &numAnswers, &answers[]);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2012, 10:35 AM
  2. Replies: 11
    Last Post: 09-25-2011, 01:22 AM
  3. Replies: 2
    Last Post: 03-04-2010, 04:19 AM
  4. Replies: 4
    Last Post: 07-06-2006, 03:53 AM
  5. Replies: 12
    Last Post: 03-10-2005, 07:48 PM

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