944,126 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 22507
  • C RSS
Jul 21st, 2005
0

Read comma separated data from a text file

Expand Post »
Hi there,

I am trying to make a piece of code in C that:

1. opens a specified file,
2. Reads the data in the file and separates it into two arrays, based on the position of commas,
3. Converts the strings into doubles and then perfoms some manipulation on them,
4. returns the new values to a second text file.

I can do 1,3 & 4, just can't seem to get point 2 to work. The data I have is in the format:

0.000,1
0.001,2
0.002,4
0.003,1
0.004,5
0.005,3

and I want to end up with two arrays of the form:

a[6] = {0.000,0.001,0.002,0.003,0.004,0.005};
b[6] = {1,2,4,1,5,3};

Any offers?

cheers, Ravenous.
Similar Threads
Reputation Points: 244
Solved Threads: 77
Posting Pro in Training
ravenous is offline Offline
450 posts
since Jul 2005
Jul 21st, 2005
0

Re: Read comma separated data from a text file

looking at the strings library, you could do the following:

  1. char *ptrtocomma;
  2.  
  3. if(ptrtocomma=strchr(Inputstringname, ',')!=NULL)
  4. {
  5. outputarray[i]=*++ptrtocomma;
  6. i++;
  7. }

strchr returns a character pointer to the first occurance of the second argument (a character) in the character array designated by the first argument.
Reputation Points: 22
Solved Threads: 5
Posting Whiz in Training
Drowzee is offline Offline
244 posts
since Jul 2005
Jul 21st, 2005
0

Re: Read comma separated data from a text file

  1. while(fgets(line, sizeof line, file)}
  2. {
  3. if(sscanf(line, "%lf,%d", &a[i], &b[i]) == 2)
  4. {
  5. ++i;
  6. }
  7. }
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jul 21st, 2005
0

Re: Read comma separated data from a text file

That's much nicer... Wow.
Reputation Points: 22
Solved Threads: 5
Posting Whiz in Training
Drowzee is offline Offline
244 posts
since Jul 2005
Apr 12th, 2007
-1

Re: Read comma separated data from a text file

  1. while(fgets(line, sizeof line, file)}
  2. {
  3. if(sscanf(line, "%lf,%d", &a[i], &b[i]) == 2)
  4. {
  5. ++i;
  6. }
  7. }
Hi, how if each row contains large number of elements in 1 row of a large matrix? For example I have a matrix of 100x100 which already stored in a text file, each line contains 100 elements of 1 row of the matrix, seperated by a tab (\t). How could I read the file back into a matrix structure?

Many thanks,
Fantabk
Reputation Points: 9
Solved Threads: 0
Newbie Poster
fantabk is offline Offline
1 posts
since Apr 2007
Apr 13th, 2007
0

Re: Read comma separated data from a text file

Show us what you have and we can help you fix it. But do it in an new thread. It's not nice to hijack someone else's thread.
Moderator
Reputation Points: 3281
Solved Threads: 895
Posting Sage
WaltP is offline Offline
7,748 posts
since May 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: atof ??? how can I change from user?
Next Thread in C Forum Timeline: Help! Help!help! Help!!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC