【发布时间】:2014-08-06 10:21:01
【问题描述】:
我已经编写了用于在 linux 中读取、写入系统调用的示例代码......执行没有任何问题。
因此,将缓冲区数据存储到文件中......
存储在文件中的预期结果是 Hello World!..但我正在像这样 Hello World 的文件中获取数据!^@
我需要做什么才能得到预期的结果?
int main(int argc , char *argv[])
{
int fd;
char buff[14];
fd = open("myfile.txt",O_CREAT| O_WRONLY,0777);
if(fd == -1)
{
printf("Failed to open the file to write");
exit(0);
}
write(fd,"Hello World!\n",13);
close(fd);
fd = open("myfile.txt",O_RDONLY,0777);
if(fd == -1)
{
printf("Failed to open the file to read");
exit(0);
}
read(fd,buff,14);
buff[14] = '\0';
close(fd);
printf("Buff is %s\n",buff);
return 0;
}
【问题讨论】:
-
buff[14] = '\0';-->buff[13] = '\0'; -
您是否进行了任何重新编译或重建?
标签: c linux linux-kernel operating-system