How to read end of line?

Reply

Join Date: Apr 2005
Posts: 11
Reputation: XxAndyxX is an unknown quantity at this point 
Solved Threads: 0
XxAndyxX XxAndyxX is offline Offline
Newbie Poster

How to read end of line?

 
0
  #1
Apr 28th, 2005
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.

#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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,309
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 228
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: How to read end of line?

 
0
  #2
Apr 28th, 2005
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.
  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. }
"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
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 11
Reputation: XxAndyxX is an unknown quantity at this point 
Solved Threads: 0
XxAndyxX XxAndyxX is offline Offline
Newbie Poster

Re: How to read end of line?

 
0
  #3
Apr 28th, 2005
^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];
}
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,309
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 228
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: How to read end of line?

 
0
  #4
Apr 28th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,309
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 228
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: How to read end of line?

 
0
  #5
Apr 28th, 2005
A sample.
  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.
"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
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 11
Reputation: XxAndyxX is an unknown quantity at this point 
Solved Threads: 0
XxAndyxX XxAndyxX is offline Offline
Newbie Poster

Re: How to read end of line?

 
0
  #6
Apr 28th, 2005
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...
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,309
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 228
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: How to read end of line?

 
0
  #7
Apr 28th, 2005
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
"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
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 11
Reputation: XxAndyxX is an unknown quantity at this point 
Solved Threads: 0
XxAndyxX XxAndyxX is offline Offline
Newbie Poster

Re: How to read end of line?

 
0
  #8
Apr 28th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,309
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 228
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: How to read end of line?

 
0
  #9
Apr 28th, 2005
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.
"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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC