新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
C++獲取文件具體方法詳解
在這篇文章中,我們將會(huì)為大家詳細(xì)介紹一下有關(guān)C++獲取文件的相關(guān)方法。對(duì)于剛剛接觸C++編程語(yǔ)言不久的朋友們來(lái)說(shuō),這篇文章介紹的內(nèi)容可以幫助他們解決一些在文件操作中經(jīng)常遇到的難題。

- /*read File*/
- char *txt = NULL;
- long txtlen;
- //seek to file end to calculate file length
- fseek(fp,0,SEEK_END);
- txtlen=ftell(fp);
- //rewind to file start
- rewind(fp);
- //read from file
- txt = new char[txtlen + 1];
- if (txt != NULL)
- {
- fread(txt,sizeof(char),txtlen,fp);
- txt[txtlen]='\0';
- fv.setData(txt);
- }
- //close file and destroy temp array
- fclose(fp);
- if(txt!=NULL)
- {
- delete []txt;
- txt = NULL;
- }
C++獲取文件的寫(xiě)法:
- /*read File*/
- ifstream in(filesrc);
- if(in.fail())
- {
- printf("open file failed!\n");
- }
- else
- {
- string strtmp;
- while (getline(in,strtmp))
- {
- fv.getData()+=strtmp;
- fv.getData()+='\n';
- }
- in.close();
- }
以上就是我們?yōu)榇蠹医榻B的C++獲取文件相關(guān)方法。
【編輯推薦】
- C++ makefile寫(xiě)法標(biāo)準(zhǔn)格式簡(jiǎn)介
- C++統(tǒng)計(jì)對(duì)象個(gè)數(shù)方法詳解
- C++ #define預(yù)處理指令特點(diǎn)評(píng)比
- C++二維數(shù)組初始化相關(guān)應(yīng)用技巧分享
- C++模擬event關(guān)鍵字具體實(shí)現(xiàn)方案
文章名稱(chēng):C++獲取文件具體方法詳解
本文地址:http://www.5511xx.com/article/coghjss.html


咨詢(xún)
建站咨詢(xún)
