Thread: EOF detection

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    4

    Lightbulb EOF detection

    Hey guys,

    This is my first thread here, so I apologize in advance for breaking any conventions . Anyways, I have been having the most horrible time getting this to work and I know that I have done something similar before. So the gist of it is, I have a small peice of code to read a bunch of numbers on a text file, and store it to a dynamicly sized array. But, I am simply unable to get it to stop at the EOF. This peice of code just gets into an infinite loop. If I replace the num[i-1] with i<= 10, it reads the 10 numbers fine. Any help would really really be appretiated. Thanks in advance.

    Code:
             for (i = 1; num[i-2] != EOF; i++)    {
    
                if (i == x-2)    {
                x += 10;
                num = realloc (num, sizeof (int) * x);
                } 
    
                fscanf (infile, "%d\n", &num [i-1]);
                printf ("%d\n", num[i-1]);
            
            }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest an approach like this:
    Code:
    int temp;
    while (fscanf(infile, "%d", &temp) == 1) {
        if (size == capacity) {
            /* increase capacity */
        }
        num[size++] = temp;
    }
    where size is the number of elements in use in the dynamic array and capacity is the number of elements for which space has been allocated.

    Note that for realloc, you should by right assign to another pointer to avoid overwriting your num pointer in case realloc returns NULL. If realloc does not return NULL, then you assign this pointer to num, otherwise you handle the error.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    4
    Thanks, that worked like a charm and thanks for the tips about realloc as well. I sometimes tend to assume that the functions I call will always work!! But out of curiosity, is there any reason the EOF method should not work? I mean is there any error in the code that I came along with. Shouldn't it work and the loop terminate when EOF is hit?

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Well clearly what your thinking isn't right on you can read the EOF. You wont be reading EOF into the num array. fscanf functin will return EOF as soon it hit the EOF. Therefore checking the num array to check if it has EOF is not going to work cleary!

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    4
    Ahhh!!! Thanks, that makes a lot of sense.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. object detection
    By barneygumble742 in forum C Programming
    Replies: 1
    Last Post: 02-22-2010, 06:23 PM
  2. Better key detection?
    By elnerdo in forum C++ Programming
    Replies: 4
    Last Post: 04-23-2005, 07:54 AM
  3. PCI Bus Detection
    By a1a14635 in forum C++ Programming
    Replies: 4
    Last Post: 03-20-2003, 01:44 PM
  4. internet detection
    By penpen420 in forum Windows Programming
    Replies: 4
    Last Post: 10-03-2002, 03:47 PM
  5. key detection
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2001, 09:48 AM

Tags for this Thread