Help with finding # of rows.

How do you read in just the quantity of rows from the first column of a .txt file?
I got the # of columns correctly but idk how to get the number of rows correctly
For an example....

SeedPattern.txt:
00000
00000
00000
00000

row = 4
col = 5

I need the number of rows to correctly make my 2D array.
Last edited on
you can't really do it efficiently. Youll have to read all the rows one by one and count them unless you know something about the file. If the file is fixed width, you can get its size and compute it. You can also just build your data structure to not care ... dynamic like.

basically
something like this (this is crude but its the general IDEA)

while(! filevar.eof())
{
filevar.getline(throwaway);
counter++;
}

So for an example if there was XX rows of a .txt file you're tellin me i have to do getline XX times and make XX string variables to go along with them?
no.
you don;t need xx variables, you can read it into the same one each time and discard it.

the rest of your statement is correct, unless you have additional information.

if you are allowed to change the file format, why not make row 1 have rows, columns ahead of the data?
Last edited on
You have to open the file and count the number of lines, you can then close it and open for reading if you like.

What your saying is you want to add the lines to a array but you don't know it's size ahead of time. That is what a vector is used for. It's like an array with variable size.
Topic archived. No new replies allowed.