Reading only numbers from a file

I have a file containing multiple lines. Numbers on each line are separated by ","
Each line is in set notation using "}".
The files look like:
{{1,2,3},{4,5,6},{7,8,9}}
{{10,11,12},{13,14,15},{16,17,18}}
Is there a way to read in only the numbers?

I was thinking of using get() to read the numbers until "," is encountered.
Then use ignore() to discard ",".
But anyway i think of using get() and ignore() will result in get reading a number as well as "}" assuming that i ignore the {{ at the start of the line.
i.e. it would eventually read 3} and then {4 etc.

I want to read the numbers into a 3D array but am stuck at reading only the numbers.
Look at functions
http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/
http://www.cplusplus.com/reference/clibrary/cstdlib/strtol/

Or if you know the input format precisely you may use scanf. For example,
the first you line can be read as
scanf("{{%d,%d,%d},{%d,%d,%d},{%d,%d,%d}}", &arr[0], &arr[1], ..., &arr[8]);

Or you can preliminary replace all non-number characters with spaces and than scanf or cin as usual

1
2
3
4
5
6
7
8
9
10
11
char* str="{{1,2,3},{4,5,6},{7,8,9}}\n{{10,11,12},{13,14,15},{16,17,18}}"
char *p = str;
while(*p)
{
  if(!isdigit(*p)) *p = ' ';
  ++p;
}

//now the string is "  1 2 3   4 5 6   7 8 9   10 11 12   13 14 15   16 17 18 "
sscanf(str, "%d%d%d", &a, &b, &c);
...
Topic archived. No new replies allowed.