Parsing strings from an input file
I'm trying to parse out a bunch of information from a parameter input file, which I will paste below:
1000,250000,5,0,1, /Users/davis/Desktop/TEST,/Users/davis/Documents/covarion simulation/covarions/pam250.csv,/Users/davis/Documents/covarion simulation/covarions/input.csv,1,3,4,10,0
The 6th, 7th, and 8th fields are filenames which will be passed to functions within my program to read other data in.
Here is my code for reading in each line
The problem that I'm running into is that for the 5th field (paramlist[entry].output_filename), it is reading the following in "/Users/davis/Desktop/TEST,/Users/davis/Documents/covarion" (without quotes), instead of "/Users/davis/Desktop/TEST". i.e. sscanf is not recognizing the comma at the end of /Users/davis/Desktop/TEST in the parameter input file. The second problem I'm having, is that sscanf doesn't like spaces in the strings it is reading in, cause it interprets them as terminators. I know(??) that the solution to this problem lies in properly formatting the %s specifier in the sscanf statement, but I'm not entirely clear on how to do this.
params *parameter_file_input(char filename, int *number_of_entries)
int entries= 0;
int entry= 0;
if ((fp= fopen(filename, "rt"))==NULL)
printf("error in parameter_file_input(%s)\ncould not open file\n", filename);
// count the number of lines to read in
while ((fgets(buf, 1024, fp)!=NULL))
// store the number of entries read in
// rewind back to the start of the file
// instantiate the parameter file database
paramlist= (params*) malloc(entries*sizeof(params));
while (fgets(buf, 1024, fp)!=NULL)
sscanf(buf, "%d, %d, %d, %f, %d, %s, %s, %d, %s, %d, %d, %f, %f", ¶mlist[entry].burn_in, ¶mlist[entry].incompatabilities, ¶mlist[entry].depth, ¶mlist[entry].gamma_shape, ¶mlist[entry].max_trees, paramlist[entry].output_filename, paramlist[entry].tpm_filename, ¶mlist[entry].runs_per_set, paramlist[entry].ssr_filename, ¶mlist[entry].run_type,¶mlist[entry].distribution_type, ¶mlist[entry].distribution_param_one, ¶mlist[entry].distribution_param_two);
It has occurred to me that one possible solution would be to encapsulate the filenames within double quotations. I'm somewhat hesitant to do this, since I'm not the only person who will be using this program- I imagine my boss will want to use it, and forgetting to put quotes around file names is something I believe he is apt to do. Then, when the program doesn't work, he's going to get all cranky.
Another solution, I suppose, would be to use strtok and tokenize the input. I think that'd help me, but I don't know if it is a complete solution, because I'll still have the problem with spaces in the file names.
Suggestions would be appreciated.