There are some data files look like
1.) data1.txt
----------------
1.0 2.0 3.0
4.0 5.0 6.0
7.0 8.0 9.0
10.0
2.) data2.txt
---------------------
1.0 2.0 3.0 4.0
5.0 6.0 7.0 8.0
9.0 10.0
3.) data3.txt
----------------
1.0 2.0
3.0 4.0 5.0
6.0 7.0
8.0 9.0 10.0
They will be store in a array. A[0]=1.0,A[1]=2.0,.....
I never know the format in each file.
In fortran, I just declare an array A(10), and read them
READ(file_no,*)A
I don't know how to do the same thing in C.
I need your help. Thanks!
Dennis 3 4375
Wei-Chao Hsu <d8******@mail.ntust.edu.tw> wrote: There are some data files look like
1.) data1.txt ---------------- 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
2.) data2.txt --------------------- 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
3.) data3.txt ---------------- 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
They will be store in a array. A[0]=1.0,A[1]=2.0,..... I never know the format in each file. In fortran, I just declare an array A(10), and read them READ(file_no,*)A I don't know how to do the same thing in C.
Without any error checking (which you definitely should do) and
assuming that the lines of dashes don't belong to the files:
int i;
double A[ 10 ];
FILE *fp = fopen( "data1.txt", "r" );
for ( i = 0; i < 10; i++ )
fscanf( fp, "%lf", A + i );
Hint: scanf() skips white space in most cases (that includes the end
of line, i.e. '\n').
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Wei-Chao Hsu wrote: There are some data files look like
1.) data1.txt ---------------- 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
2.) data2.txt --------------------- 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
3.) data3.txt ---------------- 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
They will be store in a array. A[0]=1.0,A[1]=2.0,..... I never know the format in each file. In fortran, I just declare an array A(10), and read them READ(file_no,*)A I don't know how to do the same thing in C. I need your help. Thanks!
If the data is as shown in the above examples, all floating
point numbers, you can use function fscanf to read the file.
To declare an array:
double array[100];
where the number is large enough to hold the values from the file.
Declare a variable to hold a count of the data read from the file.
unsigned count;
To open the file for reading, you use function fopen.
FILE *fp = fopen("test.txt","r");
Use function fscanf in a loop to read the file and store the file
contents in the array.
if(fp)
for(count = 0; count < 100 &&
1 == fscanf(fp,"%lf",&array[count]); count++) ;
then close the file.
fclose(fp);
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email) http://www.geocities.com/abowers822/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: sea |
last post by:
I have text files in the following format:
123,34,
,345,890,
123,23
..
..
..
As you can see, the problem is that (1) the commas can occur in
|
by: 2mc |
last post by:
Generally speaking, if one had a list (from regular Python) and an
array (from Numerical Python) that contained the same number of
elements, would a While loop or a For loop process them at the...
|
by: Sebastian Krause |
last post by:
Hello,
I tried to read in some large ascii files (200MB-2GB) in Python using
scipy.io.read_array, but it did not work as I expected. The whole idea
was to find a fast Python routine to read in...
|
by: Mark |
last post by:
How do I output formatted numerical data to an external (text) file. At
a minimum I want to specify the number of decimal places to print.
Ideally I'd like the type of control the fortran FORMAT...
|
by: Blankdraw |
last post by:
....
somewhere, a newbie is dying ...
Is there anybody out there who can help me get the right input
for the following segment? I am trying to read entire records
of 5 (2-digit) integers at a...
|
by: Pol Bawin |
last post by:
Hi All,
Did somebody already define attributes for numerical properties to define
value : minima, maxima, a number of decimal, ...? This information would be
useful to unify syntax
Polo
|
by: mhodkin |
last post by:
What is the best way to fill an array (or other container) with values
from a text file containing numbers in a simple column, especially when
one doesn't know how many values the text file list...
|
by: acacia314 |
last post by:
I'm attempting to read a data file into a array of structures. The data comes like this
Jane Doe 5 245-44-8899 September 15, 2005
Number of entries in the file is unknown.
My segment to...
|
by: justin |
last post by:
Hi ,
I have the following code which reads the first line of the Excel file
and comes out of the loop.
The test.xls file conmatains two columns in which first column has
numerical data and...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
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...
| |