Read comma separated data from a text file

Reply

Join Date: Jul 2005
Posts: 23
Reputation: ravenous is an unknown quantity at this point 
Solved Threads: 3
ravenous's Avatar
ravenous ravenous is offline Offline
Newbie Poster

Read comma separated data from a text file

 
0
  #1
Jul 21st, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Solved Threads: 5
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: Read comma separated data from a text file

 
0
  #2
Jul 21st, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,335
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Read comma separated data from a text file

 
0
  #3
Jul 21st, 2005
  1. while(fgets(line, sizeof line, file)}
  2. {
  3. if(sscanf(line, "%lf,%d", &a[i], &b[i]) == 2)
  4. {
  5. ++i;
  6. }
  7. }
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Solved Threads: 5
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: Read comma separated data from a text file

 
0
  #4
Jul 21st, 2005
That's much nicer... Wow.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 1
Reputation: fantabk is an unknown quantity at this point 
Solved Threads: 0
fantabk fantabk is offline Offline
Newbie Poster

Re: Read comma separated data from a text file

 
-1
  #5
Apr 12th, 2007
Originally Posted by Dave Sinkula View Post
  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
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Read comma separated data from a text file

 
0
  #6
Apr 13th, 2007
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC