Thread: best way to read a string

  1. #1
    zsaniK Kinasz's Avatar
    Join Date
    Jan 2003
    Posts
    222

    best way to read a string

    ok before you jump to give me a link to the faq's, check this out...

    Reading a string from a file, have to stop when i get to a certain delimiter, can contain white space in string. What I have is this....

    Code:
     
     	int ch;
       	int index = 0;
       
       	while (( ch = fgetc( fp )) != delimeter && ch != '\n' && ch != EOF ) {
       		*(buffer + index) = (char)ch;
       
       		index++;
       		if ( index == max_buffer ) {
       			break;
       		}
       	}
    What i want is to do it with fscanf but i cant work out how to get it to keep reading past the whitespace and stop at the delimiter.

    Code:
    ????  fscanf( fp, "%s,", string );  //doesnt skip white space
    Any help would be great.



    Edit: fixed up code sample
    Last edited by Kinasz; 08-17-2004 at 09:13 PM.
    "Assumptions are the mother of all **** ups!"

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Have you looked into the %[ specifier?
    Code:
    /* file.txt
    line.1 line_2 line~3
    */
    
    #include <stdio.h>
    
    void foo(const char *filename, int delimiter)
    {
       FILE *file = fopen(filename, "r");
       if ( file )
       {
          char format[10], result[32];
          sprintf(format, "%%%d[^%c]", (int)(sizeof result - 1), delimiter);
          printf("format = \"%s\"\n", format);
          if ( fscanf(file, format, result) == 1 )
          {
             printf("result>>\n%s\n<<\n", result);
          }
       }
       else
       {
          perror(filename);
       }
    }
    
    int main (void)
    {
       foo("file.txt", '_');
       return 0;
    }
    
    /* my output
    format = "%31[^_]"
    result>>
    line.1 line
    <<
    */
    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.*

  3. #3
    zsaniK Kinasz's Avatar
    Join Date
    Jan 2003
    Posts
    222
    will do, thankyou
    "Assumptions are the mother of all **** ups!"

  4. #4
    zsaniK Kinasz's Avatar
    Join Date
    Jan 2003
    Posts
    222
    okay baffled! Based on the man page i figured this should work...

    Code:
     #define MAX_TK1 10
     void test_scanf( void )
     {
         int i;
         char format[BUFSIZ];
         char buffer[] = "s3328,24";
         char token1[MAX_TK1];
         int token2;
     
     
         sprintf( format, "%%%d[^,]s%%*c%%d", MAX_TK1 - 1 );
         printf( "format: `%s'", format );
     
         sscanf( buffer, format, token1, &token2 );
     
         printf( "test: %s %d\n", token1, token2 );
     
         return;
     }
    but it only copies the first token but not the second. I just cant figure out the correct syntax!
    "Assumptions are the mother of all **** ups!"

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    sprintf( format, "%%%d[^,]s%%*c%%d", MAX_TK1 - 1 );
    Remove the s.
    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.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Replies: 1
    Last Post: 05-30-2003, 02:31 AM
  4. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM
  5. read records fron file into a binary tree
    By Kirsten in forum C Programming
    Replies: 1
    Last Post: 04-23-2002, 02:48 PM