C/C++读取分析文件
1、读取文件,逐行读取的函数
View Code
1 void read_line(FILE* fp,char* line) 2 { 3 if (fp == NULL)//判断指针是否为空 4 { 5 return; 6 } 7 int i = 0; 8 9 char ch = fgetc(fp); 10 while(ch != '\n' && !feof(fp)) 11 { 12 line[i] = ch; 13 i++; 14 ch = fgetc(fp); 15 } 16 line[i] = '\0'; 17 return; 18 }
或者可以用getline()函数读取
View Code
#include <iostream> #include <string> #include <fstream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { cout<<"-----------------------------------------------\n"; cout<<"File Begin"<<endl; //开始处理文件 fstream istrm("Company.js"); fstream ostrm("Company.txt"); string fstr; int nBeginPos; int nEndPos; int nLen = 0; string strsub; for(int i = 0;i < 10;i++) { getline(istrm,fstr); //cout<<fstr<<endl; nBeginPos = fstr.find("id"); nEndPos = fstr.find_first_of(',',nBeginPos); nLen = nEndPos - nBeginPos; strsub = fstr.substr(nBeginPos,nLen); //cout<<"-----------------------------------------------\n"; //cout<<strsub<<endl; ostrm<<strsub<<'|'; nBeginPos = fstr.find("CnName"); nEndPos = fstr.find_first_of(',',nBeginPos); nLen = nEndPos - nBeginPos; strsub = fstr.substr(nBeginPos,nLen); //cout<<"-----------------------------------------------\n"; //cout<<strsub<<endl; ostrm<<strsub<<'|'; nBeginPos = fstr.find("CNaddress"); nEndPos = fstr.find_first_of(',',nBeginPos); nLen = nEndPos - nBeginPos; strsub = fstr.substr(nBeginPos,nLen); //cout<<"-----------------------------------------------\n"; //cout<<strsub<<endl; ostrm<<strsub<<'|'; nBeginPos = fstr.find("phone"); nEndPos = fstr.find_first_of(',',nBeginPos); nLen = nEndPos - nBeginPos; strsub = fstr.substr(nBeginPos,nLen); //cout<<"-----------------------------------------------\n"; //cout<<strsub<<endl; ostrm<<strsub<<'|'; nBeginPos = fstr.find("fax"); nEndPos = fstr.find_first_of(',',nBeginPos); nLen = nEndPos - nBeginPos; strsub = fstr.substr(nBeginPos,nLen); //cout<<"-----------------------------------------------\n"; //cout<<strsub<<endl; ostrm<<strsub<<'|'; nBeginPos = fstr.find("Email"); nEndPos = fstr.find_first_of(',',nBeginPos); nLen = nEndPos - nBeginPos; strsub = fstr.substr(nBeginPos,nLen); //cout<<"-----------------------------------------------\n"; //cout<<strsub<<endl; ostrm<<strsub<<'|'; //nBeginPos = fstr.find("Homepage"); //nEndPos = fstr.find_first_of(',',nBeginPos); //nLen = nEndPos - nBeginPos; //strsub = fstr.substr(nBeginPos,nLen); //cout<<"-----------------------------------------------\n"; //cout<<strsub<<endl; //ostrm<<strsub<<','; nBeginPos = fstr.find("Website"); nEndPos = fstr.find_first_of(',',nBeginPos); nLen = nEndPos - nBeginPos; strsub = fstr.substr(nBeginPos,nLen); //cout<<"-----------------------------------------------\n"; //cout<<strsub<<endl; ostrm<<strsub<<endl; } /* FILE *fp; fp=fopen("Company.js","r"); char buff[1024*10]; memset(buff,0,1024*10); read_line(fp,buff); cout<<buff<<endl;*/ cout<<"-----------------------------------------------\n"; cout<<"File End"<<endl; system("pause"); return 0; }
读取文件的几种方式:
View Code
void file1() { char ch; int i; char s[]={'A','B','\n','C'}; FILE *fp1,*fp2; if((fp1=fopen("test1.txt","wt"))==NULL) { printf("can not open file\n"); exit(0); } if((fp2=fopen("test2.txt","wb"))==NULL) { printf("can not open file\n"); exit(0); } for(i=0;i<4;i++) { fputc(s[i],fp1); //以文本方式向文件中写入数据 fputc(s[i],fp2); //以二进制方式向文件中写入数据 } fclose(fp1); fclose(fp2); if((fp1=fopen("test1.txt","rt"))==NULL) { printf("can not open file\n"); exit(0); } if((fp2=fopen("test1.txt","rb"))==NULL) { printf("can not open file\n"); exit(0); } ch=fgetc(fp1); while(!feof(fp1)) //以文本方式从文件中读取数据 { printf("%02X",ch); ch=fgetc(fp1); } printf("\n"); ch=fgetc(fp2); while(!feof(fp2)) //以二进制方式从文件中读取数据 { printf("%02X",ch); ch=fgetc(fp2); } printf("\n"); fclose(fp1); fclose(fp2); }
View Code
void file2() { int n; FILE* fp; if((fp=fopen("test.txt","a"))==NULL) { printf("can not open file\n"); exit(0); } n=ftell(fp); //得到此时fp所处位置距文件首的偏移字节数 printf("%d\n",n); fputs("test",fp); n=ftell(fp); printf("%d\n",n); fclose(fp); }
View Code
void file3() { int ch; int n; FILE *fp; if((fp=fopen("test.txt","r+"))==NULL) { printf("can not open file\n"); exit(0); } fseek(fp,1L,0); //将fp移动到距文件首1字节的位置 ch=fgetc(fp); printf("%c\n",ch); //rewind(fp); fseek(fp,1L,0); fputs("test",fp); ch=fgetc(fp); printf("%c\n",ch); fclose(fp); }
测试文件的几个函数例子
View Code
int main(void) { char szBuff[1024] = "file test"; cout<<szBuff<<endl; FILE *fp; //写文件 fp=fopen("acctinfo.txt","wt+"); if(fp==NULL) { printf("can not open file\n"); exit(0); } cout<<"-------------------------------------------------------------------------------"<<endl; //fputs(szBuff,fp);//往文件里写单个字符 fwrite(szBuff,strlen(szBuff),1,fp);//往文件里写数据 //读文件 /*fp=fopen("acctinfo.txt","r"); char buff[1024]; memset(buff,0,1024); int nLen = ftell(fp); int i = 0; while(!feof(fp)) { read_line(fp,buff); cout<<buff<<endl; //i++; //if (i == 10) //{ // break; //} } cout<<i<<endl; */ /* fseek(fp, 0L, SEEK_END); fseek(fp,0,SEEK_SET);//将文件指针移回首部 rewind(fp);//将文件指针放到开头 fgets(buff,1024,fp) ; cout<<buff<<endl; fread(buff,strlen(szBuff),1,fp); cout<<buff<<endl; fclose(fp);//关闭文件 */ /* ifstream in("acctinfo.txt"); string line; getline(in, line); cout<<line<<endl; */ system("pause"); return 0; }


浙公网安备 33010602011771号