Topic: Help Reading Comma Delim Data
Author: akehurst@tahoma.cwu.edu (Combustion Rulz)
Date: 16 Feb 1995 13:42:34 -0800 Raw View
SteveFarson graced this group with:
: I'm teaching myself C/C++ and I'm now in the wonderful world of file io and arrays. I am successful at reading a simple file like this:
: ABC -10 10 30
: CIA 20 -20 20
: FBI -30 30 10
: But when I put commas between the fields or quotes around the text, my program below goes bananas. I have not found anything
: in my books that address this. Could someone show me how to adapt my program below to work with a text file having commas
: between fields and quotes around text? I would deeply appreciate it. Thanks, Steve
: main()
: {
: FILE *fp;
: char brand[3][30];
: signed int xval[3],yval[3],amp[3];
: int i = 0;
: while (!feof(fp))
: {
: fscanf(fp, "%s %d %d %d\n", brand[i], &xval[i], &yval[i], &[i]);
: printf("\n");
: printf("Brand name: %s\n", brand[i]);
: printf("X Value: %d\n", xval[i]);
: printf("Y Value: %d\n", yval[i]);
: printf("Amplitude: %d\n", amp[i]);
: i = i++;
: }
: fclose(fp);
: return 0;
: }
I would use fgets() to read in a whole line from the text file, commas
and all, and then analyze the character array, char by char to store
input into a multidimensional array like you are doing above.
That is, read in a line, then read the array until you hit a comma, then
store that stuff you have read into an element in your multidim. array.
--
-------------------------------------------------------------------------------
Justin Akehurst - akehurst@tahoma.cwu.edu | "I have some ideas on how we can
http://tahoma.cwu.edu:2000/~akehurst | reform the CS dept. on campus."
aka Combustion Rulz "Fire! Fire! Fire!" | - I know one, lets all use C++!!