Source code of myscanf()

This is a discussion on Source code of myscanf() within the C Programming forums, part of the General Programming Boards category; I want to design a function myscanf(*format,...)which can work as scanf() but without using any library function other than getchar().It ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    1

    Unhappy Source code of myscanf()

    I want to design a function myscanf(*format,...)which can work as scanf() but without using any library function other than getchar().It should support string datatype also..Pls someone do it for me.plsssssssssss

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Unfortunately for you, we don't do homework. http://cboard.cprogramming.com/annou...t.php?f=4&a=39

    What we will do is help you once you've made an attempt at a solution. Try the problem. If you're stuck, ask a more specific question.

    One hint: you'll need to use va_start(), va_arg(), va_end(), and va_list from <stdarg.h> to code the variable arguments.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by dwks View Post
    Unfortunately for you, we don't do homework. http://cboard.cprogramming.com/annou...t.php?f=4&a=39

    What we will do is help you once you've made an attempt at a solution. Try the problem. If you're stuck, ask a more specific question.

    One hint: you'll need to use va_start(), va_arg(), va_end(), and va_list from <stdarg.h> to code the variable arguments.
    But ... with no library functions. This is starting to look a bit more complicated!

    I wonder what the real assignment is.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Those are macros.

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    I don't see how you're going to pass n amount of arguments to myscanf() without using a va_list.

    > I want to design a function myscanf(*format,...)which can work as scanf() but without using any library function other than getchar().It should support string datatype also..Pls someone do it for me.plsssssssssss

    You said you want to design it, then you ask someone else to do it for you.

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    Quote Originally Posted by zacs7 View Post
    I don't see how you're going to pass n amount of arguments to myscanf() without using a va_list.
    If you're willing to make some non-portable assumptions (and since when did university instructors care a whit about portability?) it can be done rather simply.

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    Quote Originally Posted by zacs7 View Post
    You said you want to design it, then you ask someone else to do it for you.
    There are various forms of this:

    "I have the idea to try to X..."
    "While eating biscuits and marmalade I suddenly realized that I could X..."
    "I want to do X..."
    "Suddenly the idea of X is quite stimulating..."

    These are all code words for:

    "I have been assigned X and it's due tomorrow."

    People don't seem to understand how unbelievably obvious it is when they are requesting homework help. Asking for help on homework is allowed here -- how hard can it be to just follow the rules? Don't lie about whether it's homework, post your current attempts, ask specific questions, and don't expect us to write code for you.

    That's four rules. How hard can it be to keep track of four rules?

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,522
    I want to design a function myscanf(*format,...)which can work as scanf() but without using any library function other than getchar().
    Your instructor is going to be very unhappy with the result if he insists on such a stupid restriction. You need at least ungetc, so that you have the functionality to put some bytes back into the stream.

    If you expect myscanf to work properly you need to understand that scanf does a lexical read, which basically means it keeps reading until:
    * there is no more format
    * there is no more data
    * or the data doesn't match the format

    The computer can only discern the last case after it has already read too far into the data. A further explanation is here.

    You can still do your homework and get something close to scanf, but be smart and defend your grade.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Now now children, you all know that a little source code example goes a long way:
    Code:
    int myscanf ( const char *fmt, ... )
    {
      va_list args;
      int rv;
    
      va_start ( args, fmt );
      rv = scan_stream ( stdin, fmt, args );
      va_end ( args );
    
      return rv;
    }
    Now all you need to do is write scan_stream, and you're done! It's not too long either, my toy version is only about 600 lines from start to finish[1].

    [1] But that's without wide character support or C99 conformance
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do you call another source code file?
    By nifear4 in forum C Programming
    Replies: 2
    Last Post: 10-28-2008, 12:16 PM
  2. 'Type' Error on Build of Officially Released Source Code
    By Jedi_Mediator in forum C++ Programming
    Replies: 5
    Last Post: 07-07-2008, 05:28 PM
  3. DxEngine source code
    By Sang-drax in forum Game Programming
    Replies: 5
    Last Post: 06-26-2003, 05:50 PM
  4. Lines from Unix's source code have been copied into the heart of Linux????
    By zahid in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 05-19-2003, 03:50 PM
  5. Source Code Beautifier
    By Hammer in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 05-05-2002, 09:21 PM

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