Node Results
1001 0.00 1157.33 55.61 0.00
1002 0.00 1157.33 53.96 0.00
Link Results
1 2.27 0.01 0.00
6 2.04 0.02 0.00
Node Results
1001 0.00 1157.33 55.61 0.00
1002 0.00 1157.33 53.96 0.00
Link Results
1 2.27 0.01 0.00
6 2.04 0.02 0.00
Node Results
1001 0.00 1157.33 55.61 0.00
1002 0.00 1157.33 53.96 0.00
Link Results
1 2.27 0.01 0.00
6 2.04 0.02 0.00
文档中周期性出现“Node Results”和“Link Results”,将文档中所有“Node Results”后面的内容全部输出,小白新手,非常期待大神的指点!!!!!

新手求助!C++,从输入文件中找到指定字符串,并将该字符串后面的内容全部输出。
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
4条回答 默认 最新
- 枫舞的季节 2017-08-16 05:56关注
#include <stdio.h> #include <string> #include <iostream> using namespace std; string findNodeString(FILE* fp) { string outbuf; if(!fp) { return ""; } int state = 0; int pos = 0; while(1) { char c = fgetc(fp); if(c==-1) { break; } switch(state) { case 0: pos = ftell(fp); if(c=='N') { if(c=='N' && (c=fgetc(fp),c=='o')&&(c=fgetc(fp),c=='d')&&(c=fgetc(fp),c=='e') &&(c=fgetc(fp),c==' ')&&(c=fgetc(fp),c=='R')&&(c=fgetc(fp),c=='e') &&(c=fgetc(fp),c=='s')&&(c=fgetc(fp),c=='u')&&(c=fgetc(fp),c=='l') &&(c=fgetc(fp),c=='t')&&(c=fgetc(fp),c=='s')) { state=1; } else { fseek(fp, pos + 1, SEEK_SET); } } // else if(c=='L') // { // if(c=='L' && (c=fgetc(fp),c=='i')&&(c=fgetc(fp),c=='n')&&(c=fgetc(fp),c=='k') // &&(c=fgetc(fp),c==' ')&&(c=fgetc(fp),c=='R')&&(c=fgetc(fp),c=='e') // &&(c=fgetc(fp),c=='s')&&(c=fgetc(fp),c=='u')&&(c=fgetc(fp),c=='l') // &&(c=fgetc(fp),c=='t')&&(c=fgetc(fp),c=='s')) // { // state=1; // } // else // { // fseek(fp, pos + 1, SEEK_SET); // } // } break; case 1: if(c==' ' || c=='\r' || c=='\n' || c=='\t' || (c>='0' && c<='9') || c=='.') { outbuf+=c; continue; } else { //输出结果 pos = ftell(fp); fseek(fp, -1,SEEK_CUR); return outbuf; } break; default: break; } } return ""; } int main(int argc,char** argv) { FILE *fp = fopen("d:\\test\\test.txt","rb"); if(fp) { while(1) { string str = findNodeString(fp); if(str.empty()) { break; } cout << str << endl; } fclose(fp); } else { cout <<"fopen failed" << endl; } return 0; }
thank you.
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报