468,133 Members | 1,368 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,133 developers. It's quick & easy.

Converting a text string to an array of numbers

3
Hello,

I am trying to convert a textfile made up of strings of numbers separated by spaces, into a 2D array of the (floating point) numbers themselves.

For example the textfile would contain the following:
5 -2 3.3 6
2 1 0 86
9 -3 4 5.8
converted into a float array with dimensions [4][3].

Where [0][0]=5, [1][0]=-2, [3][1]=86, etc.
Nov 23 '06 #1
5 2627
db1145
3
I have had limited success using the atof function, but can only read the first number of each line this way.
Nov 23 '06 #2
horace1
1,510 Expert 1GB
I have had limited success using the atof function, but can only read the first number of each line this way.
try something along the lines of
Expand|Select|Wrap|Line Numbers
  1.     float data[3][4];
  2.     int i, j;
  3.     for (i=0; i<3; i++)
  4.        for(j=0; j<4; j++)
  5.           scanf("%f", &data[i][j]);
  6.  
  7.  
Nov 23 '06 #3
I think it will be better to use fscanf instead of scanf.
Nov 23 '06 #4
db1145
3
It's not quite as simple as that, because each number can be represented by more than 1 character in the text file. Ie. this would work for 1 3 5 2, etc, but not for 3.25 -40 7 12, etc.

I managed it in the end by using the 'space' and 'end of line' characters in my textfile as delimiters, then putting each character of the text strings representing an individual number into its appropriate place in a 3 dimensional char array. I then used the atof() function to parse this text, and put the result into the 2D float array.
Nov 24 '06 #5
horace1
1,510 Expert 1GB
It's not quite as simple as that, because each number can be represented by more than 1 character in the text file. Ie. this would work for 1 3 5 2, etc, but not for 3.25 -40 7 12, etc.

I managed it in the end by using the 'space' and 'end of line' characters in my textfile as delimiters, then putting each character of the text strings representing an individual number into its appropriate place in a 3 dimensional char array. I then used the atof() function to parse this text, and put the result into the 2D float array.
not sure what you mean "because each number can be represented by more than 1 character in the text file"

scanf() (and fscanf()) with the conversion specification "%f" would use whitespace (space characters and newlines) as delimiters so would convert sets of digits seperated by spaces into floats. e.g.
3.25 -40 7 12

would be read as four float values 3.25, -40.0, 7.0 and 12.0
Nov 24 '06 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Asbjørn Ulsberg | last post: by
3 posts views Thread by Max Gattringer | last post: by
1 post views Thread by UKuser | last post: by
2 posts views Thread by XML newbie: Urgent pls help! | last post: by
27 posts views Thread by didacticone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.