对于刚刚接触C++编程语言不久的朋友们来说,这篇文章介绍的内容可以帮助他们解决一些在文件操作中经常遇到的难题。
- /*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();
 - }
 























