linux c建立資料夾,並在資料夾中建立檔案

2021-06-27 09:17:40 字數 1885 閱讀 4381

建立資料夾,在在檔案裡面建立檔案:

乙個例項:

#include

#include

#include

#include

int main()

}char pathname[100];

pathname[0]='f';

pathname[1]='l';

pathname[2]='o';

pathname[3]='w';

pathname[4]='/';

pathname[5]='/';

int i;

for(i=0;txtname[i]!='\0';i++)

pathname[i+6]='.'; //這幾句是加上字尾 .txt的

pathname[i+7]='t';

pathname[i+8]='x';

pathname[i+9]='t';

pathname[i+10]='\0';//最後別忘記加上這個

file  *fp;

if((fp=fopen(pathname,"w"))==null)//開啟檔案 沒有就建立

fprintf(fp,"建立成功");

fclose(fp);

return 0;

編譯乙個後有警告:

createfile.c: in function 『main』:

createfile.c:10:16: warning: multi-character character constant

createfile.c:10:5: warning: overflow in implicit constant conversion

修改:char txtname[100];

*txtname='005';//這句將結果轉變為字串

改為:char txtname[100] = 「005」;

access()函式的使用:無印證

標頭檔案:unistd.h

功 能: 確定檔案或資料夾的訪問許可權。即,檢查某個檔案的訪問方式,比如說是唯讀方式、只寫方式等。如果指定的訪問方式有效,則函式返回0,否則函式返回-1。

用 法: int access(const char *filenpath, int mode); 或者int _access( const char *path, int mode );

引數說明:

filenpath

檔案或資料夾的路徑,當前目錄直接使用檔案或資料夾名

備註:當該引數為檔案的時候,access函式能使用mode引數所有的值,當該引數為資料夾的時候,access函式值能判斷資料夾是否存在。在win nt 中,所有的資料夾都有讀和寫許可權

mode

要判斷的模式

在標頭檔案unistd.h中的預定義如下:

#define r_ok 4 /* test for read permission. */

#define w_ok 2 /* test for write permission. */

#define x_ok 1 /* test for execute permission. */

#define f_ok 0 /* test for existence. */

具體含義如下:

r_ok 只判斷是否有讀許可權

w_ok 只判斷是否有寫許可權

x_ok 判斷是否有執行許可權

f_ok 只判斷是否存在

access函式程式範例(c語言中)

#include

#include

int file_exists(char *filename);

int main(void)

int file_exists(char *filename)

建立資料夾

當某資料夾不存在時,建立資料夾 import os path dir file if not os.path.exists path dir os.makedirs path dir 同時建立資料夾有兩種函式,os.mkdir和os.makedirs,兩者的區別在於前者是一級一級建立檔案目錄,後者可...

Qt 新建資料夾並在該資料夾下新建檔案

在編寫乙個小工具時,需要儲存日誌檔案,並且是統一放在乙個資料夾下,一開始單純的用filename log log.txt 發現行不通,於是檢視qt幫助文件 找到了方法,如下 qstring filename log.txt qdir dir if dir.exists log dir log qst...

C 建立資料夾,刪除資料夾,建立檔案,刪除檔案

protected void button1 click object sender,eventargs e 判斷檔案的存在 else string name getfiles.filename 獲取已上傳檔案的名字 string size getfiles.postedfile.contentle...