Thread: Reading from stdin

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    1

    Reading from stdin

    Hi,

    I am writing a program that reads in from stdin and writes into a string.

    Code:
    int main()
    {
            int file;
            char *sb, c;
    
            while((c = getchar()) != '\n'){
                    printf("Entered in\n");
    
                    if (isalnum(c))
                            *sb++ = c;
    
                    printf(" The String: %s\n" , sb);
            }
    }
    I enter few characters and hit enter expectign that the final printf statement would output a string coming all the characters. But I always get junk printed out. I tried to malloc(10) for *sb and tried to print again. I got blank string. Could someone please point out what is going wrong ? Is it a problem with using getchar() ?

    Please suggest me a way to get series of characters to be copied as a single string to *sb
    -Jeck

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    #1. You need to either malloc memory for sb or point it to some preexisting buffer.
    #2. If you continuously alter/move your sb pointer (*sb++)as you are writing to it, how do you expect to output the data correctly. Every time you try to print the string, your sb pointer is at the end of the data you've copied. You probably want another separate pointer that points to the start of your allocated memory or buffer that you can increment independently of the sb pointer.
    #3. getchar returns an int.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Command line/ stdin
    By erasm in forum C Programming
    Replies: 2
    Last Post: 08-11-2009, 11:55 AM
  2. Checking stdin for input
    By hawaiian robots in forum C Programming
    Replies: 7
    Last Post: 05-19-2009, 09:06 AM
  3. stdin + (ctrl+z), how i detect it?
    By Olimpo in forum C Programming
    Replies: 1
    Last Post: 09-30-2006, 05:33 AM
  4. reading from stdin
    By AngKar in forum C Programming
    Replies: 4
    Last Post: 05-03-2006, 12:14 PM
  5. Array, reading in response etc...
    By mattz in forum C Programming
    Replies: 4
    Last Post: 12-05-2001, 11:41 AM