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

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

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
1 1700
weaknessforcats
9,208 Expert Mod 8TB
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

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

Similar topics

13
by: Don Kim | last post by:
I'm writing a program to read 2,000,000 floating point numbers off a text file, to compute the sum, mean, and median. This is a direct example of Stroustrup's paper. But the program will not...
18
by: jas | last post by:
Hi, I would like to start a new process and be able to read/write from/to it. I have tried things like... import subprocess as sp p = sp.Popen("cmd.exe", stdout=sp.PIPE)...
10
by: Tibby | last post by:
I need to read/write not only text files, but binary as well. It seems like on binary files, it doesn't right the last 10% of the file. -- Thanks --- Outgoing mail is certified Virus...
4
by: marcmc | last post by:
Hi, I hope to create a XML file that will hold my Connection data to a SQL Db. I want to write an XML file from 4 Text Box named UserName, Password, Database & Server and later read from it. My...
35
by: RyanS09 | last post by:
Hello- I am trying to write a snippet which will open a text file with an integer on each line. I would like to read the last integer in the file. I am currently using: file = fopen("f.txt",...
8
by: Vijay | last post by:
Hi , I am doing a small project in c. I have a Hexadecimal file and want to convert into ascii value. (i.e., Hexadecimal to Ascii conversion from a file). Could anyone help me? Thanks in...
2
by: Prateek | last post by:
I have a wierd sort of problem. I'm writing a bunch of sets to a file (each element is a fixed length string). I was originally using the built-in sets type but due to a processing issue, I had...
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...
1
by: csgirlie | last post by:
I'm trying to store or arrange three sets of two-dimensional data into three 2xN matrices that are stored as NumPy arrays. import os # for file handling functions import numpy as...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.