Thread: Reading a string from file in quotes..

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    132

    Reading a string from file in quotes..

    Hello,
    I have 2 strings the one following the other in a text file, liked this:
    "text1" "text2"

    Now, I need to read them without the quotes.. I am using sscanf function. I am trying to find the correct formatting, but I had no luck so far, so I thought I would ask here.

    Thanks.
    Y3K Network http://www.y3knetwork.com
    Bringing the software of the future on the net.

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    This is the C++ board, maybe this one is for the C board...

    I would think about reading the file one character at a time.
    checking that character for a quote and then storing the character into a string value at the appropriate position.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    An idea:
    Code:
    /* file.txt
    "text1" "text2"
    */
    
    #include <stdio.h>
    
    int main(void)
    {
       const char filename[] = "file.txt";
       FILE *file = fopen(filename, "r");
       if ( file )
       {
          char buffer[BUFSIZ];
          if ( fgets(buffer, sizeof buffer, file) )
          {
             char s[FILENAME_MAX], t[FILENAME_MAX];
             if ( sscanf(buffer, "\"%[^\"]\" \"%[^\"]\"", s, t) == 2 )
             {
                printf("s = [%s], t = [%s]\n", s, t);
             }
          }
          fclose(file);
       }
       return 0;
    }
    
    /* my output
    s = [text1], t = [text2]
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    132
    Good it worked!! Can you explain to me what all this formatting means? Why does it read the text in the quotes?
    Y3K Network http://www.y3knetwork.com
    Bringing the software of the future on the net.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >Can you explain to me what all this formatting means?
    Code:
    if ( sscanf(buffer, "\"%[^\"]\" \"%[^\"]\"", s, t) == 2 )
    Look for a double-quote.
    Get a string that contains any character that is not a double-quote.
    Look for a double-quote followed by a space and another double-quote.
    Get a string that contains any character that is not a double-quote.
    Look for a double-quote.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    132
    This is great, thanks.
    Y3K Network http://www.y3knetwork.com
    Bringing the software of the future on the net.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. disposing error
    By dropper166 in forum C# Programming
    Replies: 2
    Last Post: 03-30-2009, 11:53 PM
  3. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  4. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM