簡體   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