繁体   English   中英

系统调用以读写文件

[英]system call to write and read from a file

我想测试的系统调用readwrite

#include <unistd.h>
#include <fcntl.h>

int main(void)
{
    fd = open("/Users/me/Desktop/PubRepo/C/APUE/3.File_IO/test", O_RDWR);
    write(fd, "Test the first line",20);
}

抄送报告:

In [29]: !cc write_test.c                                                                                         
write_test.c:6:5: error: use of undeclared identifier 'fd'
    fd = open("/Users/me/Desktop/PubRepo/C/APUE/3.File_IO/test", O_RDWR);
    ^
write_test.c:7:11: error: use of undeclared identifier 'fd'
    write(fd, "Test the first line",20)
          ^
2 errors generated.

我有一些python基础知识,但不知道如何完成代码。

您需要声明fd是什么类型。 那是什么类型 检查open()的引用,其中提到:

int open(const char * path,int oflag,...);

您可以看到返回类型为int 因此,应为该函数的返回值分配的变量也应为同一类型。

所以改变:

fd = open("/Users/me/Desktop/PubRepo/C/APUE/3.File_IO/test", O_RDWR);

对此:

int fd = open("/Users/me/Desktop/PubRepo/C/APUE/3.File_IO/test", O_RDWR);

PS:如果文件不存在,则需要执行以下操作:

int fd = open("/Users/me/Desktop/PubRepo/C/APUE/3.File_IO/test", O_RDWR | O_CREAT, 0600);

使用open()在C中创建文件中阅读更多内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM