自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C++獲取文件具體方法詳解

開發(fā) 后端
C++編程語言對于文件的操作是一個比較基礎(chǔ)而且重要的操作。我們今天將會為大家詳細(xì)介紹其中C++獲取文件的像相關(guān)操作,方便大家理解。

在這篇文章中,我們將會為大家詳細(xì)介紹一下有關(guān)C++獲取文件的相關(guān)方法。對于剛剛接觸C++編程語言不久的朋友們來說,這篇文章介紹的內(nèi)容可以幫助他們解決一些在文件操作中經(jīng)常遇到的難題。

  1. /*read File*/  
  2. char *txt = NULL;  
  3. long txtlen;  
  4. //seek to file end to calculate file length  
  5. fseek(fp,0,SEEK_END);  
  6. txtlen=ftell(fp);  
  7. //rewind to file start  
  8. rewind(fp);  
  9. //read from file  
  10. txt = new char[txtlen + 1];  
  11. if (txt != NULL)   
  12. {  
  13. fread(txt,sizeof(char),txtlen,fp);  
  14. txt[txtlen]='\0';  
  15. fv.setData(txt);  
  16. }  
  17. //close file and destroy temp array  
  18. fclose(fp);  
  19. if(txt!=NULL)  
  20. {  
  21. delete []txt;  
  22. txt = NULL;  

C++獲取文件的寫法:

  1. /*read File*/  
  2. ifstream in(filesrc);  
  3. if(in.fail())  
  4. {  
  5. printf("open file failed!\n");  
  6. }  
  7. else  
  8. {  
  9. string strtmp;  
  10. while (getline(in,strtmp))  
  11. {  
  12. fv.getData()+=strtmp;  
  13. fv.getData()+='\n';  
  14. }  
  15. in.close();  

以上就是我們?yōu)榇蠹医榻B的C++獲取文件相關(guān)方法。

【編輯推薦】

  1. C++ makefile寫法標(biāo)準(zhǔn)格式簡介
  2. C++統(tǒng)計對象個數(shù)方法詳解
  3. C++ #define預(yù)處理指令特點評比
  4. C++二維數(shù)組初始化相關(guān)應(yīng)用技巧分享
  5. C++模擬event關(guān)鍵字具體實現(xiàn)方案
責(zé)任編輯:曹凱 來源: 博客園
點贊
收藏

51CTO技術(shù)棧公眾號