473,398 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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 2850
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

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

Similar topics

2
by: Asbjørn Ulsberg | last post by:
Hi. I'm trying to convert Brady Hegberg's great RTF2HTML VB 6.0 module to C#. I've managed to convert the VB code to VB.NET, which gave me the following code: Option Strict On Option...
3
by: Max Gattringer | last post by:
I have written a little programm, which converts normal Text into Unicode Bytes - nothing special, but i tried to creat 2nd Encoder which converts strings(numbers) in a textBox (strings which i...
4
by: Clark Stevens | last post by:
I have a program that I'm converting from VB6 to VB.NET. It reads in a text file containing barcode numbers and their corresponding descriptions. Then the user enters the barcode number and the...
4
by: sal | last post by:
Greets, All Converting array formula to work with datatables/dataset tia sal I finally completed a formula I was working on, see working code below. I would like to change this code so it...
1
by: UKuser | last post by:
Hi Guys, I have a program which converts Excel spreadsheets to Javascript and allows interactivity. However it can't convert it to PHP, which is obviously better for users to view (in case J/S...
2
by: XML newbie: Urgent pls help! | last post by:
Does anyone have a snippet of code that will convert a string to a long array? I've nearly smashed my head against the wall trying to figure this out. I'm Using vb.net 2005 Pls reply asap. I...
10
by: Ron | last post by:
I want to calculate the surface area of a sphere from an inputed radius with option strict on. I guess I am not converting something correctly. Here is what I am doing: I have a textbox...
0
by: JosAH | last post by:
Greetings, Introduction Before we start designing and implementing our text builder class(es), I'd like to mention a reply by Prometheuzz: he had a Dutch version of the entire bible ...
0
Frinavale
by: Frinavale | last post by:
Convert a Hex number into a decimal number and a decimal number to Hex number This is a very simple script that converts decimal numbers into hex values and hex values into decimal numbers. The...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.