【问题标题】:Why does the following C code skip the read() system call to execute the next write() system call?为什么下面的 C 代码会跳过 read() 系统调用来执行下一个 write() 系统调用?
【发布时间】:2018-03-12 21:44:52
【问题描述】:

我的问题是关于 Linux 系统编程,特别是关于读写 API。

我正在编写一个复制 shell 的程序。它接受一个字符串参数并用空格分隔符对其进行标记。根据第一个令牌的命令,它使用剩余的令牌作为参数执行操作。到目前为止,我只为“添加”命令实现了这个。代码循环运行,直到用户输入“n”表示“继续? [是/否]'。但是,在第一次迭代之后,我的程序在第一次 write() 调用后跳过 read() 以输入命令,并以“继续?”结束。写()调用。为什么它会在第一次 write() 之后立即跳过 read() 调用?

int main (int argc, char *argv[]) {
int true=0;
while (true==0) {
    char buff1[]="Please enter your command\n";
    int count1= strlen(buff1);
    write (STDOUT_FILENO, buff1, count1);
    char buff2[100];
    int count2=read (STDIN_FILENO, buff2, 100);
    buff2[count2-1]='\0';
    char *list[30]; //This creates an array of character pointers (strings)
    /*
    * Begin tokenization and entering tokens in list
    */
    const char delim[]=" ";
    char *token;
    token=strtok(buff2, delim);
    const char newline[]="\n";
    int i=0;
    while (token!= NULL) {
        write (STDOUT_FILENO, newline, strlen(newline));
        list[i]=token;
        write (STDOUT_FILENO, list[i], strlen(list[i]));
        i++;
        token=strtok(NULL,delim);
    }
    /*
    * End tokenization
    */

    /*
    * Begin Addition operation
    */
    const char add[]="add";
    if (strcmp(list[0], add)==0) {
        int result=0;
        for (int j=1; j<i; j++) {
            result+=atoi(list[j]);
        }
        char sum[50];
        int sumcount=sprintf(sum, "%d", result);
        write (STDOUT_FILENO, newline, strlen(newline));
        write (STDOUT_FILENO, sum, sumcount);
    }
    /*
    * End Addition operation
    */


    char *truefalse;
    char endmessage[]="Continue: [y/n]\n";
    write (STDOUT_FILENO, endmessage, strlen(endmessage));
    read (STDIN_FILENO, truefalse, 1);
    if (*truefalse=='n') {
        true=1;
    }

}
return 0;
}

As this output image shows, in the second iteration, after asking me to enter a command, the code skips to asking me to continue rather than actually reading my command

【问题讨论】:

  • 为了便于阅读和理解:1) 一致地缩进代码。在每个左大括号 '{' 后缩进。在每个右大括号 '};' 之前不缩进。建议每个缩进级别为 4 个空格。
  • 发布的代码无法编译!。它缺少所需的 #include 语句,用于公开从 C 库调用的函数的原型的头文件。如stdio.hstring.hunistd.h
  • 关于:char *truefalse;read (STDIN_FILENO, truefalse, 1); 指针 truefalse 永远不会设置为指向应用程序拥有的某些内存。它将包含堆栈上的任何垃圾作为其位置。设置指针指向的值是(因此)未定义的行为,并可能导致段错误事件。
  • 应检查调用read() 的返回值以确保操作成功。
  • 关于:int main (int argc, char *argv[]) 参数argcargv 未使用,这会导致编译器输出2 条警告消息。建议使用签名:int main( void )

标签: c linux operating-system systems-programming


【解决方案1】:

你的程序有未定义的行为。

您使用尚未初始化的指针指向任何有效的东西。

线

char *truefalse;

声明了一个指针,但它还没有被初始化为指向任何有效的东西。你继续在行中使用它

read (STDIN_FILENO, truefalse, 1);

代替

char *truefalse;
char endmessage[]="Continue: [y/n]\n";
write (STDOUT_FILENO, endmessage, strlen(endmessage));
read (STDIN_FILENO, truefalse, 1);

使用

char truefalse; // Make it an object instead of a pointer.
char endmessage[]="Continue: [y/n]\n";
write (STDOUT_FILENO, endmessage, strlen(endmessage));
read (STDIN_FILENO, &truefalse, 1); // Use the address of the variable.

更新

您的代码在第二次迭代中不等待您输入任何内容的原因是换行符仍留在输入流中。第二次调用只是读取换行符。

阅读问题的答案后,您需要代码来跳过该行的其余部分。

最简单的方法是使用:

int c;
while ((c = fgetc(stdin)) != EOF && c != '\n');

【讨论】:

  • 非常感谢您指出这一点(双关语)。但是,这仍然不能解决我的问题,我得到相同的输出
  • @OmerFarooqAhmed,你为什么使用低级别的read/write函数而不是fread/fwrite
  • 导师专门告诉我们暂时使用读/写 更新成功了,谢谢!
猜你喜欢
  • 1970-01-01
  • 2014-02-22
  • 2011-02-04
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 2021-04-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多