fgets() Problem

This is a discussion on fgets() Problem within the C Programming forums, part of the General Programming Boards category; Hi I'm new to programming with C, and am having trouble understanding why my use of the fgets() function does ...

  1. #1
    Registered User kihina's Avatar
    Join Date
    Feb 2010
    Posts
    1

    fgets() Problem

    Hi

    I'm new to programming with C, and am having trouble understanding why my use of the fgets() function does not allow me to input any text from the command line...

    I am trying to store a string into the first row of an array (and then will move onto the next row, etc.)

    When I execute my program, it simply skips to the next part of my method without letting me input any text, and instead stores a white space in the string.

    Thankfully I don't have to worry about going over the allotted amount of space in the array (we are to assume that the user enters a string less than 50 characters)

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    //creates 2D array to hold input
    char matrix [50][50]; //defines character array of [rows][columns]
    int length = 0; //stores length of input string
    
    int main(void)
    {
    
    int i = 0; //defines column
    int j = 0; //defines row 
    int k = 0; //int referring to char in string
    char inputText[50]; //will store input text
    		
            // for one row at a time
    	for (j=0; j<2; j++) {
    
    		//stores input as string
    		printf("Please enter a line of text.\n");
    		fgets(inputText, sizeof(inputText), stdin);
    		
                    length = strlen(inputText);
    		printf("Length of file: %d\n", length);
    			
    			...
    I haven't included the rest of the code, since it is rather long. (Hopefully I have indented/posted this correctly!)

    Any help would be greatly appreciated! Thanks in advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    I tested with this program and could not duplicate your problem:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    //creates 2D array to hold input
    char matrix [50][50]; //defines character array of [rows][columns]
    int length = 0; //stores length of input string
    
    int main(void)
    {
        int i = 0; //defines column
        int j = 0; //defines row 
        int k = 0; //int referring to char in string
        char inputText[50]; //will store input text
    
        // for one row at a time
        for (j=0; j<2; j++) {
    
            //stores input as string
            printf("Please enter a line of text.\n");
            fgets(inputText, sizeof(inputText), stdin);
    
            length = strlen(inputText);
            printf("Length of file: %d\n", length);
        }
    
        return 0;
    }
    Post the smallest and simplest compilable program that demonstrates the problem.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The code you have posted, works fine (I did finish off the for loop with a closing brace).

    It loops through and gets two words, and tell me their length, just like it's supposed to.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    I'm guessing in the editing of your long code, that you've managed to snip some scanf() calls before that loop is entered.

    Use fgets() for ALL input (don't mix and match, it's usually a disaster).
    Having read a line using fgets(), you can then use something like sscanf() to parse an int (say), or any other function available for converting your data.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting illegal case error
    By scmurphy64 in forum C Programming
    Replies: 2
    Last Post: 09-17-2009, 10:35 AM
  2. fgets in switch-case problem
    By icestorm in forum C Programming
    Replies: 3
    Last Post: 08-20-2009, 07:07 PM
  3. Problem with fgets....
    By Huskar in forum C Programming
    Replies: 5
    Last Post: 03-29-2009, 10:13 AM
  4. problem with fgets
    By learninC in forum C Programming
    Replies: 3
    Last Post: 05-19-2005, 08:10 AM
  5. print problem while using fgets()
    By learninC in forum C Programming
    Replies: 12
    Last Post: 05-15-2005, 09:29 PM

Tags for this Thread


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