views:

2764

answers:

5

While reading a file (ifstream), is there any way to direct it to make a new line?

For instance, I would like for THIS to happen:

myfile>>array[1]>>array[2]>>endl;

Obviously, the "endl" just isn't allowed. Is there another way to do this?

Edit---thanks for the quick responses guys!

From a text file, I'm trying to store two strings from that file into arrays and then do the same with the next line (or until I desire, using a for loop)

Using strings is important to me as it will make my future program a lot more flexible.

+2  A: 

Read your two items, then call myfile.ignore(8192, '\n')

Zan Lynx
Thank you very much Zan, this is exactly what I was looking for. Where exactly did the 8192 come from though?
@Vinny: 2 x 4096; it is likely a maximum number of characters to read if no newline is found first. But given the hint - there's a standard function - you can do the RTFM as well, can't you?
Jonathan Leffler
I guess it is just 'big enough for a line'.
David Rodríguez - dribeas
8192 is 2 * 4096, and 4096 is a memory page on x86 (it's also 4 KB).
Max Lybbert
@dribeas: making assumptions like that will mysteriously break your code one day.
Martin York
@yahoo person: So what. Not sure how this relates to strings.
Martin York
8192 is just a large number that is going to be bigger than a line. I could use max<int> or whatever, but 8192 is easier.
Zan Lynx
bad style. never use magic numbers like that.
Johannes Schaub - litb
So? Do I actually want to read 4 billion bytes if it happened to be a binary file without a '\n' character? No. In situations like this I feel its ridiculous to define a constant for max_expected_characters_in_line.
Zan Lynx
+4  A: 

Several options:

You can use ignore.

myfile >> array[1] >> array[2];
myfile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

Or you can read each line into as string stream

std::string line;
std::getline(myfile,line);
std::stringstream  stream(line);

stream >> array[1] >> array[2];

Please note: Array indexing starts at 0.

Martin York
`std::line` ???
shoosh
@shoosh: Ops missed a word
Martin York
+3  A: 

Use std::getline to read a line into a memory stream, then get the two strings from that.

while (cin)
{
  string line;
  getline(cin, line);

  stringstream stream;
  stream << line;

  stream >> array[1]>>array[2];
}
1800 INFORMATION
+1  A: 

I have no idea what this question means. Here's a simple way to read all the lines of a file into a vector of strings. It might be easier to do what you want to do if you do this first.

std::vector<std::string> lines;

std::string line;
while (std::getline(myFile, line))
    lines.push_back(line);

Now you can say lines[4] to get the fifth line, or lines.size() to find out how many lines there were.

Daniel Earwicker
A: 

This should work:

stringstream stream;
string sLine;
int iLine;

while (cin)
{
  getline(cin, sLine);

  stream << sLine;
  stream >> data[iLine][0] >> data[iLine][1];
}

Customized version of an earlier answer.

TomWij
Unfortunately this will not work. You keep appending each line to stream without re-setting it to empty. You should declare stream locally inside the loop and it will work.
Martin York