新聞中心
在Linux系統(tǒng)下,文件操作是非常常見的任務。其中,文件的讀寫操作是一個十分基本和常見的操作。而在C語言中,打開文件和讀寫文件常常使用fopen函數(shù)。fopen函數(shù)可以打開一個指定的文件,并返回一個文件指針,由此可以進行文件的讀寫操作。

創(chuàng)新互聯(lián)是一家集網(wǎng)站建設,羅定企業(yè)網(wǎng)站建設,羅定品牌網(wǎng)站建設,網(wǎng)站定制,羅定網(wǎng)站建設報價,網(wǎng)絡營銷,網(wǎng)絡優(yōu)化,羅定網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學習、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。
fopen函數(shù)的語法如下:
FILE* fopen(const char *path, const char *mode);
其中,path表示打開文件的路徑,mode表示打開的模式,具體的打開方式如下表所示:
| 模式 | 描述 |
| —- | —- |
| r | 以只讀方式打開文件 |
| w | 以只寫方式打開文件 |
| a | 以追加方式打開文件 |
| r+ | 以可讀可寫的方式打開文件 |
| w+ | 以可讀可寫的方式打開文件(若文件存在則清空文件) |
| a+ | 以可讀可寫的方式打開文件(若文件存在則從文件尾開始寫入) |
值得注意的是,在Linux系統(tǒng)下,文件路徑需要使用”/”代替”\”,并且路徑中不允許出現(xiàn)空格。
接下來,我們將通過實例詳解fopen函數(shù)在Linux下的路徑操作。
示例1:在當前目錄下創(chuàng)建一個文件并寫入數(shù)據(jù)
+ 代碼:
“`
#include
int mn()
{
FILE *fp;
char *filename = “test.txt”;
char *mode = “w”;
char *content = “This is a test file.”;
fp = fopen(filename, mode);
if(fp == NULL)
{
printf(“Fled to open file %s.\n”, filename);
}
else
{
fprintf(fp, “%s”, content);
printf(“Write file %s succeeded.\n”, filename);
fclose(fp);
}
return 0;
}
“`
+ 運行結果:
“`
Write file test.txt succeeded.
“`
我們可以看到,程序正常地創(chuàng)建了一個文件,并向其中寫入了數(shù)據(jù)。
示例2:在當前目錄下打開一個已有文件并讀取數(shù)據(jù)
+ 代碼:
“`
#include
int mn()
{
FILE *fp;
char *filename = “test.txt”;
char *mode = “r”;
char buffer[100];
fp = fopen(filename, mode);
if(fp == NULL)
{
printf(“Fled to open file %s.\n”, filename);
}
else
{
fgets(buffer, 100, fp);
printf(“Read file %s succeeded: %s\n”, filename, buffer);
fclose(fp);
}
return 0;
}
“`
+ 運行結果:
“`
Read file test.txt succeeded: This is a test file.
“`
在這個例子中,我們打開了一個已有的文件,并從中讀取了數(shù)據(jù)。fgets函數(shù)可以從文件中讀取一行數(shù)據(jù),并將其保存在一個字符數(shù)組中。
示例3:在上級目錄中創(chuàng)建一個文件并寫入數(shù)據(jù)
+ 代碼:
“`
#include
int mn()
{
FILE *fp;
char *filename = “../test.txt”;
char *mode = “w”;
char *content = “This is a test file in the parent directory.”;
fp = fopen(filename, mode);
if(fp == NULL)
{
printf(“Fled to open file %s.\n”, filename);
}
else
{
fprintf(fp, “%s”, content);
printf(“Write file %s succeeded.\n”, filename);
fclose(fp);
}
return 0;
}
“`
+ 運行結果:
“`
Write file ../test.txt succeeded.
“`
在這個例子中,我們在上級目錄中創(chuàng)建了一個文件,并向其中寫入了數(shù)據(jù)。需要注意的是,我們在路徑中使用了”../”來表示上級目錄。
示例4:在絕對路徑下創(chuàng)建一個文件并寫入數(shù)據(jù)
+ 代碼:
“`
#include
int mn()
{
FILE *fp;
char *filename = “/home/user/test.txt”;
char *mode = “w”;
char *content = “This is a test file with absolute path.”;
fp = fopen(filename, mode);
if(fp == NULL)
{
printf(“Fled to open file %s.\n”, filename);
}
else
{
fprintf(fp, “%s”, content);
printf(“Write file %s succeeded.\n”, filename);
fclose(fp);
}
return 0;
}
“`
+ 運行結果:
“`
Write file /home/user/test.txt succeeded.
“`
這個例子展示了如何使用絕對路徑來創(chuàng)建一個文件。需要注意的是,不同用戶的home目錄可能不同,因此需要根據(jù)實際情況修改路徑。
相關問題拓展閱讀:
- linux fopen函數(shù) 打開文件總是失敗
- linux c 的 open(文件路徑,O_WRON | O_CREAT) 里面的與運算為什么可以實現(xiàn)打不開就創(chuàng)建
- fopen的路徑參數(shù)不能是char*?
linux fopen函數(shù) 打開文件總是失敗
是這樣襪吵的再輸入文件路徑的時候要注意:
你的方向錯了應殲唯該是”\”這樣的反斜杠而且要輸入兩個反斜杠
因為
字符串
中的1個反斜告改侍杠的意思就是說他是個轉(zhuǎn)意字符只有\(zhòng)\的時候才會顯示出來1個字符向你的那個路徑就應該寫成fp=fopen(“\\mnt\\yaffs\\red.txt”)
有興趣共同探討C就給我留言啊
linux c 的 open(文件路徑,O_WRON | O_CREAT) 里面的與運算為什么可以實現(xiàn)打不開就創(chuàng)建
因為第二個參數(shù):O_WRON | O_CREAT
O_CREAT:如果打不開就創(chuàng)建
O_WRON | O_CREAT中間使用“|”,所以支持打不開就創(chuàng)建
open 函數(shù)可以打開或創(chuàng)建一個文件。
#include
#include
#include
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
返回值:成功返回新分配的文件描述符,出錯返回-1并設置errno
在Man Page中open 函數(shù)有兩種形式,一種帶兩個參數(shù),一種帶三個參數(shù),其實在C代碼
中open 函數(shù)是這樣聲明的:
int open(const char *pathname, int flags, …);
最后的可變參數(shù)可以是0個或1個,由flags 參數(shù)中的標志位決定,見下面的詳細說明。
pathname 參數(shù)是要打開或創(chuàng)建的文件名,和fopen 一樣,pathname 既可以是相對路徑也可以是絕
對路徑。flags 參數(shù)有一系列常數(shù)值可供選擇,可以同時選擇多個常數(shù)用按位或運算符連接起
來,所以這些常數(shù)的宏定義都以O_開頭,表示or。
必選項:以下三個常數(shù)中必須指定一個,且僅允許指定一個。
O_RDON 只讀打開
O_WRON 只寫打開
O_RDWR 可讀可寫打開
以下可選項可以同時指定0個或多個,和必選項按位或起來作為flags 參數(shù)??蛇x項有很多,這
里只介紹一部分,其它選項可參考open(2)的Man Page:
O_APPEND 表示追加。如果文件已有內(nèi)容,這次打開文件所寫的數(shù)據(jù)附加到文件的末尾而不
覆蓋原來的內(nèi)容。
O_CREAT 若此文件不存在則創(chuàng)建它。使用此選項時需要提供第三個參數(shù)mode ,表示該文件
的訪問權限。
O_EXCL 如果同時指定了O_CREAT,并且文件已存在,則出錯返回。
O_TRUNC 如果文件已存在,并且以只寫或可讀可寫方式打開,則將其長度截斷
(Truncate)為0字節(jié)。
O_NONBLOCK 對于設備文件,以O_NONBLOCK 方式打開可以做非阻塞I/O(Nonblock I/O).
在C中,常常使用二進制位來做控制標志,這個辦法使得高效且代碼短小,在頭文件fcntl.h中,可以見到O_WRON的定義值是”01″,八位二進制就是””,O_CREAT是八進制”0100″,二進制就是””,豎線“|”不是“與”,是逐位“或”運算,O_RWON|O_CREAT合起來就是”“,這兩個”1″的位置并不沖突,在這里,open()函數(shù)得到的值是編譯器已經(jīng)合并好了的值””,open()函數(shù)可以根據(jù)這兩個獨立的二進制”位”知道是讀寫打開或者創(chuàng)建。
這個是位或,不是與。位或、位與是這樣計算的。
如:二進制的 010 | 001 結果是 011,而 010 & 001 結果就是0了。
O_WRON 和 O_CREAT 的關系就相當于上面的 010 和 001。他們位或的值不是0,位與的值就是0了。0表示什么都不做。用了位或后,就在一個整型的值上設置了不同的標志位,open函數(shù)會檢測對應的標志位,如果該標志位設置為1了,就執(zhí)行對應的操作。
O_CREAT的意思就是創(chuàng)建的意思,在這里就是將 創(chuàng)建文件 的標志位設置為1,這樣open函數(shù)無法寫這個文件的時候就會創(chuàng)建他。
#include
int open(const char *pathname, int oflag, … /*
mode_t mode */ );
We show the third argument as …, which is the ISO C way to specify that the number and types of the remaining arguments may vary. For this function, the third argument is used only when a new file is being created, as we describe later. We show this argument as a comment in the prototype.
The pathname is the name of the file to open or create. This function has a multitude of options, which are specified by the oflag argument. This argument is formed by ORing together one or more of the following constants from the header:
O_RDON
Open for reading only.
O_WRON
Open for writing only.
O_RDWR
Open for reading and writing.
Most implementations define O_RDON as 0, O_WRON as 1, and O_RDWR as 2, for compatibility with older programs.
One and only one of these three constants must be specified. The following constants are optional:
O_APPEND
Append to the end of file on each write. We describe this option in detail in Section 3.11.
O_CREAT
Create the file if it doesn’t exist. This option requires a third argument to the open function, the mode, which specifies the access permission bits of the new file. (When we describe a file’s access permission bits in Section 4.5, we’ll see how to specify the mode and how it can be modified by the umask value of a process.)
O_EXCL
Generate an error if O_CREAT is also specified and the file already exists. This test for whether the file already exists and the creation of the file if it doesn’t exist is an atomic operation. We describe atomic operations in more detail in Section 3.11.
O_TRUNC
If the file exists and if it is successfully opened for either write-only or readwrite, truncate its length to 0.
O_NOCTTY
If the pathname refers to a terminal device, do not allocate the device as the controlling terminal for this process. We talk about controlling terminals in Section 9.6.
O_NONBLOCK
If the pathname refers to a FIFO, a block special file, or a character special file, this option sets the nonblocking mode for both the opening of the file and subsequent I/O. We describe this mode in Section 14.2.
fopen的路徑參數(shù)不能是char*?
照理說file應該聲明為char數(shù)組啊宏洞,getcwd是將當前路徑復制到之一個參數(shù)指向的空間,你只聲明了一個指針鉛談,槐絕碰沒有分配空間。你試試
char file;
linux fopen 路徑的介紹就聊到這里吧,感謝你花時間閱讀本站內(nèi)容,更多關于linux fopen 路徑,Linux下fopen路徑操作實例詳解,linux fopen函數(shù) 打開文件總是失敗,linux c 的 open(文件路徑,O_WRON | O_CREAT) 里面的與運算為什么可以實現(xiàn)打不開就創(chuàng)建,fopen的路徑參數(shù)不能是char*?的信息別忘了在本站進行查找喔。
成都創(chuàng)新互聯(lián)建站主營:成都網(wǎng)站建設、網(wǎng)站維護、網(wǎng)站改版的網(wǎng)站建設公司,提供成都網(wǎng)站制作、成都網(wǎng)站建設、成都網(wǎng)站推廣、成都網(wǎng)站優(yōu)化seo、響應式移動網(wǎng)站開發(fā)制作等網(wǎng)站服務。
名稱欄目:Linux下fopen路徑操作實例詳解(linuxfopen路徑)
本文網(wǎng)址:http://www.5511xx.com/article/djeoojg.html


咨詢
建站咨詢
