Thread: sscanf_ tricky func

  1. #1
    Registered User
    Join Date
    Jan 2003

    sscanf_ tricky func

    I figured it out by trial.
    But I dont understand how it works.

    buff[8] , (sscanf(&buff[0], "%x", &x[i]) reads out the 1. row (x)
    buff[9] , (sscanf(&buff[2], "%x", &x[i]) reads out the 2. row (y)

    int main()
    char buff[9] ;
    int i;
    int x[10];
    FILE *Hex;
    Hex = fopen("hex.txt","r");  //Liest von hex.txt
      while (i<10 && fgets( buff, sizeof(buff), Hex)!= NULL )
        if (sscanf(&buff[2], "%x", &x[i])==1)
           printf("sscanf=%d  i=%d %x\n",sscanf(buff, "%x", &x[i]) ,i,x[i]);
    How does buff size affect the outcome?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Show us the text file

    There's no point making buff unnecessarily small
    char buff[BUFSIZ];
    is the usual default

    > How does buff size affect the outcome?
    Lines longer that sizeof(buff)-1 get split into multiple parts, which result in multiple calls to fgets, and more increments to i than you expect.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems reading formatted input with sscanf
    By Nazgulled in forum C Programming
    Replies: 17
    Last Post: 05-10-2006, 12:46 AM
  2. sscanf - tricky func.
    By overspray in forum C++ Programming
    Replies: 2
    Last Post: 07-19-2004, 06:57 AM
  3. typedef a func
    By trekker in forum C Programming
    Replies: 4
    Last Post: 07-02-2002, 05:15 AM
  4. Delete & print func()
    By spentdome in forum C Programming
    Replies: 5
    Last Post: 05-29-2002, 09:02 AM
  5. sscanf func. used on float ??
    By Gugge in forum C Programming
    Replies: 1
    Last Post: 03-20-2002, 09:59 AM