944,172 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 69471
  • C++ RSS
Apr 28th, 2005
0

How to read end of line?

Expand Post »
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.

Quote ...
#include <iostream>
#include <fstream>
using namespace std;

int main ()
{
ifstream infile("test_file.txt");

while(infile!='\n' && !infile.eof())
cout<<infile;

return 0;
}
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
XxAndyxX is offline Offline
11 posts
since Apr 2005
Apr 28th, 2005
0

Re: How to read end of line?

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)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int main ()
  7. {
  8. string line;
  9. ifstream infile("test_file.txt");
  10. while ( getline(infile, line) )
  11. {
  12. cout << line << endl;
  13. }
  14. return 0;
  15. }
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Apr 28th, 2005
0

Re: How to read end of line?

^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];
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
XxAndyxX is offline Offline
11 posts
since Apr 2005
Apr 28th, 2005
0

Re: How to read end of line?

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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Apr 28th, 2005
0

Re: How to read end of line?

A sample.
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <sstream>
  5. #include <vector>
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. vector < vector < double > > info;
  11. ifstream file("file.txt");
  12. string line;
  13. while ( getline(file, line) )
  14. {
  15. vector < double > data;
  16. double value;
  17. istringstream iss(line);
  18. while (iss >> value)
  19. {
  20. data.push_back(value);
  21. }
  22. info.push_back(data);
  23. }
  24. for ( vector < vector < double > > :: size_type i = 0, size = info.size(); i < size; ++i)
  25. {
  26. cout << "line " << i + 1 << ": ";
  27. for ( vector < double > :: size_type j = 0, length = info[i].size(); j < length; ++j)
  28. {
  29. cout << info[i][j] << " ";
  30. }
  31. cout << endl;
  32. }
  33. return 0;
  34. }
  35.  
  36. /* file.txt
  37. 1 2 3
  38. 4 5 6 7 8 9
  39. 10 11 12 13
  40. 14
  41. 15 16
  42. */
  43.  
  44. /* my output
  45. line 1: 1 2 3
  46. line 2: 4 5 6 7 8 9
  47. line 3: 10 11 12 13
  48. line 4: 14
  49. line 5: 15 16
  50. */
There are probably better/easier ways to go about iterating through the loop, and I'm sure someone will let me know.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Apr 28th, 2005
0

Re: How to read end of line?

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...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
XxAndyxX is offline Offline
11 posts
since Apr 2005
Apr 28th, 2005
0

Re: How to read end of line?

Quote 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++.
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
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Apr 28th, 2005
0

Re: How to read end of line?

Quote 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
Thanks man! I wish I would have known that a couple programs ago, lol.

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
XxAndyxX is offline Offline
11 posts
since Apr 2005
Apr 28th, 2005
0

Re: How to read end of line?

Quote originally posted by XxAndyxX ...
So all istringstream does is make each number on that one line ready to be stored into a variable?
It is used to do things like >> from a string instead of cin or an ifstream.

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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Selectoin SOrt
Next Thread in C++ Forum Timeline: Bus Error when I run my C++ program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC