Ifstream, problems.

This is a discussion on Ifstream, problems. within the C++ Programming forums, part of the General Programming Boards category; I'm using ifstream to scan in integers from a file, but I come into a problem when there is a ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    45

    Ifstream, problems.

    I'm using ifstream to scan in integers from a file, but I come into a problem when there is a character mixed up within the integer list. It will read correctly till it hits the character then loop forever on the integer before. The inFile position woun't move up. How do I fix this problem? This is my code from that section that I am having a problem with. I need to fix it so that it will see that the next value is a char and then it will break out and stop there.
    x is an int, if that makes helps at all.


    Code:
    while (!inFile.eof())
            {
            inFile >> x;
            if(isdigit(x))
                    {
                    tmp[i] = x;
                    i++;
                    }
            }

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    To fix your specific problem, check the stream after you attempt to read in a value and clear the error (and ignore the offending character) before continuing. You should also check for eof() there. The way your code was it would go through the loop an extra time before exiting because eof() only returns true after an attempt to read fails.
    Code:
    while (true)
    {
        if (!inFile >> x)
        {
            if (inFile.eof())
                break;
    
            inFile.clear();
            inFile.ignore();
        }
        else
        {
            if(isdigit(x))
            {
                tmp[i] = x;
                i++;
            }
        }
    }
    Note - code not compiled or tested, but I've gotta go now!

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    45
    I had to make a few changes... to
    Code:
    while (true)
    {
        if (inFile.eof())
            break;
        else if (!inFile >> x)
        {
             break;
        }
        else
        {
            inFile >> x;
            tmp[i] = x;
            i++;
        }
    }
    because on a file that was correct it would loop on the last number. But I am still getting an error when the next value is going to be a char. For somereason (!inFile >> x) will not return false even when it tries to force itself into a int. It just seems to completely skip that part. Any ideas? At the same time I am trying to rewrite it so that it will read in as a char, but I'm running into a problem. How do I tell a string to store it's self as the number value stored within it, not as the ascii value?

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Post more of your code, like the declarations for tmp and x (or the whole thing if it is small enough), and post the data in the input file, or at least enough to cause the error.

    It appears that x is a char, which I didn't notice before, but on a quick examination I can't see why my code would not work. Your code is wrong because you ignore the value in x if (!inFile >> x) part succeeds. Maybe this makes more sense. If the input succeeds, continue, otherwise ignore the character. Of course, if x is a char, then this doesn't make as much sense, but my point is for you to understand that the inFile >> x part loads data into x if it succeeds, but not if it doesn't.
    Code:
    while (true)
    {
        if (inFile >> x) // if the read succeeds
        {
            // process the value in x
            if(isdigit(x))
            {
                tmp[i] = x;
                i++;
            }
        }
        else
        {
            // otherwise it is end of file or some other error.
            if (inFile.eof())
                break;
    
            inFile.clear();
            inFile.ignore();
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 04:48 PM
  2. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM
  3. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 08:19 PM
  4. Read problems
    By xddxogm3 in forum C++ Programming
    Replies: 10
    Last Post: 10-05-2003, 12:17 AM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 03:35 PM

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