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

how to read a sets of data from text file using C?

P: 2
I want to write a program using C to read a sets of data from text file. 5sets of data as shown below. 1st column is label and follow with its value from index1-25...(index : value).

1 1:0.736553 2:0.769987 3:0.736553 4:0.620231 5:0.642820 6:0.575940 7:0.592416 8:0.672809 9:0.526718 10:0.611134 11:0.704128 12:0.728041 13:0.761181 14:0.697038 15:0.733756 16:0.554738 17:1.000000 18:0.766591 19:0.692934 20:0.556762 21:0.033290 22:0.007517 23:0.015536 24:0.000000 25:0.200000
1 1:0.859721 2:0.827131 3:0.807009 4:0.761175 5:0.794402 6:0.660984 7:0.770657 8:1.000000 9:0.961957 10:0.615957 11:0.960490 12:0.792396 13:0.000000 14:0.220034 15:0.160251 16:0.199049 17:0.287420 18:0.226137 19:0.246403 20:0.222603 21:0.095478 22:0.184060 23:0.220383 24:0.231836 25:0.200000
1 1:0.749929 2:0.604483 3:0.463605 4:0.870553 5:0.984389 6:0.935251 7:1.000000 8:0.892586 9:0.818196 10:0.934432 11:0.878320 12:0.933613 13:0.907406 14:0.959820 15:0.981113 16:0.961458 17:0.976595 18:0.852983 19:0.151507 20:0.016379 21:0.000000 22:0.069612 23:0.069136 24:0.046681 25:0.200000
1 1:0.983890 2:0.718258 3:1.000000 4:0.745783 5:0.646945 6:0.589420 7:0.102088 8:0.142395 9:0.199197 10:0.238554 11:0.130723 12:0.312962 13:0.147063 14:0.104578 15:0.131325 16:0.181928 17:0.120763 18:0.159902 19:0.188130 20:0.175762 21:0.088866 22:0.095708 23:0.007229 24:0.000000 25:0.200000
1 1:0.782566 2:0.717626 3:0.890881 4:1.000000 5:0.767509 6:0.676363 7:0.676363 8:0.651185 9:0.792221 10:0.564168 11:0.629532 12:0.592689 13:0.574489 14:0.756820 15:0.791222 16:0.694466 17:0.785784 18:0.666709 19:0.706937 20:0.675760 21:0.034406 22:0.000000 23:0.034406 24:0.000000 25:0.000000

I have done the programming, but it only read 1st line of datasets, which is 1 to 25 not all of them. Please help me to fix this problem...Do i need 2-dimensional array? I try but still didnt work, maybe there is a problem with my program..

A part of my program:

int m[25];
double n[25];
int p,i;
double sum=0;
FILE *fp;

fp = fopen(data, "r");
do{
fscanf(fp,"%d",&p);
for(i=0; i<25; i++)
{
fscanf(fp, "%d:%lf",&m[i],&n[i]);
}
}while(! feof(fp));
Nov 19 '08 #1
Share this Question
Share on Google+
1 Reply


weaknessforcats
Expert Mod 5K+
P: 9,197
No you do not need a two-dimensional array. Read this: http://bytes.com/forum/thread772412.html.

From what I see, I would get individual characters until I fetched a semi-colon (:), then I would fscanf into a double. Then repeat this two-step process until I reached end of file.
Nov 19 '08 #2

Post your reply

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