0
int main()
{
    {
        ifstream inputFile;
        // 1. Open the file
        inputFile.open(txt);

        // 2. Do something ( While file is not end of the line and it's opened)

        while (inputFile.is_open() && !inputFile.eof())
        {
            inputFile >> creatureTxt;
            LoadCreature(creatureTxt);
        }

        // 3. Close the file
        inputFile.close();
    }
}

Trying to save onto creatureTxt while emptySpace being counted. However, it seems like it's ignoring the emptySpace. I'm trying to avoid using getline().

5900w
  • 5
  • 4
  • 4
    _I'm trying to avoid using getline()._ Why? – 001 Feb 09 '22 at 18:59
  • just trying to do in different way – 5900w Feb 09 '22 at 19:02
  • 3
    See the I/O manipulator [`std::noskipws`](https://en.cppreference.com/w/cpp/io/manip/skipws) – Thomas Matthews Feb 09 '22 at 19:06
  • Tried usign noskipws but it won't work unless it's char – 5900w Feb 09 '22 at 19:08
  • 2
    Side note: Don't use `eof()` as a loop exit condition unless you know exactly what you are doing and have a good reason to do it. If you don't, [it's almost certainly wrong](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons). – user4581301 Feb 09 '22 at 19:14
  • 1
    Side note: why keep testing if the file is open? Unless you know that `LoadCreature` could close the file (a confusing thing to do in my opinion), all you should need is a single `if`. Note that the whole point is moot if you `while (inputFile >> creatureTxt) { LoadCreature(creatureTxt); }` as suggested by the link in the preceding comment. It will not enter the body of the loop on any iteration if the stream is not in a readable state for any reason. – user4581301 Feb 09 '22 at 19:17
  • 2
    @5900w `operator>>` is literally designed to ignore whitespace. So why are you trying to read whitespace with it? It is not clear what you are actually trying to accomplish, please provide more detail. You would likely benefit from replacing `creatureTxt` with a `class`/`struct` that has its own overloaded `operator>>`. – Remy Lebeau Feb 09 '22 at 19:17

0 Answers0