如何使用read系统调用从用户那里获取输入并使用write system调用将其存储在文件中。在C. [英] How do I take input from user using read system call and store in a file using write system call. In C
本文介绍了如何使用read系统调用从用户那里获取输入并使用write system调用将其存储在文件中。在C.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我现在一直在论坛上,我正在寻求进一步的帮助。
Jochen一直在帮助我,但我没有关注一些东西。
如果有人能帮助我告诉我我的代码是否正确我会很高兴
如果错了,具体修改是什么。
我尝试过:
I have been on the forum now and I am looking for further help.
Jochen has been helping me out , however I am not following some stuff.
I would be glad if someone could just help me with letting me know if the code I have is correct or not
If its wrong, what is the exact modification to be made.
What I have tried:
#define USERNAME_SIZE 10
int main()
{
int fd;
fd = fd("user.dat", O_CREAT | O_RWDR, 0666);
if (fd == -1)
{
write(1,"There is an error opening the file",35);
}
write(1,"Enter the username",18);
char username[USERNAME_SIZE];
char c;
int count = 0;
do
{
read(0, &c, 1);
if (c == '\n')
break;
username[count++] = c;
}
while (count < USERNAME_SIZE - 1);
username[count] = 0;
write(fd,username,count);
close(fd);
return 0;
}
推荐答案
为什么使用低级别读取
获得用户输入?使用友好的系统调用,如 gets_s,_getws_s [ ^ ]读取完整的字符串。
此外,您还没有解释您的程序有什么问题。
Why are you using the low levelread
to get user input? Use a friendly system call like gets_s, _getws_s[^] which reads a complete string.
Also, you have not explained what is wrong with your program.
这篇关于如何使用read系统调用从用户那里获取输入并使用write system调用将其存储在文件中。在C.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文