c++ - Issues when reading from a .cpp file -
i have file call text.cpp. mission read file.
text.cpp file (inside file)
#include <iostream> #include <cstring> #include <ctype.h> #include "program.h" #include "binarytree.h" #include "linkedlist.h" when detect
'#' symbol the system start looping, if detect '<' & '"' symbol, print out remain word. example output : iostraem
i can detect '<' symbol , print output success. when detect '"' symbol, can print remain word (program.h), follow 烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫 behind word.
may know whats wrong of code?
my code below:
#include <iostream> #include <string> #include <fstream> using namespace std; int main() { char template[100]; char filename[20]; cout << "enter filename:" ; cin >> filename; fstream read(filename); if (read.is_open()) { while(!read.eof()) { read.getline(template, 100); (int k = 0; k < 100 ;k++) { if(template[k] == '#') { for(int = 0; < 100; i++) { if(template[i] == '<' || template[i] == '"') { (int j = i+1; j < 100; j++) { if(template[j] == '>' || template[j] == '"') { break; } cout << template[j]; } cout <<endl; } } } } } } else { cout << "file not exist, press enter exit program." << endl; system("pause"); exit(exit_success); } system("pause"); } output:
enter filename: text.cpp iostream cstring ctype.h program.h ype.h binarytree.h 烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫 linkedlist.h 烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫
this way more complicated needs be. might consider this:
#include <iostream> #include <string> #include <fstream> int main() { std::string inputfilename; std::cout << "enter filename:"; std::cin >> inputfilename; std::ifstream inputfile(inputfilename); if(inputfile) { std::string line; while(std::getline(inputfile, line)) { if(line.find('#') != std::string::npos) { size_t startpos = line.find_first_of("<\""); size_t endpos = line.find_last_of(">\""); if(startpos != std::string::npos && endpos != std::string::npos) { //advance start after < or " ++startpos; //sanity check in case line ill formed if(startpos < endpos) { std::cout << line.substr(startpos, endpos - startpos) << std::endl; } } } } } else { std::cout << "file '" << inputfilename << "' not exist." << std::endl; } return 0; }
Comments
Post a Comment