| | |
How to read end of line?
![]() |
•
•
Join Date: Apr 2005
Posts: 11
Reputation:
Solved Threads: 0
I'm wanting the program to read a line out of file. I wish c++ had .eoln()!
I get: test.cpp:10: no match for `std::ifstream& != char' operator
obviously because I'm comparing the wrong data types.
Is there a function in c++, like in pascal, so you can detect the end of a line? I know in pascal it would be like infile.eoln(), but that doesn't work in c++.
Any help will be appreciated,
Thanks
Andy
I get: test.cpp:10: no match for `std::ifstream& != char' operator
obviously because I'm comparing the wrong data types.
•
•
•
•
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
ifstream infile("test_file.txt");
while(infile!='\n' && !infile.eof())
cout<<infile;
return 0;
}
Any help will be appreciated,
Thanks
Andy
If you're trying to read from a file, use some function that will read from a file. Sitting at the beginning of a file and waiting for the end to come will not do much.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <string> using namespace std; int main () { string line; ifstream infile("test_file.txt"); while ( getline(infile, line) ) { cout << line << endl; } return 0; }
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Apr 2005
Posts: 11
Reputation:
Solved Threads: 0
^yeah but what if the line can't be treated as a string?
On the line, I am wanting to get, there is going to be 1 to 5 double's. Since I don't know how many doubles are going to be on that line, I just need to grab the whole line.
Then I was going to fill an array with how ever many double's were on that line:
double arr[5];
int i;
for (i=0; not end of the line; ++i)
{infile>>arr[i];
}
On the line, I am wanting to get, there is going to be 1 to 5 double's. Since I don't know how many doubles are going to be on that line, I just need to grab the whole line.
Then I was going to fill an array with how ever many double's were on that line:
double arr[5];
int i;
for (i=0; not end of the line; ++i)
{infile>>arr[i];
}
I'd go with a vector of a vector of doubles. The vector of doubles per line, and the vector of these vectors for each line.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
A sample. There are probably better/easier ways to go about iterating through the loop, and I'm sure someone will let me know.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <string> #include <sstream> #include <vector> using namespace std; int main() { vector < vector < double > > info; ifstream file("file.txt"); string line; while ( getline(file, line) ) { vector < double > data; double value; istringstream iss(line); while (iss >> value) { data.push_back(value); } info.push_back(data); } for ( vector < vector < double > > :: size_type i = 0, size = info.size(); i < size; ++i) { cout << "line " << i + 1 << ": "; for ( vector < double > :: size_type j = 0, length = info[i].size(); j < length; ++j) { cout << info[i][j] << " "; } cout << endl; } return 0; } /* file.txt 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 */ /* my output line 1: 1 2 3 line 2: 4 5 6 7 8 9 line 3: 10 11 12 13 line 4: 14 line 5: 15 16 */
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Apr 2005
Posts: 11
Reputation:
Solved Threads: 0
Okay, sorry, I'm still learning about all this stuff and I thought if you knew how many items were going to be used, then use an array?
So you think I should do something like this...
double num;
vector<double> charges[0];
while (not end of the line)
{ file>>num;
charges.push_back(num);
}
Am I on the right track, or am I way off?
[EDIT] ...reading the code you posted...
So you think I should do something like this...
double num;
vector<double> charges[0];
while (not end of the line)
{ file>>num;
charges.push_back(num);
}
Am I on the right track, or am I way off?
[EDIT] ...reading the code you posted...
•
•
•
•
Originally Posted by XxAndyxX
Is there a function in c++, like in pascal, so you can detect the end of a line? I know in pascal it would be like infile.eoln(), but that doesn't work in c++.
http://www.eskimo.com/~scs/C-faq/q12.2.html
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Apr 2005
Posts: 11
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Dave Sinkula
By the way, since we're posting at similar times, I thought I'd bring this up FWIW:
http://www.eskimo.com/~scs/C-faq/q12.2.html
In my CS class, we haven't learned istringstream yet, so I don't know how I would have known how to do that.
So all istringstream does is make each number on that one line ready to be stored into a variable?
I appreciate your help.
•
•
•
•
Originally Posted by XxAndyxX
So all istringstream does is make each number on that one line ready to be stored into a variable?
So the code I posted reads the whole line into a string, rather than going one value at a time (because both a newline and a space are whitespace characters, it might be tougher to distinguish using other means). Then it parses each double via the stringstream and >> operator.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Selectoin SOrt
- Next Thread: Bus Error when I run my C++ program
| Thread Tools | Search this Thread |
action api array auto based beginner binary bitmap c++ c/c++ calculator challenge char class classes code coding compile console conversion count createcopyofanyfileinc delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game garbage givemetehcodez graph gui hmenu homeworkhelp homeworkhelper iamthwee ifstream input insert int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node noob output parameter pointer primenumbersinrange problem program programming project python random read recursion reference rpg sockets string strings temperature template test text text-file tree url variable vector video win32 windows winsock wordfrequency wxwidgets






