简体   繁体   中英

Issue with fread when read from stdin for System generated and Manual Input

Application reads the input from the stdin and the function is as follows:

filepos = ftell(stdin);
if (filepos < 0 && errno != 0)
{
    perror("ftell");
    return 1;
}
if ((n = fread(input_data, sizeof(char), 2, stdin)) != 2)
{
    if (n == 1)
    {
        if (*input_data == '\n')
            fprintf(stderr, "Unexpected NL character read\n");
        else if (*input_data== '\r')
            fprintf(stderr, "Unexpected CR character read\n");
        else
            fprintf(stderr, "Unexpected character read <%c>\n", *input_data);
    }
    else if (n != 0 && errno != 0)
    {
        perror("fread");
    }
    return 1;
}
    ... process data ....

When I ran this over a system generated input the application is processing correctly and when I ran this for the same output which is manually created, I am getting the error message ""Unexpected NL character read".

$ convertInput < input.system > out
$
$ convertInput < input.manual > out
Unexpected NL character read
$

Both the cases the output is correct.

When I did a diff between the two input files, it showed the message as below.

$ diff input.manual input.system
1c1
< INPUT
---
> INPUT
\ No newline at end of file

I have verified the manual input file and there is no new line also after the input. I am not sure whether the fread itself should be replaced with fgets or something to fix this.

The gdb showed that the "fread" returned "0" after the end of INPUT for "input.system" where as the "fread" returned "\\n" after the end of INPUT for "input.manual".

The manual file is created as "vim input" and "pasted" the data and removed all characters after the end of data (including "\\n") and "save and quit" the editor.

Any suggestions or thoughts to fix this is appreciated.

Thanks,

The problem depends on the way you create the "manual" file. If you use a text editor (vim), it's normal that it puts a \\n at the very last place, since it has to complete te last "text" line. I'd rather use a binary editor to do the job. As far as I remember, recent vim's versions have a "binary mode". Another way to create a file that misses the last \\n is by using "echo -en '...your data...' > file". The -n options omits the \\n, the -e one interprets the sequence of characters beginning with "\\" (eg "\\n", "\\r"...). I hope it can help.

By the way, "ftell" on stdin may return useless values if stdin is not redirected to a real file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM