Like Tree2Likes
  • 1 Post By laserlight
  • 1 Post By ssharish2005

EOF detection

This is a discussion on EOF detection within the C Programming forums, part of the General Programming Boards category; Hey guys, This is my first thread here, so I apologize in advance for breaking any conventions . Anyways, I ...

  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
    21,310
    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.
    rsnair2 likes this.
    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
    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,682
    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
    rsnair2 likes this.
    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, 05: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, 12: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, 08:48 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