// grabline - get's a line from a text file where first n chars match pattern // written jan 05 by warren myers for joe myers using namespace std; #include #include #include #include int main(int argc, char** argv){ unsigned int match = 0, skip = 0; int kt = 0; string pat, fle, cmd, out, line, mt, pad; char ln[25555]; ofstream ofle; if(argc>1){ skip = atoi(argv[1]); } else{ cout << "How many characters to skip? "; cin >> skip; } if(argc>2){ mt = argv[2]; } else{ cout << "Enter what you want to find: "; cin >> mt; } match = mt.length(); if(argc>3){ pat = argv[3]; } else{ cout << "Enter file pattern: "; cin >> pat; } if(argc>4){ out = argv[4]; } else{ cout << "Enter file to put results in: "; cin >> out; } cout << "Working...\n"; ofle.open(out.c_str()); if(!ofle.good()){ cout << "Cannot create output file: " << out << ". Bailing.\n"; return 1; } pad=""; kt=0; while(kt<(match+skip+1)){ pad += " "; kt++; } cmd = "dir /b " + pat + " > grabline.in"; system(cmd.c_str()); ifstream ifle("grabline.in"); if(!ifle.good()){ cout << "Couldn't make grabline.in with file results. Bailing.\n"; return 1; } ifstream chk; while(!ifle.eof()){ pat = fle; ifle.getline(ln,260); // next file name fle = ln; if(fle == pat) break; if(!fle.length()) continue; chk.open(fle.c_str()); if(!chk.good()){ cout << "Couldn't open: " << fle << endl; } else{ kt=0; while(!chk.eof()){ chk.getline(ln,25550); line = ln + pad; kt++; if(line.substr(skip,match) == mt){ ofle << "in file: " << fle << " at line: " << kt << " *\t" << line.substr(skip) << "\n"; } } } chk.close(); } ifle.close(); ofle.close(); ifle.open(out.c_str()); if(ifle.good()){ cout << "Results of search:\n\n"; while(!ifle.eof()){ ifle.getline(ln,25550); cout << ln << endl; } } cout << "Done. Please press a key to end...\n"; getchar(); return 0; }