C++獲取文件具體方法詳解
作者:佚名
C++編程語言對于文件的操作是一個比較基礎(chǔ)而且重要的操作。我們今天將會為大家詳細(xì)介紹其中C++獲取文件的像相關(guān)操作,方便大家理解。
在這篇文章中,我們將會為大家詳細(xì)介紹一下有關(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)方法。
【編輯推薦】
責(zé)任編輯:曹凱
來源:
博客園