C++ ifstream not reading \n?
Asked Answered
H

3

13

I'm trying to do operations per-line of a text file, and the way I have it right now, my ifstream object isn't detecting the \n character for each line, which is required for this project. Here's how I have it right now:

std::ifstream instream;
instream >> value;
while (value != '\n')
{
    // do code and such
}

But when I have it run the loop, all I'm getting is a single line of everything in the program. While it is doing exactly what it is supposed to in the loop, I NEED the \n to be recognized. Here's my .txt file:

LXXXVII
cCxiX
MCCCLIV
CXXXLL
MMDCLXXIII
DXLCC
MCdLxxvI
XZ
X
IV

Exactly like that. I cannot change it.

Haughay answered 29/3, 2013 at 15:59 Comment(5)
what data type is value? You might want to use value = cin.get() instead of cin >> value.Sniffy
If you want to read lines, use a function that reads lines. By "\n", what you really mean is an empty line.Jailbreak
value is a char type, and it cannot be an array of any kind, so what would i use instead? and @DavidSchwartz, by \n i really mean the end of the line.Haughay
Read about getline.Azaria
@wbAnon Based on your comment, Ben Voigt's comment should solve your problem.Pacificism
V
25

The >> operator does a "formatted input operation" which means (among other things) it skips whitespace.

To read raw characters one by one without skipping whitespace you need to use an "unformatted input operation" such as istream::get(). Assuming value is of type char, you can read each char with instream.get(value)

When you reach EOF the read will fail, so you can read every character in a loop such as:

while (instream.get(value))
  // process value

However, to read line-by-line you could read into a std::string and use std::getline

std::string line;
while (getline(instream, line))
  // ...

This is an unformatted input operation which reads everything up to a \n into the string, then discards the \n character (so you'd need to manually append a \n after each erad line to reconstruct the original input)

Vinavinaceous answered 29/3, 2013 at 16:5 Comment(10)
alright, i see where you're coming from. so will i get a \n as the value of value?Haughay
When using instream.get(value) yes, assuming value is of type charVinavinaceous
ok, well the code instream.get(value) is not giving value assigning the \n. any idea as to why?Haughay
Why don't you just read the whole line into a std::string with getline? You can parse the string at will afterwards and don't need to fuzz around with \n, as it's stripped from the std::string, as Jonathan explained.Bureau
because i cant use a string for the assignment. at allHaughay
For what assignment specifically?Bureau
This code works great and should produce what you'd like to have: ideone.com/UX6RJw (C++11 due to the auto keyword.) Output: pastie.org/private/umsqfcnyh3fpgjx3obyzwBureau
again, i cannot use a string. only charHaughay
@wbAnon, this works fine for me (using string for convenience): ideone.com/HkEZka If similar code doesn't work for you then either you're doing something wrong or your file has no \n characters. If you created the file on Windows watch out for \r characters in the file too, due to Windows' stupid line endingsVinavinaceous
ya i know... i just got it to work too. not sure why or what i did tho XDHaughay
O
3

You can read your file as follows:

ifstream instream("file.txt");
string line;
while (instream >> line)
{
    cout << line;
    if (instream.peek() == '\n') // detect '\n'
    {
        cout << endl;
    }
}

This way you can track where the line in file ends and detect end of file.

Ownership answered 29/3, 2013 at 16:9 Comment(3)
@wbAnon If this is the case, please see " #12240510" for some informationOwnership
i know how to get it to read the file char by char, the problem is that it refuses to read the \n character at the end of each line.Haughay
@wbAnon try to use fstream::get() instead?Ownership
M
2

I guess you are looking for this: instream.unsetf(std::ios_base::skipws)

Meaningful answered 11/1, 2019 at 13:21 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.