473,396 Members | 1,914 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,396 software developers and data experts.

Need help reading file data

Tom
I have a datafile which has a form of

10.10 10.20 10.30 ...
20.10 20.20 20.30 ...
30.10 30.20 30.30 ...
.. . .
.. . .
.. . .

it has 500 rows and 10002 colums.

I tried to read the whole data in a single variable.
My pc shows error. (Windows XP, notebook)

All I need is to read each column data and do some integration.
and so on for every colum.

my naive code is following but, it is too slow.
(it is also limited to read first column, but have to expand to read
every column)

---------------------------------------------------------------------------------------------
FILE *fp;
float b;
int i,j;
float a[500];

fp = fopen("2Dwedge_V1_Ga2.36_st6.28_K1.06_B4.00_e0.20p osi.dat","r");

for(i=0;i<=499;i++)
{
fscanf(fp,"%f",&(a[i]));

for(j=1;j<=10002;j++)
{
fscanf(fp,"%f",&b);
}

Dosomethingwith(float a, 500);
}
---------------------------------------------------------------------------------------------

Sep 30 '07 #1
4 1529
Tom wrote:
I have a datafile which has a form of

10.10 10.20 10.30 ...
20.10 20.20 20.30 ...
30.10 30.20 30.30 ...
. . .
. . .
. . .

it has 500 rows and 10002 colums.

I tried to read the whole data in a single variable.
My pc shows error. (Windows XP, notebook)

All I need is to read each column data and do some integration.
and so on for every colum.

my naive code is following but, it is too slow.
(it is also limited to read first column, but have to expand to read
every column)

---------------------------------------------------------------------------------------------
FILE *fp;
float b;
int i,j;
float a[500];

fp = fopen("2Dwedge_V1_Ga2.36_st6.28_K1.06_B4.00_e0.20p osi.dat","r");

for(i=0;i<=499;i++)
{
fscanf(fp,"%f",&(a[i]));

for(j=1;j<=10002;j++)
{
fscanf(fp,"%f",&b);
}

Dosomethingwith(float a, 500);
}
---------------------------------------------------------------------------------------------
If the data is in strict character columns you may be able to seek to
the exact positions of the file where each datum is. I believe it is
not guaranteed for text files, but usually works.

Another approach is to transpose the original data file before processing.

--
Thad
Oct 1 '07 #2
"Tom" <he*****@gmail.coma écrit dans le message de news:
11**********************@k79g2000hse.googlegroups. com...
>I have a datafile which has a form of

10.10 10.20 10.30 ...
20.10 20.20 20.30 ...
30.10 30.20 30.30 ...
. . .
. . .
. . .

it has 500 rows and 10002 colums.

I tried to read the whole data in a single variable.
My pc shows error. (Windows XP, notebook)
It should not: the amount of data is roughly 5 million values, which can be
loaded in memory as float or double for a total of 20 or 40 megabytes. If
you are running XP, you must have a lot more RAM than that. It could also
be a limitation of the C compiler you are using. Try cygwin and gcc.
All I need is to read each column data and do some integration.
and so on for every colum.

my naive code is following but, it is too slow.
(it is also limited to read first column, but have to expand to read
every column)

---------------------------------------------------------------------------------------------
FILE *fp;
float b;
int i,j;
float a[500];

fp = fopen("2Dwedge_V1_Ga2.36_st6.28_K1.06_B4.00_e0.20p osi.dat","r");

for(i=0;i<=499;i++)
Better use the actual number of elements:

for (i = 0; i < 500; i++)
{
fscanf(fp,"%f",&(a[i]));

for(j=1;j<=10002;j++)
{
fscanf(fp,"%f",&b);
}
try removing this second loop and just scan for the '\n':

{
int c;
while ((c = getc(fp)) != EOF && c != '\n')
continue;
}

This should make the program faster.
>
Dosomethingwith(float a, 500);
}
---------------------------------------------------------------------------------------------
--
Chqrlie.
Oct 1 '07 #3
On Sun, 30 Sep 2007 17:49:44 -0000, Tom <he*****@gmail.comwrote:
>I have a datafile which has a form of

10.10 10.20 10.30 ...
20.10 20.20 20.30 ...
30.10 30.20 30.30 ...
. . .
. . .
. . .

it has 500 rows and 10002 colums.

I tried to read the whole data in a single variable.
My pc shows error. (Windows XP, notebook)
What error? Compile time or run time?
>
All I need is to read each column data and do some integration.
and so on for every colum.

my naive code is following but, it is too slow.
(it is also limited to read first column, but have to expand to read
every column)

---------------------------------------------------------------------------------------------
FILE *fp;
float b;
int i,j;
float a[500];

fp = fopen("2Dwedge_V1_Ga2.36_st6.28_K1.06_B4.00_e0.20p osi.dat","r");
You should check that fopen succeeded.
>
for(i=0;i<=499;i++)
{
fscanf(fp,"%f",&(a[i]));
You should check that fscanf succeeded. You have now read the first
column of a record.
>
for(j=1;j<=10002;j++)
This loop will try to read the next 10002 columns of the record.
Unfortunately, there are only 10001 columns left to be read. At the
end of this loop you will have read the first column of the next
record.

You might want to try using fgets and strtod to insure you never lose
synchronization.
> {
fscanf(fp,"%f",&b);
}

Dosomethingwith(float a, 500);
It seems like you should be using a[i] and b here.
> }
---------------------------------------------------------------------------------------------

Remove del for email
Oct 10 '07 #4
On Oct 1, 1:49 am, Tom <hero...@gmail.comwrote:
I have a datafile which has a form of

10.10 10.20 10.30 ...
20.10 20.20 20.30 ...
30.10 30.20 30.30 ...
. . .
. . .
. . .

it has 500 rows and 10002 colums.

I tried to read the whole data in a single variable.
My pc shows error. (Windows XP, notebook)

All I need is to read each column data and do some integration.
and so on for every colum.

my naive code is following but, it is too slow.
(it is also limited to read first column, but have to expand to read
every column)

---------------------------------------------------------------------------------------------
FILE *fp;
float b;
int i,j;
float a[500];

fp = fopen("2Dwedge_V1_Ga2.36_st6.28_K1.06_B4.00_e0.20p osi.dat","r");

for(i=0;i<=499;i++)
{
fscanf(fp,"%f",&(a[i]));

for(j=1;j<=10002;j++)
{
fscanf(fp,"%f",&b);
}

Dosomethingwith(float a, 500);
}
---------------------------------------------------------------------------------------------
Since only numeric data presents, my suggestion is first convert this
text file into binary, and then seek to right
position as needed.

for example,
position(i, j) = i * 10002 * sizeof(float) + j * sizeof(float)
(i, j) is (row, col) index, starting from 0

Oct 10 '07 #5

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

Similar topics

9
by: Nathan Rose | last post by:
Here's my problem. I am reading from a text file using this: if (!file_exists($file)) { echo "Don't exist\n"; return false; } $fd = @fopen($file, 'r'); if (!is_resource($fd))
0
by: Andy | last post by:
Hi, In the code below (not pretty I know but it's an early version :-P) I'm having problems reading the data object back in. If I move the reading code to immediately after the section where it...
0
by: travis ray | last post by:
Hi, I have an extension in which a file object is created in python and passed down to a c extension which attempts to read from it or write to it. Writing to the file pointer seems to work...
3
by: SB | last post by:
Hello. I have an input file which is laid out in the following manner... Name Day 1 am time 1 am time 2 appointment pm time 1 pm time 2 appointment Day 2
8
by: Darsant | last post by:
I'm currently reading 1-n number of binary files, each with 3 different arrays of floats containing about 10,000 values a piece for a total of about 30,000 values per file. I'm looking for a way...
66
by: genestarwing | last post by:
QUESTION: Write a program that opens and read a text file and records how many times each word occurs in the file. Use a binary search tree modified to store both a word and the number of times it...
8
by: skumar434 | last post by:
i need to store the data from a data base in to structure .............the problem is like this ....suppose there is a data base which stores the sequence no and item type etc ...but i need only...
3
by: rsk | last post by:
Hi Friends, I have the following code which reads the hexadecimal data from the "data.txt" file into the arrays. But when i run this code it is reading some garbage data after reading all the...
6
by: Apollo1376 | last post by:
I am very new to C++. I need some help with loading/reading data from text file and write in an array. I have the following data. 12/31/2004 1213.55 1217.33 1211.65 1211.92 786900000 1211.92...
30
by: carlos123 | last post by:
Ok I am working on a Hall Pass program for my computer programming class. There are 3 things that I am confused on. 1. Reading a file. 2. Taking that data read from the file and putting it into...
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
Oralloy
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,...
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
agi2029
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 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.