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 4403
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***********@p hysik.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******@myrapi dsys.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 same
speed? Or, would the array process faster?
I'm new to Python, so my question may expose my ignorance. I
appreciate anyone's effort to help me understand.
Thanks. It is much appreciated.
|
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 arbitrary ascii files, to
replace Yorick (which I use right now and which is really fast, but not
as general as Python). The problem with scipy.io.read_array was, that it
is really slow, returns errors when trying to process large files and it...
|
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 statement
offers. I'm sure this is easy, but I can't seem to find the answer!
I want an output file to look like this:
0.1 0.8575
0.2 12.0900
0.4 345.0000
|
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 time. It would be best to read the
integers into their own 5 respective variables.
I thought I had it. I've redone my program so many different ways
I am coming to the same conclusion I did when I tried to do it
several years ago: that it cannot...
| |
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 contains?
|
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 read the file is as follows:
while( (fscanf(inf, "%s %s %d %s %s %d, %d", arr.fName, arr.lName, arr.gradeLvl, month, arr.date.day,
|
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 the other column has utf-8 encoded data(in
Japanese)
Can anyone please correct me where is the problem?
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |