| | |
Read comma separated data from a text file
![]() |
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.
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.
•
•
Join Date: Jul 2005
Posts: 244
Reputation:
Solved Threads: 5
looking at the strings library, you could do the following:
strchr returns a character pointer to the first occurance of the second argument (a character) in the character array designated by the first argument.
C Syntax (Toggle Plain Text)
char *ptrtocomma; if(ptrtocomma=strchr(Inputstringname, ',')!=NULL) { outputarray[i]=*++ptrtocomma; i++; }
strchr returns a character pointer to the first occurance of the second argument (a character) in the character array designated by the first argument.
C Syntax (Toggle Plain Text)
while(fgets(line, sizeof line, file)} { if(sscanf(line, "%lf,%d", &a[i], &b[i]) == 2) { ++i; } }
"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
•
•
Join Date: Apr 2007
Posts: 1
Reputation:
Solved Threads: 0
•
•
•
•
C Syntax (Toggle Plain Text)
while(fgets(line, sizeof line, file)} { if(sscanf(line, "%lf,%d", &a[i], &b[i]) == 2) { ++i; } }
Many thanks,
Fantabk
![]() |
Similar Threads
- writing students data to a text file (Assembly)
- Help Please, how do i read from text file into array? (Visual Basic 4 / 5 / 6)
- How to select data frm text file based on a condition (C)
- conversion of text file into data base file (Visual Basic 4 / 5 / 6)
- Need Help in Reading characters from a text file (C++)
Other Threads in the C Forum
- Previous Thread: atof ??? how can I change from user?
- Next Thread: Help! Help!help! Help!!
| Thread Tools | Search this Thread |
* ansi api array arrays binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile creafecopyofanytypeoffileinc createcopyoffile createprocess() database directory dynamic execv fflush file floatingpointvalidation fork forloop frequency function getlasterror getlogicaldrivestrin givemetehcodez grade graphics gtkgcurlcompiling gtkwinlinux hardware highest histogram homework i/o inches include infiniteloop input intmain() iso keyboard km license linked linkedlist linux list looping loopinsideloop. lowest matrix microsoft mysql oddnumber open opendocumentformat openwebfoundation pdf pointer posix power program programming pyramidusingturboccodes radix read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test threads turboc unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi






