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

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.

#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;
}

^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'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.

A sample.

#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
*/

There are probably better/easier ways to go about iterating through the loop, and I'm sure someone will let me know.

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...

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

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.