【问题标题】:Readin in stdin to my program returns ��,଄��? What is this将标准输入读入我的程序会返回 ��,଄��?这是什么
【发布时间】:2015-03-06 01:45:22
【问题描述】:

我试图了解如何修复我的代码,但它返回了一堆奇怪的字符。这是什么,我该如何解决?

涉及 2 个程序。第一个读取 stdinput 并将该行发送到另一个程序,然后该程序单独解析和评估每个文件。当第二个程序尝试单独读取每个文件时,它会出错并返回它无法读取文件“��,଄��”

为第一个程序输入标准输入的代码

while (fgets(tmpstring, 1024, stdin) && (tmpstring != NULL)) 
{
    fileName = tmpstring;

    write(report2access[1], fileName, (LINE_MAX* sizeof(char))+1);
    write(negreport2access[1], fileName, (LINE_MAX* sizeof(char))+1);

}

评估第二个程序中每个文件名的代码

while (fgets(tmpstring, 1024, stdin) && (tmpstring != NULL)) 
{
    fileName = tmpstring;

    //remove newline
    if ((pos=strchr(fileName, '\n')) != NULL)
        *pos = '\0';

    token = strtok(fileName, s);

    //walk through tokens
    while(token != NULL){
        access = lastAccess(token);

        if (num > 0){
            if(access > argvseconds){

                printf("%s\n", token); //#DEBUG
            }
        }

        else if (num < 0){
            if (access < argvseconds){
                printf("%s\n", token); //#DEBUG
            }
        }
        token = strtok(NULL, s);
    }
}

【问题讨论】:

  • 您错过了帖子中最重要的内容,tmpstring 的声明。
  • 我声明为 char tmpstring[1024];
  • 所以tmpstring != NULL 总是正确的。你需要发布第二个程序。
  • 第二个代码 sn-p 来自第二个程序,我应该制作它!=“”那么?当没有文件返回并且它只是空格时,我试图不让它运行
  • 第一个代码中的写语句是write(report2access[1], fileName, (LINE_MAX* sizeof(char))+1);,但应该是write(report2access[1], fileName, strlen(fileName)-1);(或者如果你也想写换行符,你可以去掉-1)。您正在向另一个程序以及文件名写入大量垃圾。

标签: c stdin


【解决方案1】:

一般来说,它看起来像一个文件名,后面跟着一堆垃圾字节被写入negreport2access[1]report2access[1],然后第二个程序试图解析这些垃圾字节,就好像它们包含有效的文件名一样。

【讨论】:

    猜你喜欢
    • 2020-08-04
    • 2021-09-29
    • 2020-08-28
    • 1970-01-01
    • 2017-10-16
    • 2011-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多