C 檔案操作的5種方式

2021-06-09 05:21:10 字數 2199 閱讀 1862

純c語言讀取檔案方式

寫檔案file *pfile;

pfile=fopen("jingge.txt","w");

fwrite("",1,strlen("")+1,pfile);

fseek(pfile,0,seek_set);

fwrite("liyuanjing",1,strlen("liyuanjing"),pfile);

//fclose(pfile);

fflush(pfile);

讀檔案file *pfile;

pfile=fopen("jingge.txt","r");

char *pchr;

fseek(pfile,0,seek_end);

int len=ftell(pfile);

pchr=new char[len+1];

rewind(pfile);

fread(pchr,1,len,pfile);

fclose(pfile);

pchr[len]=0;

messagebox(pchr);

c++讀寫檔案方式

寫檔案ofstream ofs("4.txt");

ofs.write("",strlen(""));

ofs.close();

讀檔案ifstream ifs("4.txt");

char ch[100];

memset(ch,0,100);

ifs.read(ch,100);

ifs.close();

messagebox(ch);

windows api讀寫檔案方式

寫檔案handle pfile;

pfile=createfile("5.txt",generic_write,0,null,create_new,file_attribute_normal,null);

dword dwwrite;

writefile(pfile,"",strlen(""),&dwwrite,null);

closehandle(pfile);

讀檔案 handle hfile;

hfile=createfile("5.txt",generic_read,0,null,open_existing,file_attribute_normal,null);

char ch[100];

dword dwread;

readfile(hfile,ch,100,&dwread,null);

ch[dwread]=0;

closehandle(hfile);

messagebox(ch);

cfile類讀寫檔案方式

寫檔案cfile file("6.txt",cfile::modecreate | cfile::modewrite);

file.write("",strlen(""));

file.close();

讀檔案cfile file("6.txt",cfile::moderead);

char *pchr;

dword dwfilelen;

dwfilelen=file.getlength();

pchr=new char[dwfilelen+1];

pchr[dwfilelen]=0;

file.read(pchr,dwfilelen);

file.close();

messagebox(pchr);

mfc提供的cfiledialog方式讀寫檔案

寫檔案cfiledialog filedlg(false);

filedlg.m_ofn.lpstrtitle="靜哥另存為對話方塊";

filedlg.m_ofn.lpstrfilter="text file(*.txt)\0*.txt\0all file(*.*)\0*.*\0\0";

filedlg.m_ofn.lpstrdefext="txt";

if(idok==filedlg.domodal())

讀檔案cfiledialog filedlg(true);

filedlg.m_ofn.lpstrtitle="靜哥另存為對話方塊";

filedlg.m_ofn.lpstrfilter="text file(*.txt)\0*.txt\0all file(*.*)\0*.*\0\0";

if(idok==filedlg.domodal())

C 檔案操作的5種方式

純c語言讀取檔案方式 寫檔案file pfile pfile fopen jingge.txt w fwrite 1,strlen 1,pfile fseek pfile,0,seek set fwrite liyuanjing 1,strlen liyuanjing pfile fclose pf...

Linux檢視檔案內容的5種方式

目錄 1.more指令 分頁顯示檔案內容 2.less指令 可以向前或向後檢視檔案內容 3.head指令 檢視檔案開頭的內容 4.tail指令 顯示檔案尾部的內容 5.cat指令 顯示檔案內容 more指令會以一頁一頁的形式顯示檔案內容 more file1檢視檔案file1的檔案內容 more n...

5種連線方式

if object id a is not null begin drop table a endgo create table a name nvarchar 3 nvarchar 2 go if object id b is not null begin drop table b endgo c...