Parsing strings from an input file

This is a discussion on Parsing strings from an input file within the C Programming forums, part of the General Programming Boards category; Hi all, I'm trying to parse out a bunch of information from a parameter input file, which I will paste ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    40

    Parsing strings from an input file

    Hi all,

    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

    Code:
    params *parameter_file_input(char filename[], int *number_of_entries)
    {
    	FILE* fp;
    	params *paramlist;
    	char buf[1024];
    	
    	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);
    		exit(-1);
    	}
    	
    	// count the number of lines to read in
    	while ((fgets(buf, 1024, fp)!=NULL))
    		entries++;
    	
    	// store the number of entries read in
    	*number_of_entries= entries;
    	
    	// rewind back to the start of the file
    	rewind(fp);
    	
    	// 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", &paramlist[entry].burn_in, &paramlist[entry].incompatabilities, &paramlist[entry].depth, &paramlist[entry].gamma_shape, &paramlist[entry].max_trees, paramlist[entry].output_filename, paramlist[entry].tpm_filename, &paramlist[entry].runs_per_set, paramlist[entry].ssr_filename, &paramlist[entry].run_type,&paramlist[entry].distribution_type, &paramlist[entry].distribution_param_one, &paramlist[entry].distribution_param_two);
    		paramlist[entry].ssr_specified= 1;
    		entry++;
    	}
    	
    	return paramlist;
    	
    }
    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.


    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.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    Well if we assume that , can't be part of a filename, then replace your %s with %[^,]

    You should also check the return result of sscanf for success.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    40
    Thanks a lot, that helped out immensely.

    Cheers,
    Brad

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 11:04 AM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  4. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 05:14 AM
  5. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 04:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21