在C中使用read()从stdin读取
我想逐个字符读取stdin字符,并仅使用系统调用将其与其他字符进行比较,我的问题是,给定代码:在C中使用read()从stdin读取,c,stdin,system-calls,C,Stdin,System Calls,我想逐个字符读取stdin字符,并仅使用系统调用将其与其他字符进行比较,我的问题是,给定代码: #include "util.h" #define STDOUT 1 #define STDIN 1 #define SYS_READ 3 #define SYS_WRITE 4 #define SYS_OPEN 5 #define SYS_CLOSE 6 #define SYS_LSEEK 19 int main (int argc , char* argv[], char*
#include "util.h"
#define STDOUT 1
#define STDIN 1
#define SYS_READ 3
#define SYS_WRITE 4
#define SYS_OPEN 5
#define SYS_CLOSE 6
#define SYS_LSEEK 19
int main (int argc , char* argv[], char* envp[])
{
char str[512];
int fd;
if((fd= system_call(SYS_OPEN,STDIN,0, 0777))==-1){
system_call(SYS_WRITE,STDOUT,"stdin error", 11);
}
while((system_call(SYS_READ,fd, str,1))>0){
if((strncmp(";",str,1))==0){
system_call(SYS_WRITE,STDOUT,str, 1);
system_call(SYS_WRITE,STDOUT,"\n", 2);
}
else{
system_call(SYS_WRITE,STDOUT,str, 1);
}
}
return 0;
}
我有一个程序集文件,它将“系统调用(SYS_-WRITE,STDOUT,“\n”,2);”转换为写入(…);
现在的问题是,我不知道如何让它等待输入,所以它永远不会启动while循环,因为它没有从输入中读取任何内容
编辑,系统调用的代码:
system_call:
push ebp ; Save caller state
mov ebp, esp
sub esp, 4 ; Leave space for local var on stack
pushad ; Save some more caller state
mov eax, [ebp+8] ; Copy function args to registers: leftmost...
mov ebx, [ebp+12] ; Next argument...
mov ecx, [ebp+16] ; Next argument...
mov edx, [ebp+20] ; Next argument...
int 0x80 ; Transfer control to operating system
mov [ebp-4], eax ; Save returned value...
popad ; Restore caller state (registers)
mov eax, [ebp-4] ; place returned value where caller can see it
add esp, 4 ; Restore caller state
pop ebp ; Restore caller state
ret ; Back to caller
解决了的
我最终解决了STDIN的问题(define为off),没有定义一个fd来接收值,结果它工作了,不知道确切的原因。我猜fd被设置为非阻塞。您将STDIN定义为1,而它应该是0。1是标准输出,因此您试图从仅用于写入的文件句柄中读取。您是否也有函数“system\u call”的代码?如果您也能看到这一点,那就太好了。我希望它在里面等待,只有在等待完成之后,它才会返回一个值。这意味着您的while循环也将等待,并且无法进行评估before@thebaconing添加它您是新加入此。。。什么新鲜事?C编程?使用read()/write()?使用汇编?你还没有真正把你的问题简化为一个问题,所以很难给你一个单一的答案…我对C和使用系统调用都是新手,我可以使用fget执行这段代码,但是我不知道如果不使用stdlib和eofy怎么处理你可以使用
while(fgets(buffer,bufferlen,stdin)){…}
读取等待状态下的stdin是的,我也注意到了这一点,并修复了它,但仍然不起作用,但我设法编写了一个不同的代码,所以现在一切都正常了。谢谢