[try Beta version]
Not logged in

 
Counting numbers per row in a txt file

Aug 31, 2013 at 10:35pm
I am trying to write a line of code to count the number of elements in a txt file per row, when every row has a different number of elements, and save the data into 4 separate variables. all of the elements are ints.

for example:

the file reads: (all numbers separated by a tab)

15 4 6 7
4 2 42
5 32 6 8 7
28 3 8 9 0 21

The resulting variables would be:
a = 4
b = 3
c = 5
d = 6
Aug 31, 2013 at 10:47pm
And what to do if there are lines of the file with different numbers of elements for example 0, 1, 2, 7, 10, and even 100?
So it is not clear what you are going to do.
Aug 31, 2013 at 10:52pm
closed account (2b5z8vqX)
What exactly do you need help with?
Sep 1, 2013 at 7:33am
Seems like trivial getline-stringstream-istream>>.

What is awkward is the requirement to name variables a, b, c, d, rather than a use of a list, one element per line in input.
Sep 7, 2013 at 8:37pm
@vlad from moscow: I am trying to find the code to count the number of elements per line
Sep 7, 2013 at 8:43pm
You could read in each line whole, then count the number of spaces tabs plus one. That'll give you the number of numbers in the line/row.
Last edited on Sep 7, 2013 at 8:44pm
Topic archived. No new replies allowed.