Reading a string from file in quotes..

This is a discussion on Reading a string from file in quotes.. within the C++ Programming forums, part of the General Programming Boards category; Hello, I have 2 strings the one following the other in a text file, liked this: "text1" "text2" Now, I ...

  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,006
    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,006
    >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, 05:27 PM
  2. disposing error
    By dropper166 in forum C# Programming
    Replies: 2
    Last Post: 03-31-2009, 12:53 AM
  3. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 05:30 PM
  4. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 10: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

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