Thread: Read line by line.

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    97

    Read line by line.

    I have a little problem. I'm writing a plug-in for a program and I have a problem. I need to parse kind of a script, this script is provided to me as
    Code:
    char *EntScript;
    and an example of a script could be
    Code:
    {
    "classname" "directional_audio"
    "angle" "270"
    }
    {
    "classname" "audio_source"
    "origin" "0 0 24"
    }
    Now I don't need the code to parse or anything, I want to write that code myself, the problem is that I don't know how to parse that string line by line. I know that if EntScript was a FILE kind of variable then I would use fgets because it stops as soon as it finds a line break or EOF, whichever comes first. How can I do a similar function that instead of taking a FILE variable uses the variable already defined?
    Thanks.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    97
    Originally posted by Codeplug
    How do I work with file in C

    gg
    That doesn't really help me. I know how to open and write files, but I don't have to. All the info I need is in EntScript, there is no need to read any file, and even if I had to open a file I wouldn't know how since there are no specifications for this format. Also, I don't think putting the contents of EntScript back in a file so I can use fgets would be good, it would be a working solution but just as kind of a hack(one slow hack). Thanks anyway.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Can you clarify your question a little more?

    What is this supposed to be?
    Code:
    {
    "classname" "directional_audio"
    "angle" "270"
    }
    {
    "classname" "audio_source"
    "origin" "0 0 24"
    }
    gg

  5. #5
    root
    Join Date
    Sep 2003
    Posts
    232
    >How can I do a similar function that instead of taking a FILE variable uses the variable already defined?
    Sounds like you want sscanf if the defined variable you're talking about is EntScript...
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    97
    Originally posted by Codeplug
    Can you clarify your question a little more?

    What is this supposed to be?
    Code:
    {
    "classname" "directional_audio"
    "angle" "270"
    }
    {
    "classname" "audio_source"
    "origin" "0 0 24"
    }
    gg
    That's a its just a short example of a script I have to deal with, that's what I have to parse.

    Originally posted by twm
    >How can I do a similar function that instead of taking a FILE variable uses the variable already defined?
    Sounds like you want sscanf if the defined variable you're talking about is EntScript...
    sscanf isn't flexible enough, it would work perfectly on the sample script I showed, but the script could be(and usually is) more complex. I need to be able to read character by character or line by line the contents of EntScript.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Originally posted by Hulag
    sscanf isn't flexible enough, it would work perfectly on the sample script I showed, but the script could be(and usually is) more complex. I need to be able to read character by character or line by line the contents of EntScript.
    Something like this isn't workable?
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       const char EntScript[] =
       "{\n"
       "\"classname\" \"directional_audio\"\n"
       "\"angle\" \"270\"\n"
       "}\n"
       "{\n"
       "\"classname\" \"audio_source\"\n"
       "\"origin\" \"0 0 24\"\n"
       "}";
       const char *ptr = EntScript;
       char text[80];
       int i, line = 0;
       while ( sscanf(ptr, "%79[^\n]%n%*c", text, &i) == 1 )
       {
          ptr += i + 1;
          printf("line %d: %s\n", ++line, text);
       }
       return 0;
    }
    
    /* my output
    line 1: {
    line 2: "classname" "directional_audio"
    line 3: "angle" "270"
    line 4: }
    line 5: {
    line 6: "classname" "audio_source"
    line 7: "origin" "0 0 24"
    line 8: }
    */
    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.*

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
        int n, len;
    
        /* notice the newlines (\n) and escapes for quotes (\") */
        char script[] = 
        {
            "{\n"
            "\"classname\" \"directional_audio\"\n"
            "\"angle\" \"270\"\n"
            "}\n"
            "{\n"
            "\"classname\" \"audio_source\"\n"
            "\"origin\" \"0 0 24\"\n"
            "}\n"
        };
    
        printf("Here is the script\n");
        printf("*********************************\n");
        printf(script);
        printf("*********************************\n\n");
    
        printf("Now here it is one character at a time\n");
        printf("*********************************\n");
        len = strlen(script);
        for (n = 0; n < len; n++)
        {
            printf ("%c", script[n]);
        }
        printf("*********************************\n\n");
    
        return 0;
    }
    gg

    [EDIT]
    Nice. Two examples to look at
    Last edited by Codeplug; 10-31-2003 at 04:26 PM.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > need to be able to read character by character or line by line the contents of EntScript.
    So write your own version of fgets(), which reads from a buffer instead of a FILE

    Heh, make it 3 examples....

    Code:
    #include <stdio.h>
    #include <string.h>
    
    char *EntScript =
    "\{\n"
    "\"classname\" \"directional_audio\"\n"
    "\"angle\" \"270\"\n"
    "}\n"
    "{\n"
    "\"classname\" \"audio_source\"\n"
    "\"origin\" \"0 0 24\"\n"
    "}\n";
    
    char *mySgets ( char *buff, size_t size, char **input ) {
        char    *p = *input;
        if ( *p == '\0' || size == 0 ) {
            return NULL;
        } else {
            char    *q = buff;
            while ( --size > 0 ) {
                char    ch = *p;
                if ( ch == '\0' ) break;    /* end of string */
                *q++ = *p++;                /* copy a char to our line buffer */
                if ( ch == '\n' ) break;    /* end of line */
            }
            *q = '\0';      /* terminate the string in buff */
            *input = p;     /* where we got to */
            return buff;    /* what fgets normally returns on success */
        }
    }
    
    int main ( ) {
        char buff[BUFSIZ];
        char *p = EntScript;
    
        printf( ">>%s<<\n", EntScript );
    
        /* get a line from a string, in the style of fgets() */
        while ( mySgets( buff, BUFSIZ, &p ) != NULL ) {
            printf( "Line = %s", buff );
        }
        return 0;
    }
    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.

  10. #10
    Registered User
    Join Date
    Oct 2003
    Posts
    97
    Thanks for the solutions provided, I will see which one works the best and be back with a response. Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "sorting news" assignment
    By prljavibluzer in forum C Programming
    Replies: 7
    Last Post: 02-06-2008, 06:45 AM
  2. OPen a file and read it from the last line
    By c_geek in forum C Programming
    Replies: 14
    Last Post: 01-26-2008, 06:20 AM
  3. Read each line of text file.
    By Mithoric in forum C++ Programming
    Replies: 1
    Last Post: 06-25-2003, 11:53 AM
  4. how read line by line from textbox
    By lostai in forum Windows Programming
    Replies: 1
    Last Post: 02-14-2003, 08:47 AM
  5. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 06:04 AM