I am trying to read a file and place the contents in various arrays. The fields are seperated by commas. I have fields that are strings, doubles, and integers. The string fields may contain characters such as ",&:. The double fields may not all be populated but I need to know that so that the arrays can be filled properly. My input file looks like this:

"",100,1,"IQ at Vin=10V",10.0,17.0,9.3,"mA",10,0.1,,,66,,,,"7","8"
"",100,2,"OVP High Threshold",40.0,48.0,9.3,"V",50.0,,,,,21,,,"11","12"
"",100,3,"OVP Low Threshold",37.0,43.0,9.3,"V",50.0,,,,,,,,"11","12"
"",100,4,"IQ at Vin=40V",7.5,13.0,9.3,"mA",40,,,,,,,,"13","14"
"",100,5,"OVPhigh - OVPlow",2.0,8.0,9.3,"V",20,4.0,,,,4.0,,1.1,"11","12"
"",12,2,"MinPin Buck Eff 15V/3A, CC 4A",85.0,99.000,9.3,"%",,,18.0,15.0,3.7,3.0,11.25,4.0,"16","16"

By using strtok, I can parse out the first 8 fields. I'm lost at how to parse the next 8 fields and fill the arrays correctly when not all of them have data. These would fill in arrays DSV_1, DSV_2, ...DSV_8.

My code so far is below. The progam I am using has functions OpenFile and ReadLine which do exactly what the name says,ie, opens the file and reads the line.

char tempFileName[MAX_PATHNAME_LEN];
int i,k;
char   line_str[256];
int num_bytes;
int file;	
char *word;
char *word2;
int pos;
int TestNmbr[100];
int SubTestNmbr[100];
char DLogDesc[100][100];
double HiLim[100];
double LoLim[100];
double DFormat[100];
char Units[100][10];
double DSV_1[100];
double DSV_2[100];
double DSV_3[100];
double DSV_4[100];
double DSV_5[100];
double DSV_6[100];
double DSV_7[100];
double DSV_8[100];
char LoFBin[100][5];
char HiFBin[100][5];
 if (g_myInifile) 
	file=OpenFile(limit_file, VAL_READ_ONLY, VAL_OPEN_AS_IS, VAL_ASCII);
	while ((num_bytes = ReadLine (file, line_str, 255)) >= 0)
		strcpy(stringValue[i], line_str);
		if(strstr(line_str, "\"\","))
	                                // skip the first pair of ""
			word = strtok(line_str,",");
			word = strtok(NULL,",");
			TestNmbr[k] = atoi(word);
			word = strtok(NULL,",");
			SubTestNmbr[k] = atoi(word);
			word = strtok(NULL,"\"");
			strcpy(DLogDesc[k], word);
			word = strtok(NULL,",");
			LoLim[k] = atof(word);
			word = strtok(NULL,",");
			HiLim[k] = atof(word);
			word = strtok(NULL,",");
			DFormat[k] = atof(word);
			word = strtok(NULL,",");
			strcpy(Units[k], word);

//This is where I'm lost.....			
			word = strtok(NULL,"\"");
//			word = strstr(line_str,",,");
//			word = strspn(NULL,",");
                                                DSV_1[k] = atof(word;


if you're going to use strtok, and all fields are consistent (even if they might be empty), then parse everything based on commas... don't try mixing it up halfway through and parse tokens based on quotation marks (")

then once you've gotten each field in the line as a token, you can strip out quotation marks if needed and convert numeric strings to values if desired -- all based on your rules for whichever field they were found in.

for instance, say each line has 12 comma-separated fields that may or may not have something in them. use strtok to separate each token (using only the comma delimiter) and assign each token to a pointer in an array of pointers.

then use a for(i=0 to 11) loop to index each of the 12 pointers and evaluate/manipulate/whatever each element based on its index (i.e. the field position). a switch/case construct will work nicely.


Edited 6 Years Ago by jephthah: n/a

This article has been dead for over six months. Start a new discussion instead.