Counting numbers per row in a txt file

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
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.
closed account (2b5z8vqX)
What exactly do you need help with?
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.
@vlad from moscow: I am trying to find the code to count the number of elements per line
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
Topic archived. No new replies allowed.