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

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

为第一个程序输入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);
    }
}

最佳答案

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

关于c - 以stdin读取我的程序会返回��,଄��?这是什么,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28891074/

10-11 22:40
查看更多