新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C++獲取文件具體方法詳解
在這篇文章中,我們將會為大家詳細介紹一下有關(guān)C++獲取文件的相關(guān)方法。對于剛剛接觸C++編程語言不久的朋友們來說,這篇文章介紹的內(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++獲取文件的寫法:
- /*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寫法標準格式簡介
- C++統(tǒng)計對象個數(shù)方法詳解
- C++ #define預處理指令特點評比
- C++二維數(shù)組初始化相關(guān)應用技巧分享
- C++模擬event關(guān)鍵字具體實現(xiàn)方案
網(wǎng)站標題:C++獲取文件具體方法詳解
本文鏈接:http://www.dlmjj.cn/article/coghjss.html


咨詢
建站咨詢
