Thread: fgets && sscanf

  1. #1
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    fgets && sscanf

    code:

    struct Base
    {
    float Tocke;
    struct Base *next;
    };

    // this code is in the function

    float Points1;

    printf("Enter points: ");
    fgets(Points1, strlen(Points1), stdin);
    sscanf(Points1, "%f", (*NEW)->Points);

    Could anyone tell me why this code is not working?
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  2. #2
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    fgest reads a string so you need a tempary character array

    Code:
    char temp[MAX];  /* MAX size of input */
    float Points1;
    
    printf("Enter points: ");
    fgets(temp, sizeof(temp)-1, stdin);
    sscanf(temp, "%f", &Points);
    you also need to read sizeof - 1 as fgets leaves the newline on the string and adds the NULL to the end, so you array needs to have an extra element
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  3. #3
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    This is working. But if I do this with pointers than it is not working. How to do it with pointers?
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  4. #4
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    When you use the structure pointer in the sscanf you have to use the & in front of it.
    Code:
    sscanf(temp, "%f", &struct_PTR->Points);
    That should work, it does for me.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem using sscanf fgets and overflow checking
    By jou00jou in forum C Programming
    Replies: 5
    Last Post: 02-18-2008, 06:42 AM
  2. fgets, sscanf question.....
    By Ash1981 in forum C Programming
    Replies: 12
    Last Post: 01-06-2006, 01:31 PM
  3. Fgets + sscanf
    By MethodMan in forum C Programming
    Replies: 3
    Last Post: 03-15-2004, 08:53 PM
  4. sscanf (I think)
    By RyeDunn in forum C Programming
    Replies: 7
    Last Post: 07-31-2002, 08:46 AM
  5. SSCANF help
    By mattz in forum C Programming
    Replies: 7
    Last Post: 12-10-2001, 04:53 PM