0

I have a file which has several lines and each lines have unknown integer values which are separated by space characters. I want to assign each line's integers to different lines of an array. I tried to find some example code but there was no.

example input file is:

1 23 4
44 12 
8 10 2 
66 3 22 5
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    what do you mean by `lines of an array`? – taocp May 18 '13 at 21:05
  • and your own efforts yet were? – Solkar May 18 '13 at 21:08
  • i will read values from my file which is input.txt i want to read these values line by line , numbers in first line 1 , 23 , 4 . finally my array will be like this; array[0][0]=1 , array[0][1]=23 , array[0][2]= 4 . array[1][0]=44 , array[1][1]=12 etc.. – pythonic May 18 '13 at 21:13
  • 1
    This has be answered many, many times here on SO. (E.g. [here](http://stackoverflow.com/a/8116843/596781).) – Kerrek SB May 18 '13 at 21:13
  • Welcome to stackoverflow. Please remember to search for the answer to your question in previously asked questions before you post. It makes for a better community. thanks – fontno May 18 '13 at 21:23
  • [How do I tokenize a string in C++?](http://stackoverflow.com/questions/53849/how-do-i-tokenize-a-string-in-c) – pr. May 18 '13 at 21:25
  • One issue you'll need to consider is how you will know how many values are in a particular line in the array — especially if the lines can contain zeros. Also, are you looking for a fixed size array or dynamically allocated array (which is spelled `vector< vector >` in C++). You'll need to read lines and then parse them; other techniques will ignore line boundaries, in general. – Jonathan Leffler May 19 '13 at 02:25

1 Answers1

0

Maybe you mean to push the values to an array of arrays.

If you know the number/size of vales per line, per file just create it statically (or allocate it if not).

Then read the values and push them into the array of array.

Just like here: How do I work with dynamic multi-dimensional arrays in C?

Community
  • 1
  • 1
jalone
  • 1,953
  • 4
  • 27
  • 46
  • Im getting this error : expected unqualified-id before ‘while’ is there an easier code example , I just dont know how i exactly should search for it – pythonic May 18 '13 at 21:42
  • wasn't this for plain C? well then in c++ it's even easier if you can use STL vector. just create a vector and for each line create a new vector, and for each value of the line push that value in the vector. – jalone May 18 '13 at 21:48