Tien Nguyen
Tien Nguyen

Reputation: 51

Reading until just the end of line.

So I'm trying to read in from a file that has an unknown/arbitrary number of characters on a line and unknown/arbitrary number of lines.

I'm trying to make a dynamic 2D array to move all the information into the file to so I can make A Game of Life but I need to first read the file in to get the number of columns and rows.

I already understand how to get the number of rows. I'm just going to read in the file using getline and place a counter on how many times it repeats until EOF.

I'm stuck though on how to get the number of columns. Is there a way for the get function to only read until the end of line?

Upvotes: 0

Views: 88

Answers (1)

Damien Black
Damien Black

Reputation: 5647

Once you've done getline you can call .length() on the string to get how many characters it is. For example:

getline (cin,line);
columns = line.length();

The number of characters you get back should represent the number of columns.

Upvotes: 2

Related Questions