Problems reading formatted input with sscanf

This is a discussion on Problems reading formatted input with sscanf within the C Programming forums, part of the General Programming Boards category; Actually, I had to repost. Code: int foo, bar; char string[BUFSIZ], matchme[]="10,4,\"Foo'd up beyond all repair\",h\n"; sscanf(matchme, "%d,%d,%512[^\n]", &foo, &bar, ...

  1. #16
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,740
    Actually, I had to repost.
    Code:
        int foo, bar;
        char string[BUFSIZ], matchme[]="10,4,\"Foo'd up beyond all repair\",h\n";
        sscanf(matchme, "%d,%d,%512[^\n]", &foo, &bar, string);
        printf("%d %d %s\n", foo, bar, string);
    The little asterisk trick doesn't seem to work with brackets, so just use the number that BUFSIZ equals.

    EDIT: OK, did it again. I mistyped the nul character. Sorry. That really does work, if you don't want to read to a flag character instead.
    Last edited by whiteflags; 05-09-2006 at 02:57 PM.

  2. #17
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    You can use an extra variable to read anything past the last
    character then if you don't a return code of 4 there was an error.
    I think thats the simplest way to do it.

    PHP Code:
    #include <stdio.h>
    #include <string.h>



    main(argc,argv)
        
    int  argc;
        
    char *argv[]; 
    {
        
    int numnum2;
    char matchme[]="1,5,\"some text\",h\n";
    char str[40];
    char ch[3], endchars[100];
      if (
    sscanf(matchme"%d,%d,%[^,],%1[hv]%[^\n]", &num

    &
    num2strchendchars ) != 4)
        
          
    printf("Error reading string 1.\n");
      else 
          
    printf("%d %d %s %s\n"numnum2strch);


    }
    /*main */ 

  3. #18
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    > If only there was perl compatible regular expressions included in the standard C.
    Do you have some objection to linking with a library which does the same?
    http://www.pcre.org/


    > 1,5,"some text",h
    Is this also an example (comma in the quotes)?
    1,5,"maybe this, maybe that",h
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 06:27 AM
  2. Reading from an input file + processing
    By Mooncow in forum C Programming
    Replies: 2
    Last Post: 12-01-2008, 01:45 AM
  3. Formatted input
    By vaibhav in forum C++ Programming
    Replies: 1
    Last Post: 10-21-2005, 11:19 PM
  4. reading input files with different types of data
    By sanu in forum C++ Programming
    Replies: 4
    Last Post: 06-27-2002, 08:15 AM
  5. function and input problems
    By meka in forum C++ Programming
    Replies: 4
    Last Post: 09-22-2001, 11:56 AM

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