日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Linux下fopen路徑操作實(shí)例詳解(linuxfopen路徑)

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

創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),羅定企業(yè)網(wǎng)站建設(shè),羅定品牌網(wǎng)站建設(shè),網(wǎng)站定制,羅定網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,羅定網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(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)空格。

接下來,我們將通過實(shí)例詳解fopen函數(shù)在Linux下的路徑操作。

示例1:在當(dāng)前目錄下創(chuàng)建一個(gè)文件并寫入數(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;

}

“`

+ 運(yùn)行結(jié)果:

“`

Write file test.txt succeeded.

“`

我們可以看到,程序正常地創(chuàng)建了一個(gè)文件,并向其中寫入了數(shù)據(jù)。

示例2:在當(dāng)前目錄下打開一個(gè)已有文件并讀取數(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;

}

“`

+ 運(yùn)行結(jié)果:

“`

Read file test.txt succeeded: This is a test file.

“`

在這個(gè)例子中,我們打開了一個(gè)已有的文件,并從中讀取了數(shù)據(jù)。fgets函數(shù)可以從文件中讀取一行數(shù)據(jù),并將其保存在一個(gè)字符數(shù)組中。

示例3:在上級目錄中創(chuàng)建一個(gè)文件并寫入數(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;

}

“`

+ 運(yùn)行結(jié)果:

“`

Write file ../test.txt succeeded.

“`

在這個(gè)例子中,我們在上級目錄中創(chuàng)建了一個(gè)文件,并向其中寫入了數(shù)據(jù)。需要注意的是,我們在路徑中使用了”../”來表示上級目錄。

示例4:在絕對路徑下創(chuàng)建一個(gè)文件并寫入數(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;

}

“`

+ 運(yùn)行結(jié)果:

“`

Write file /home/user/test.txt succeeded.

“`

這個(gè)例子展示了如何使用絕對路徑來創(chuàng)建一個(gè)文件。需要注意的是,不同用戶的home目錄可能不同,因此需要根據(jù)實(shí)際情況修改路徑。

相關(guān)問題拓展閱讀:

  • linux fopen函數(shù) 打開文件總是失敗
  • linux c 的 open(文件路徑,O_WRON | O_CREAT) 里面的與運(yùn)算為什么可以實(shí)現(xiàn)打不開就創(chuàng)建
  • fopen的路徑參數(shù)不能是char*?

linux fopen函數(shù) 打開文件總是失敗

是這樣襪吵的再輸入文件路徑的時(shí)候要注意:

你的方向錯(cuò)了應(yīng)殲唯該是”\”這樣的反斜杠而且要輸入兩個(gè)反斜杠

因?yàn)?/p>

字符串

中的1個(gè)反斜告改侍杠的意思就是說他是個(gè)轉(zhuǎn)意字符只有\(zhòng)\的時(shí)候才會(huì)顯示出來1個(gè)字符向你的那個(gè)路徑就應(yīng)該寫成fp=fopen(“\\mnt\\yaffs\\red.txt”)

有興趣共同探討C就給我留言啊

linux c 的 open(文件路徑,O_WRON | O_CREAT) 里面的與運(yùn)算為什么可以實(shí)現(xiàn)打不開就創(chuàng)建

因?yàn)榈诙€(gè)參數(shù):O_WRON | O_CREAT

O_CREAT:如果打不開就創(chuàng)建

O_WRON | O_CREAT中間使用“|”,所以支持打不開就創(chuàng)建

open 函數(shù)可以打開或創(chuàng)建一個(gè)文件。

#include

#include

#include

int open(const char *pathname, int flags);

int open(const char *pathname, int flags, mode_t mode);

返回值:成功返回新分配的文件描述符,出錯(cuò)返回-1并設(shè)置errno

在Man Page中open 函數(shù)有兩種形式,一種帶兩個(gè)參數(shù),一種帶三個(gè)參數(shù),其實(shí)在C代碼

中open 函數(shù)是這樣聲明的:

int open(const char *pathname, int flags, …);

最后的可變參數(shù)可以是0個(gè)或1個(gè),由flags 參數(shù)中的標(biāo)志位決定,見下面的詳細(xì)說明。

pathname 參數(shù)是要打開或創(chuàng)建的文件名,和fopen 一樣,pathname 既可以是相對路徑也可以是絕

對路徑。flags 參數(shù)有一系列常數(shù)值可供選擇,可以同時(shí)選擇多個(gè)常數(shù)用按位或運(yùn)算符連接起

來,所以這些常數(shù)的宏定義都以O(shè)_開頭,表示or。

必選項(xiàng):以下三個(gè)常數(shù)中必須指定一個(gè),且僅允許指定一個(gè)。

O_RDON 只讀打開

O_WRON 只寫打開

O_RDWR 可讀可寫打開

以下可選項(xiàng)可以同時(shí)指定0個(gè)或多個(gè),和必選項(xiàng)按位或起來作為flags 參數(shù)??蛇x項(xiàng)有很多,這

里只介紹一部分,其它選項(xiàng)可參考o(jì)pen(2)的Man Page:

O_APPEND 表示追加。如果文件已有內(nèi)容,這次打開文件所寫的數(shù)據(jù)附加到文件的末尾而不

覆蓋原來的內(nèi)容。

O_CREAT 若此文件不存在則創(chuàng)建它。使用此選項(xiàng)時(shí)需要提供第三個(gè)參數(shù)mode ,表示該文件

的訪問權(quán)限。

O_EXCL 如果同時(shí)指定了O_CREAT,并且文件已存在,則出錯(cuò)返回。

O_TRUNC 如果文件已存在,并且以只寫或可讀可寫方式打開,則將其長度截?cái)?/p>

(Truncate)為0字節(jié)。

O_NONBLOCK 對于設(shè)備文件,以O(shè)_NONBLOCK 方式打開可以做非阻塞I/O(Nonblock I/O).

在C中,常常使用二進(jìn)制位來做控制標(biāo)志,這個(gè)辦法使得高效且代碼短小,在頭文件fcntl.h中,可以見到O_WRON的定義值是”01″,八位二進(jìn)制就是””,O_CREAT是八進(jìn)制”0100″,二進(jìn)制就是””,豎線“|”不是“與”,是逐位“或”運(yùn)算,O_RWON|O_CREAT合起來就是”“,這兩個(gè)”1″的位置并不沖突,在這里,open()函數(shù)得到的值是編譯器已經(jīng)合并好了的值””,open()函數(shù)可以根據(jù)這兩個(gè)獨(dú)立的二進(jìn)制”位”知道是讀寫打開或者創(chuàng)建。

這個(gè)是位或,不是與。位或、位與是這樣計(jì)算的。

如:二進(jìn)制的 010 | 001 結(jié)果是 011,而 010 & 001 結(jié)果就是0了。

O_WRON 和 O_CREAT 的關(guān)系就相當(dāng)于上面的 010 和 001。他們位或的值不是0,位與的值就是0了。0表示什么都不做。用了位或后,就在一個(gè)整型的值上設(shè)置了不同的標(biāo)志位,open函數(shù)會(huì)檢測對應(yīng)的標(biāo)志位,如果該標(biāo)志位設(shè)置為1了,就執(zhí)行對應(yīng)的操作。

O_CREAT的意思就是創(chuàng)建的意思,在這里就是將 創(chuàng)建文件 的標(biāo)志位設(shè)置為1,這樣open函數(shù)無法寫這個(gè)文件的時(shí)候就會(huì)創(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應(yīng)該聲明為char數(shù)組啊宏洞,getcwd是將當(dāng)前路徑復(fù)制到之一個(gè)參數(shù)指向的空間,你只聲明了一個(gè)指針鉛談,槐絕碰沒有分配空間。你試試

char file;

linux fopen 路徑的介紹就聊到這里吧,感謝你花時(shí)間閱讀本站內(nèi)容,更多關(guān)于linux fopen 路徑,Linux下fopen路徑操作實(shí)例詳解,linux fopen函數(shù) 打開文件總是失敗,linux c 的 open(文件路徑,O_WRON | O_CREAT) 里面的與運(yùn)算為什么可以實(shí)現(xiàn)打不開就創(chuàng)建,fopen的路徑參數(shù)不能是char*?的信息別忘了在本站進(jìn)行查找喔。

成都創(chuàng)新互聯(lián)建站主營:成都網(wǎng)站建設(shè)、網(wǎng)站維護(hù)、網(wǎng)站改版的網(wǎng)站建設(shè)公司,提供成都網(wǎng)站制作、成都網(wǎng)站建設(shè)、成都網(wǎng)站推廣、成都網(wǎng)站優(yōu)化seo、響應(yīng)式移動(dòng)網(wǎng)站開發(fā)制作等網(wǎng)站服務(wù)。


本文標(biāo)題:Linux下fopen路徑操作實(shí)例詳解(linuxfopen路徑)
網(wǎng)站網(wǎng)址:http://www.dlmjj.cn/article/djeoojg.html