0

I am having some issues whenever the user inputs a pipe "|" character the output becomes all mangled, I have already tried flushing the buffer to no avail.

#include <stdio.h>

int main(int argc, char **argv)
{
    char userInput[2000];
    while(1)
    {
        printf("Please Enter Your Input:");
        scanf("%s", userInput);
        printf("%s\n", userInput);
    }
    return 0;
}
6
  • Code looks fine to me. Commented Oct 16, 2013 at 1:55
  • It scans one white space delimited word at a time. So if there are spaces in the input it's a little awkward. Commented Oct 16, 2013 at 1:59
  • 1
    Can you show an input and corresponding output? "Here is what I got, and this is what I was expecting" is usually helpful. Commented Oct 16, 2013 at 2:04
  • 2
    There's nothing that should cause problems with the | character. Can you copy-and-paste the output for a run of the program? One unlikely possibility: could | be mapped to something in your tty settings? Run stty to see your settings. But as @CharlieBurns says, you will get a bit of a mess if there are spaces in your input. Try typing "1 2 3 4" as input; is that the same kind of mess you get with |? Commented Oct 16, 2013 at 2:05
  • Are you reading from a keyboard or a piped input? Commented Oct 16, 2013 at 3:41

1 Answer 1

2

There are a couple of things you can do to debug this. First - read in the entire input line, rather than the first word. The safe way to do this is with getline() - it will notice if the line is too long for your input buffer, and adjust things (updated with thanks to Elchonon Edelson)

char *myString;
int stringLength;
size_t bufLength=0;
myString = NULL; // let getline() adjust the string
stringLength = getline(&myString, &bufLength, stdin)

Next, print out the line as entered:

printf("The line is <<%s>>\n", myString);

Note the use of << and >> to show where the string starts / ends - see white space etc.

Finally, print out the string one character at a time, including the hex code:

for(ii = 0; ii < stringLength; ii++) {
  char ch;
  ch = myString[ii];
  printf("myString[%d]: character '%c', hex code %02x\n", ii, ch, ch);
}

This should help you pinpoint the problem.

Sign up to request clarification or add additional context in comments.

7 Comments

Also, compile with all warnings and debugging info (gcc -Wall -g) and learn how to use the gdb debugger
Happy to see you accepted the answer - meaning that you must have figured out what was wrong. Would you tell us what you discovered?
This is wrong. The getline() function is specified to take a size_t *, not an int. (Also, the getline() function takes care of making sure there's enough room for the terminaling null character, so telling it the buffer is smaller than it really is, is pointless, and your code never attempts to append a null character either, so again, telling it the buffer is smaller than its actual size, is, again, pointless.)
Other notes regarding getline(): It isn't necessary to malloc() a buffer just to hand it to the function, unless you've already got a general-purpose buffer hanging around you might as well just pass it a pointer to a NULL pointer. In fact, direct quote from the POSIX docs: Setting *lineptr to a null pointer and *n to zero are allowed and a recommended way to start parsing a file.
Also, at no point does the POSIX specification actually say what the getline() function will do to the second argument, although the only rational explanation is that if it resizes your buffer, the new size will be stored there.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.