473,407 Members | 2,359 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,407 software developers and data experts.

How to read numercial data from a file

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
Jul 22 '05 #1
2 1079
On Sun, 11 Jul 2004 16:15:53 +0800, 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.
I need your help. Thanks!

Dennis

Well its not so difficult in C++ (I assume you mean C++ not C, since you
posted to a C++ group), You just declare the array and read them. The only
difference is that you must read in some sort of loop.

#include <fstream>

std::ifstream file("data1.txt");
double a[10];
for (int i = 0; i < 10; ++i)
file >> a[i];

If you want a C answer post to news:comp.lang.c

john
Jul 22 '05 #2
John Harrison wrote:
On Sun, 11 Jul 2004 16:15:53 +0800, 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 [snip]
Dennis

Well its not so difficult in C++ (I assume you mean C++ not C, since
you posted to a C++ group), You just declare the array and read them.
The only difference is that you must read in some sort of loop.

#include <fstream>

std::ifstream file("data1.txt");
double a[10];
for (int i = 0; i < 10; ++i)
file >> a[i];

If you want a C answer post to news:comp.lang.c

john


Actually, he did. Perhaps, I didn't pay attention
to the time and date stamps of these posts. ;-)
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Yang Li Ke | last post by:
Hi guys! I have some datas that I must check everytime a visitor comes to my site What is better to do: 1- Read data from a file or 2- Read data from a mysql db Thank you
2
by: Gunnar | last post by:
Hello, I've just written a CPP program that reads integers from a binary file, and used this code while (my_ifstram.read( (char* ) &number, sizeof(int)) { // do something with number } My...
5
by: Pete | last post by:
I having a problem reading all characters from a file. What I'm trying to do is open a file with "for now" a 32bit hex value 0x8FB4902F which I want to && with a mask 0xFF000000 then >> right...
14
by: Mark Broadbent | last post by:
Does anybody know what is (factual please -not just guess) the quickest method to read data from a file? I am not interested in the format of the data (i.e. blocks, bytes, string etc) just that the...
2
by: agphoto | last post by:
There is big or problem in open file in read and write mode.. $file = "data.txt"; $fp = fopen($file,"w+"); $line = fgets($fp,"120"); // i need only 1st line to read and upto 120 bytes echo...
0
by: lovecarole | last post by:
hi, i am the student who should write a program about reading wav file and do the DFT. actually i don't know how to read data of the wav song and save it into the array... if i want to read...
9
by: vineeth | last post by:
Hello all, I have come across a weird problem, I need to determine the amount of bytes read from a file, but couldn't figure it out , My program does this : __ file = open("somefile") data =...
4
by: zl2k | last post by:
hi, there I have a appendable binary file of complex data structure named data.bin created by myself. It is written in the following format: number of Data, Data array Suppose I have...
1
by: Sachin Garg | last post by:
I have a program which opens a fstream in binary input+output mode, creating the file if it doesn't exists. But writing doesn't works after reading, it must be something obvious that I am not aware...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
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...
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
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...

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.