Search code examples
cfgets

Call to fgets results in string printing to stdout


void deleteFile( FAT *allotable ) {
/* PRECONDITION: This function expects a FAT structure that is valid.
 * POSTCONDITION: A file is flagged as removed from the disk and it will 
 * be possible to write over it
 */

    // Local variables
    unsigned char test[9] = { 0 };

    // Select file to remove
    // TODO: The user will select the file to remove based on the 
    // listing in listDir
    // For testing, we are removing file at location 0 in the entry
    fgets( test, NAME_SIZE, stdin );
    return;
}

When I run the function and type in a string, I see the string printed back in stdout. I am sure I have an issue with a buffer, but I can't seem to figure this out.


Solution

  • When you type characters into the console, they are echoed back to you. The characters will still be read when you read from stdin.

    Alternatively you can pipe the output of a program into your own, or redirect a file to stdin. In those two cases, the characters will not be echoed:

    echo Program output | ./myprog
    

    or:

    ./myprog < fileinput.txt
    

    edit - Sounds like it's a terminal problem.

    You haven't stated what system you are using or how you are interfacing with it, but I can get this behaviour by connecting to a system via SSH with PuTTY.

    I change the terminal settings to force on both "Local echo" and "Local line editing". Then I get the line echoed whenever I press enter. Obviously only one of those should be on. Preferably "Local echo".