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.
5 2850
I have had limited success using the atof function, but can only read the first number of each line this way.
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 -
float data[3][4];
-
int i, j;
-
for (i=0; i<3; i++)
-
for(j=0; j<4; j++)
-
scanf("%f", &data[i][j]);
-
-
I think it will be better to use fscanf instead of scanf.
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.
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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 ...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
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...
| |