Reading input text from keyboard in c

Discussion in 'Programming & Software Development' started by shihyuan, Jul 8, 2010.

  1. shihyuan

    shihyuan Member

    Joined:
    Apr 21, 2004
    Messages:
    50
    Location:
    Thornbury 3071 VIC
    Hi, all

    I am going to throw another C issue that I am currently stuck with at you guys and hoping I can get some feedback on my code.

    I have declared an array of char of size 2 and trying to read 2 char input from keyboard into the array using fgets but it only read the first char for whatever reason.

    int main(void)
    {
    char input[2];
    fgets(input, sizeof input,stdin);
    printf("%s\n",input);
    }

    The output I get is only "a". I read couple of text books and did some research in google and found there is nothing wrong in my code that I can see. Could anyone please help me out with this problem. :)

    I just took up C Programming Language so that's why I am having so many questions. I hope everyone can accommodate my noob questions.
     
  2. domlebo

    domlebo Member

    Joined:
    Nov 13, 2004
    Messages:
    7,776
    Location:
    Brisbane
    Noob questions are always welcome, because programming isn't easy. If you haven't got an account of stackoverflow.com do so. It is a GODSEND for questions like this.

    As for your question, remember that C strings are null terminated i.e. if every string ends with a /n character (treated as one char). So to store 10 chars using fgets, you need an array of size 11 ;)

    Also, you need to return an int.
     
    Last edited: Jul 8, 2010
  3. SLATYE

    SLATYE SLATYE, not SLAYTE

    Joined:
    Nov 11, 2002
    Messages:
    26,858
    Location:
    Canberra
    You need to make the array 3 characters long, to leave space for the null character.
     
  4. OP
    OP
    shihyuan

    shihyuan Member

    Joined:
    Apr 21, 2004
    Messages:
    50
    Location:
    Thornbury 3071 VIC
    Thanks for the tips. I have increased the size of the array to 3 instead. Upon reading the input I am getting a different error - Segmentation Fault then the program crashes. Do you have any idea why this error occurs. I googled the error and apparently it means I am trying to access an element outside of array size. Is that true? If that's true then how do I solve it?
     
  5. SLATYE

    SLATYE SLATYE, not SLAYTE

    Joined:
    Nov 11, 2002
    Messages:
    26,858
    Location:
    Canberra
    Segmentation faults are very, very common in C (at least for beginners). You could try putting a printf statement after each of the statements you've got there, to find out where it's having problems. For example:

    Code:
    int main(void)
    {
    char input[3];
    printf("Array created");
    fgets(input, sizeof input,stdin);
    printf("Input read");
    printf("%s\n",input);
    printf("Result printed");
    }
     
  6. domlebo

    domlebo Member

    Joined:
    Nov 13, 2004
    Messages:
    7,776
    Location:
    Brisbane
    Like SLATYE said, segfaults are the bread and butter of a C programmer.

    Your program compiles and runs well for me :S
     
  7. SLATYE

    SLATYE SLATYE, not SLAYTE

    Joined:
    Nov 11, 2002
    Messages:
    26,858
    Location:
    Canberra
    I suppose that it could be causing problems if this system uses chars that aren't 1-byte.

    sizeof <array> returns the number of bytes taken up by that array, not the number of elements that it can store. If he's got a 3-element array that takes up six bytes (ie 2-byte chars, as you'd find in Java) then fgets may be attempting to load six elements into that (12 bytes, which won't fit).

    I don't know of any C systems that do actually use anything other than one byte for a char, but it wouldn't surprise me if one exists.

    shihyuan: try just calling fgets as:
    Code:
    fgets(input, 3,stdin);
    
     
  8. SaTaN

    SaTaN Member

    Joined:
    Jun 18, 2002
    Messages:
    4,790
    Location:
    Caulfield-ish
    a segfault means you are trying to access memory you dont have access to. In C arrays dont have bounds checking so while a segfault could mean you are trying to access past the end its more likely not. segfault 99% of the time means you are trying to dereference a NULL pointer (If that doesnt mean anything to you yet then stick it in the back of your mind.. you *will* have that problem eventually :D )
     
  9. OP
    OP
    shihyuan

    shihyuan Member

    Joined:
    Apr 21, 2004
    Messages:
    50
    Location:
    Thornbury 3071 VIC
    thanks everyone for the useful information, I actually felt like I learnt alot. I have solved the problem now. Thanks again for everyone's help till next time :D
     

Share This Page

Advertisement: