By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,678 Members | 1,146 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,678 IT Pros & Developers. It's quick & easy.

Reading many data files

P: 2
Hello,
I am facing problem writing an algorithm/codes. From a C program, I generated 100 data files, now I have to read those files having three columns each. Using every data of each line of all the 100 files I have to determine the average for all the three variables separately. Problem (again):

(1) Reading 100 data files
(2) There are three variables in every file whose calculated values are written in double format. Utilizing every data of each line, determine average. (in each line of 100 files, total 100 data points for every variable)
or
(3) is there any way to locate and read every data point in a data file.

It might be simple, I donot know. (I agree I'm not good at programming, but I'm trying) . If u donot understand it, plz let me inform.

Is there anybody who can support me regarding this issue (at least one part)? I'll be highly obliged.

Regards

Ahmed
May 2 '07 #1
Share this Question
Share on Google+
3 Replies


Ganon11
Expert 2.5K+
P: 3,652
In order to read from the 100 different files, I'd create a loop (for loop). Inside the loop, I'd make a string for the file name (using the index as part of the name), open that file (if you are using std::string, you'll have to use the .c_str() function), and then process that file.

Now, inside the file, are you trying to find the average of the 3 numbers in each line? e.g.

Expand|Select|Wrap|Line Numbers
  1. FILE:
  2.  
  3. 3.0 5.0 10.0 // average is 6.0
  4. 4.0 4.0 6.0 // average is 2.666...
or the average of all the numbers? e.g.

Expand|Select|Wrap|Line Numbers
  1. FILE:
  2.  
  3. 3.0 5.0 10.0
  4. 4.0 4.0 6.0
  5. // average is 5.333...
May 2 '07 #2

P: 2
Hi,
Many thanks for suggestion. .c_str() is used for string as I know. Is it possible to use fscanf to read those 100 files which are named as peak1.dat to peak100.dat ? and at the same time, calculate average for every point of each line for 100 files?

By the way, dear members, I am not a computer engineer or programmer. I am a researcher on optical communication. For simulation I am using c language.

Ahmed







In order to read from the 100 different files, I'd create a loop (for loop). Inside the loop, I'd make a string for the file name (using the index as part of the name), open that file (if you are using std::string, you'll have to use the .c_str() function), and then process that file.

Now, inside the file, are you trying to find the average of the 3 numbers in each line? e.g.

Expand|Select|Wrap|Line Numbers
  1. FILE:
  2.  
  3. 3.0 5.0 10.0 // average is 6.0
  4. 4.0 4.0 6.0 // average is 2.666...
or the average of all the numbers? e.g.

Expand|Select|Wrap|Line Numbers
  1. FILE:
  2.  
  3. 3.0 5.0 10.0
  4. 4.0 4.0 6.0
  5. // average is 5.333...
May 3 '07 #3

Ganon11
Expert 2.5K+
P: 3,652
Yes, you'll be able to use fscanf to read in the numbers. You can either read them into an array or into 3 separate values and then find the average easily. What else do you have to do? Will you have to print this average value into the file? Do something else?
May 3 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.