What is wrong in below simple code snippet?

Discussion in 'C++' started by doublemaster007, Dec 4, 2009.

  1. Hi All

    I want to read the keybord till user presses enter (Enter also shud be
    there in the strptr). Below code works incase of user has pressed
    chars upto 1024. Incase of more chars this code doesnt work in HP-UX
    machine....it ignores first 1024 chars and rest of them will be there
    in strptr.

    Does this code depnds on architecture? Why this code doesnt work in HP-
    UX???

    strptr = (char *)malloc(4096)
    tempsstr=strptr
    //strptr shub be memset to 0
    while((*strptr++ = getchar()) != '\n')
    {
    // This loop shud read the chars until enter is pressed
    }
    strptr = mpsstr
     
    doublemaster007, Dec 4, 2009
    #1
    1. Advertisements

  2. I thought every c pgm is also a C++ pgm.!
    Anyways thanks for ur sugg..I may post it there and see. Hope some one
    may not ask me to post it again in Unix Forum..

    One more thing..I had tried C++ way cin.getline(...) Still the output
    was same..
     
    doublemaster007, Dec 4, 2009
    #2
    1. Advertisements

  3. And what happens if the user presses the X key 4096 times?
    I thought C required statements to be terminated by semicolons?
    ;-/

    IOW, this is not compilable code. "doublemaster007" should read the FAQ
    at http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8

    Until he posts the actual code that's causing the problem, not am
    inaccurate paraphrase of it, there is no way of knowing where the
    problem is.
     
    Richard Herring, Dec 4, 2009
    #3
  4. Problem here is..i canot copy paste the code :( Due to restriction
    from my organisation..Anyways i will type simple program here...

    #include <stdio.h>

    int main ()
    {
    char str [80];
    int i;

    printf ("Enter your family name: ");
    scanf ("%s",str);
    printf ("Enter your age: ");
    scanf ("%d",&i);
    printf ("Mr. %s , %d years old.\n",str,i);
    return 0;
    }


    This code doesnt work in HP-UX machine..Code works perfectly in SUSE.
    May be this is OS Specific, But i do not know where to post it..If i
    am posting this on a wrong place..pls guide me..

    Output of above code is: If user enters more than 1024 chars...first
    1024 chars will be truncated..

    Since scanf did not work, i tried all the ways like
    cin.getline
    gets,fgets,fscanf...finally the first posted code, which reads char by
    char...

    Can any one help me?? This is annoying..make me feel like i donno to
    write a basic pgm!!!
     
    doublemaster007, Dec 4, 2009
    #4
  5. In message
    No. In the code above, if the user enters more than 79 characters, you
    have undefined behaviour.

    Your statement above is ambiguous. Do you mean that in your actual code,
    if the user enters more than 1024 characters, only the first 1024 are
    returned ("it is truncated to the first 1024 characters") or the first
    1024 are removed and the rest is returned?
    Me too. What was wrong with Christian Hackl's suggestion for dropping
    the C idioms and using C++ techniques that avoid buffer-overrun
    problems?
     
    Richard Herring, Dec 4, 2009
    #5
  6. Bad idea. Post from outside of your organisation or work on loosening
    the restrictions.
    What do you mean by "doesnt work"? In what *sense* doesn't it work?
    "Perfectly"? The code is *very* dangerous (since 'str' is declared to
    have a very limited size - 80 bytes). This code is a buffer overrun
    waiting to happen.
    You're posting C code in a C++ newsgroup. Consider 'comp.lang.c'.

    Since you mentioned the different behaviour between HP-UX and Linux, it
    *can* be OS-specific, but because you didn't say *what* didn't work on
    HP-UX (and did work on Linux), it is rather impossible to conclude reliably.
    If the user enters 1024 chars when the buffer is only declared to be 80
    bytes long, you're in the *undefined behaviour* land. Anything is
    allowed to happen. Nasal demons. Look 'em up.
    Look up 'std::getline', which reads a 'std::string'.
    Frustrating as it might be, sometimes it's easier to scrap what you have
    and start from the beginning, which should start by *stating your
    requirements*, not writing code.

    Good luck!

    V
     
    Victor Bazarov, Dec 4, 2009
    #6
  7. char str [80]; I had changed to char str [2000]; Still the output is
    truncated
     
    doublemaster007, Dec 4, 2009
    #7
  8. That is up to the compiler. Apparently it has a limitation on how big
    the input can be for a %s format in 'scanf'. You should find something
    in the compiler documentation about that. Lesson: don't use 'scanf'.

    V
     
    Victor Bazarov, Dec 4, 2009
    #8
  9. doublemaster007

    James Kanze Guest

    The problem is more likely in the system: it's not rare for
    various Unixes (and possibly other systems) to have a finite
    input buffer for the console, and to ignore characters which
    don't fit. (Normally, I would expect that the character
    wouldn't echo if it didn't fit, but who knows.)

    Anyway, I'd second the recommendation that he use std::getline
    on an std::string. If he still can't read more than 1000 some
    characters, then he should ask in an appropriate forum for his
    system, since that's probably where the problem lies.
     
    James Kanze, Dec 4, 2009
    #9
  10. Thank u sooooooooo much for the suggetion. I do not know if i can use
    std::string, Will there be any portable issue if i use string??
    because trough out the project stl string is used...
    ddoes STL string has methods to read line??
     
    doublemaster007, Dec 4, 2009
    #10
  11. Hmmm..Even i suspect problem (or may be its because of some valid
    reason) in the system..If there are any UNIX experts here or if any
    one having access to HP-UX machine could help me out?
     
    doublemaster007, Dec 4, 2009
    #11
  12. Isn't there a Unix newsgroup (or two or three) out there? This is,
    after all, a language newsgroup. If your problem is with the platform,
    please post to the platform forum. Or contact HP tech support.

    V
     
    Victor Bazarov, Dec 4, 2009
    #12
  13. doublemaster007

    Ian Collins Guest

    Why don't you just use C++?
     
    Ian Collins, Dec 4, 2009
    #13
  14. doublemaster007

    James Kanze Guest

    [...]
    Not here. For any restrictions in the system, you'd have to ask
    in a system specific newsgroup. (On the other hand, regardless
    of the system, there must be some restrictions. All of the
    usual systems support editing the line you're currently
    inputting, which in turn means some sort of internal buffering
    when reading. And that internal buffer can't be infinite---even
    if it dynamically grows, there must be a limit.)
     
    James Kanze, Dec 7, 2009
    #14
  15. doublemaster007

    James Kanze Guest

    C++ can't provide services that the system doesn't support.
    Neither Windows nor any of the Unix support infinitely long
    lines of input from the keyboard, for example. (Under Unix, he
    can find out the maximum length by using fpathconf with an
    argument of MAX_CANON. Off hand, I don't know how to get it
    from Windows---maybe GetConsoleScreenBufferInfo, but I'm not
    sure of the relationship between the screen buffer and the
    editing buffer.)
     
    James Kanze, Dec 7, 2009
    #15
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.