Thread: Cutoff at the Comma

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    32

    Cutoff at the Comma

    This is more of a general question compared to a specific question.

    I'm reading in data from a text file, and the entries are single-worded and separated by commas w/o spaces.
    Let me give an example. Say I have these two words in the document:

    tarantula,the_stig

    along with the appropriate char arrays for the words, titled word1 and word2 respectively.
    When I read in these files via fscanf, I would enter something like this:
    Code:
    fscanf(fp, "%s,%s\n", &word1, &word2);
    I'd then print back the words just to make sure that they were read in correctly, and I'd come up with something like this:

    word1 - tarantula,the_stig
    word2 - he_stig

    Is there anyway I can fix my approach so that the comma can be considered the break-off point instead of a whitespace or other related character?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    The %s modifier for scanf only stops at white space (or EOF or when theres an error). You can use the %[ modifier, so something like
    Code:
    fscanf(fp, "%19[^,],%19s", word1, word2);
    The %[] specifies a group of letters to accept, so %[abc] would only read and store consecutive letters that are 'a', 'b' or 'c' as a string. The first 'd', or '?' or '7' or space, etc would stop it. The ^ at the beginning of the group negates that, so that example is saying "give me at most 19 characters, so long as they are NOT a comma and store it, then read a comma, then at most 19 more chars to store". The second one doesn't need a %[ because there doesn't appear to be a trailing comma at the end of a line.
    Also note, I left off the & from word1 and word2. Presumably they are the names of arrays, or are char pointers that point to valid memory to store the results. Thus the & would be wrong, resulting in pointer to array of char or pointer to pointer to char, respectively, instead of just "pointer to char", which is what %s and %[ expect. Generally speaking, you don't need the & for string variables with scanf. Also note the 19. That is an example of a maximum length specifier, which avoids buffer overflows. 19 would be room for 20 characters, 19 letters plus the null terminator. If your arrays for word1 and word2 are of size 20, then you put a 19 in the scanf. If they're size 100, put a 99, etc. Always leave room for the null.

    Alternatively, you could use strtok or strchr combined with strcpy, but it's a bit more work and probably not worth it here.

    EDIT: The explanation of the "%19[^,]..." bit should read something like this (bold text added for clarity):
    "...then read a comma, then at most 19 more chars, up to the first white space to store"
    Last edited by anduril462; 12-27-2012 at 01:53 PM. Reason: clarification

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    32
    Mad props upon anduril462 for his help and explanation.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    That is a fine post, Anduril.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comma operator
    By ssharish2005 in forum C Programming
    Replies: 2
    Last Post: 10-20-2010, 05:35 AM
  2. comma operator
    By raghuveerd in forum C Programming
    Replies: 5
    Last Post: 07-09-2007, 04:52 AM
  3. Comma Operator
    By doraiashok in forum C Programming
    Replies: 2
    Last Post: 12-09-2003, 08:38 AM
  4. comma
    By hug in forum C# Programming
    Replies: 1
    Last Post: 12-14-2002, 10:48 AM
  5. Replace "." (dot-comma) with "," (regular-comma) for MS Excel
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 03-20-2002, 01:39 AM