Read from file in c++ till end of line?
Solution 1
Don't use while(!file.eof())
as eof()
will only be set after reading the end of the file. It does not indicate, that the next read will be the end of the file. You can use while(getline(...))
instead and combine with istringstream
to read numbers.
#include <fstream>
#include <sstream>
using namespace std;
// ... ...
ifstream file("file.txt",ios::in);
if (file.good())
{
string str;
while(getline(file, str))
{
istringstream ss(str);
int num;
while(ss >> num)
{
// ... you now get a number ...
}
}
}
You need to read Why is iostream::eof inside a loop condition considered wrong?.
Solution 2
As for reading until the end of the line. there's std::getline
.
You have another problem though, and that is that you loop while (!file.eof())
which will most likely not work as you expect. The reason is that the eofbit
flag is not set until after you try to read from beyond the end of the file. Instead you should do e.g. while (std::getline(...))
.
Solution 3
char eoln(fstream &stream) // C++ code Return End of Line
{
if (stream.eof()) return 1; // True end of file
long curpos; char ch;
curpos = stream.tellp(); // Get current position
stream.get(ch); // Get next char
stream.clear(); // Fix bug in VC 6.0
stream.seekp(curpos); // Return to prev position
if ((int)ch != 10) // if (ch) eq 10
return 0; // False not end of row (line)
else // (if have spaces?)
stream.get(ch); // Go to next row
return 1; // True end of row (line)
} // End function
user3050163
Updated on August 21, 2022Comments
-
user3050163 over 1 year
How can i read data untill end of line?I have a text file "file.txt" with this
1 5 9 2 59 4 6 2 1 2 3 2 30 1 55
I have this code:
ifstream file("file.txt",ios::in); while(!file.eof()) { ....//my functions(1) while(?????)//Here i want to write :while (!end of file) { ...//my functions(2) } }
in my functions(2) i use the data from the lines and it need to be Int ,not char
-
Some programmer dude over 10 years@user3050163 Read into a
std::string
, put in astd::istringstream
, loop using the input operator>>
to get each whitespace-delimited value. -
user3050163 over 10 yearsif i use getline i have to convert from int to char,but i need the data to be int!!!!
-
Some programmer dude over 10 years@user3050163 Or after you read the line and put it in an
std::istringstream
, usestd::copy
withstd::istream_iterator
to put the values directly into a container usingstd::back_inserter
. -
herohuyongtao over 10 years@user3050163 Updated.
-
user3050163 over 10 years(getline(file, str)) is not working error:can;t convert from istream to char
-
user3050163 over 10 yearsError 1 error C2664: 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::getline(_Elem *,std::streamsize)' : cannot convert parameter 1 from 'std::ifstream' to 'char *'
-
herohuyongtao over 10 years@user3050163 It shoud run OK, I just tested it on ideone.
-
user3050163 over 10 years@herohuyongtao it run,but the result is wrong. it mixed the numbers from the three lines
-
herohuyongtao over 10 years@user3050163 Then do something for each
getline()
to split them up. -
mathengineer almost 5 yearsYou can use this to read until the rest of the line: string line;getline(stream, line); . I use it to read the remaining part of the file