Retrieving Multiple inputs with fgets?

This is a discussion on Retrieving Multiple inputs with fgets? within the C Programming forums, part of the General Programming Boards category; i want to retrieve 6 lines of text, from a user. I've tried the following: char userInput[160]; fgets(userInput, 160, stdin); ...

  1. #1
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335

    Retrieving Multiple inputs with fgets?

    i want to retrieve 6 lines of text, from a user. I've tried the following:
    char userInput[160];

    fgets(userInput, 160, stdin);
    >some text inputted1
    fgets(userInput, 160, stdin);
    >some text inputted2
    fgets(userInput, 160, stdin);
    >some text inputted3
    fgets(userInput, 160, stdin);
    >some text inputted4
    fgets(userInput, 160, stdin);
    >some text inputted5
    fgets(userInput, 160, stdin);
    >some text inputted6

    for some reason when i use printf("%s", userInput); it prints the latest inputted text and not the previously entered ones

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It doesn't copy one line onto the end of another. It simply reuses the buffer you keep passing it. If you want multiple lines stored, copy them to another buffer, and concatenate them. (See: strcat)


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    strcat looks like it'll do the job but i was just intrested, can this be done with fgets only?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    Maybe
    Code:
    char userInput[6][160];
    fgets(userInput[0], 160, stdin);
    fgets(userInput[1], 160, stdin);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    Segmentation fault :|

  6. #6
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    and strcat seems to work with a fixed string but it won't work with stdin

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Axel
    Segmentation fault :|
    With what code? Salem's example is fine.
    Quote Originally Posted by Axel
    and strcat seems to work with a fixed string but it won't work with stdin
    With what code? stdin is a file stream alias. It's not a string.

    You can't just say "it doesn't work" without showing your code. Well you can, but you're not going to get any help that way.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    This is an answer.

    Correct, it's as useless as your question.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    Quote Originally Posted by quzah
    With what code? Salem's example is fine.With what code? stdin is a file stream alias. It's not a string.

    You can't just say "it doesn't work" without showing your code. Well you can, but you're not going to get any help that way.


    Quzah.

    I don't think strcpy will work with what i'm trying to do it works fine with fixed strings but what about entered text from the user?

    Also:

    Code:
    char userInput[6][160];
    that doesn't seem to work it just gives me a segmentation error. What i've did is create 6, 1 dimensional arrays representing the rows so i used gets as follows:


    fgets(text1, 10, stdin); .... text7

    and when i print it, it works :-) it's not really an ideal way of doing it
    but i still can't figure how to fill a 2-d array using fgets without overwriting the buffer when i use fgets mulitple times.

  10. #10
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Quote Originally Posted by Axel
    but i still can't figure how to fill a 2-d array using fgets without overwriting the buffer when i use fgets mulitple times.
    Code:
    {
        int line;
        char userInput[6][160];
        for (line = 0 ; line < 6; line++)
            fgets(userInput[line], 160, stdin);
    
        for (line = 0 ; line < 6; line++)
            printf("line %d: %s\n", line, userInput[line]);
    }

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    See how hard that was? Now if you had posted more than a single line of code, we could have looked at your example and told you what was wrong. See cwr's code? Let's pretend it was yours, and you posted it. Let's also pretend it's incorrect. Let's pretend you now said something like: "I'm trying to fill 'userInput' with six lines of input, but [whatever.... it stops after line 1, it doesn't print them right, whatever it is]."


    See how much better that would have been than just sticking one line of code and saying "that doesn't seem to work it just gives me a segmentation error."


    I hate having to teach people how to ask questions.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    thanks cwr. There's just one problem though printf("%c", userInput[line1][line2]);
    i have a for loop which prints out every character in the column and goes to the next row and prints out the next column etc...
    as soon as it gets to the last element in the column it prints out a space.
    i.e.

    userInput[0][0] = 'H';
    userInput[0][1] = 'e';
    userInput[0][2] = 'l';
    userInput[0][3] = 'l';
    userInput[0][4] = 'o';
    userInput[0][5] = ' '; // <--------- annoying space.

    Is there anyway to discard it? I've tried using != '\0' but it doesn't seem to help.

    But i'm gussing the main problem is with the original code, it doesn't discard the spaces by check if it's not '\0'.

    I've tried

    Code:
    
        for (row = 0 ; line < 6; row++)
            fgets(userInput[row][col], 160, stdin);
    but i just get

    warning: passing arg 1 of `fgets' makes pointer from integer without a cast


    It's a bit tricky because cwr's code, seems to fill the columns, as soon as somethings entered it adds the space to the end and goes to the next row, i can't check if an element inside a column has a space with another for loop.


    Also, quzah sorry about my my previous post... in future i'll try to be more specific.
    Last edited by Axel; 09-12-2005 at 04:48 AM.

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    > warning: passing arg 1 of `fgets' makes pointer from integer without a cast
    Well yeah, because userInput[row][col] is a single char, and fgets needs an array of them
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  14. #14
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    i tried referecing a pointer (userInput)

    fgets(*userInput, 160, stdin);

    but it doesn't seem to fill the array properly.

  15. #15
    Registered User
    Join Date
    Mar 2005
    Posts
    22

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. checking inputs meets multiple conditions
    By 60beetle60 in forum C Programming
    Replies: 5
    Last Post: 04-19-2008, 08:25 AM
  2. How do i ouput multiple inputs?
    By vodka9er in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2007, 08:45 AM
  3. multiple inputs of multiple types
    By frontz in forum C Programming
    Replies: 8
    Last Post: 01-19-2004, 01:57 PM
  4. Reading multiple values using fgets
    By Stripes in forum C Programming
    Replies: 3
    Last Post: 06-12-2002, 07:21 AM
  5. Getting multiple inputs using cin
    By edk in forum C++ Programming
    Replies: 2
    Last Post: 09-13-2001, 02:34 PM

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