Dumb Question: What is sscanf?

This is a discussion on Dumb Question: What is sscanf? within the C Programming forums, part of the General Programming Boards category; Please don't bash me. I really don't understand it. I know how to use sprintf and stuff but sscanf? On ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    71

    Dumb Question: What is sscanf?

    Please don't bash me. I really don't understand it. I know how to use sprintf and stuff but sscanf?

    On a tutorial is says:

    .....sscanf is just like scanf, only it gets the input from a string
    Doesn't ssprint do the same thing?

    Code:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    
    using std::cin;
    
    int main( int argc, char *argv[] )
    {
      char Buffer[255];
      char Name[255];
    
    
      printf("Input your name: ");
      scanf( "%s", Name );
    
      sprintf( Buffer, "Your Name: %s", Name );
      printf("%s", Buffer);
    
    
      cin.get();
      return 0;
    }
    Name: Eric Lesnar
    Learning: C/C++, SDL, WinAPI, OpenGL, and Python
    Compiler: Dev-C++ 4.9.0
    Current Game Project: Acoznict

  2. #2
    Registered User
    Join Date
    Oct 2003
    Posts
    1
    sscanf() it's a function for reading formatted data from a string.
    Read the "man" page, it's easy to understand, you have also an example there

    Some stuff about your code, you're using scanf to input a string from the user, this is not a good idea, try using fgets, but since you're writing in C++ as I see, why dont you use cin?

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161

    Re: Dumb Question: What is sscanf?

    Originally posted by KingZoolerius66
    Please don't bash me. I really don't understand it. I know how to use sprintf and stuff but sscanf?

    Doesn't ssprint do the same thing?
    No. sprintf() takes a bunch of values and creates a string, like
    -- printf() to screen output
    -- sprintf() to a string buffer

    sscanf() takes a string and breaks it up into values, like
    -- scanf() input from keyboard
    -- sscanf() input from a string buffer
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    am I the only one who notices that he is using C++ libraries and using C functions??
    (And posted in C Board)

    Well I guess its alright cuz of CSTDLIB

    but please just do
    Code:
    #include <stdlib.h>
    and if ur going to program in C, stay away from C++ stuff like cin

    Code:
    int sprintf( char *buffer, const char *format [, argument] ... );
    Parameters:

    buffer

    Storage location for output

    format

    Format-control string

    argument

    Optional arguments
    ________________________

    sprintf takes a string buffer and allows one to change formatted text into a string to be placed in that buffer.

    Ex:

    Code:
    char myBuffer[256];
    sprintf(myBuffer,"this buffer is called %s and is %d characters big","myBuffer",256);
    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sscanf and string handling question
    By ursula in forum C Programming
    Replies: 14
    Last Post: 05-30-2009, 02:21 AM
  2. sscanf question..
    By transgalactic2 in forum C Programming
    Replies: 2
    Last Post: 04-22-2009, 01:43 PM
  3. A dumb question from an idiot
    By imanidiot in forum C Programming
    Replies: 7
    Last Post: 04-11-2006, 07:03 PM
  4. a sscanf question....
    By Intelicopter in forum C Programming
    Replies: 2
    Last Post: 04-08-2005, 08:50 PM
  5. Another sscanf() question.
    By caduardo21 in forum C Programming
    Replies: 3
    Last Post: 02-11-2005, 11:35 AM

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