Issues with writing to a file in c++ -
as part of parsing file, task involves stripping off comments in file , parsing file. once parsing completed, need insert comments @ same lines.
to strip of comments, read file , check if comment has occured , enter newline there. use ragel this, , logic works fine.
example: file.cpp #1 /** comments */ #2 /** comments */ #3 #4 abcd : xyz, 123 #5 efgh : abc, 987 intermediate_file.cpp #1 #2 #3 #4 abcd : xyz, 123 #5 efgh : abc, 987 the comments stored line numbers in std::map<int,string> , used later iterate , output string file, intermediate_file.cpp. using below code same:
fstream m_fstr; //class member std::map<int,std::string> m_comments_map; //to hold comments void writebackcommentstofile(void) { m_fstr.open("intermediate_file.cpp", ios_base::out); int linenbr = 0; for(std::map<int,std::string>::iterator = m_comments_map.begin(); != m_comments_map.end(); ++it) { linenbr = it->first; if(m_fstr.is_open()) { m_fstr.seekg(std::ios::beg); gotoline(m_fstr,linenbr ); m_fstr << it->second << endl; } } } std::fstream& gotoline(std::fstream& file, unsigned int num){ for(int i=0; < num - 1; ++i){ file.ignore(std::numeric_limits<std::streamsize>::max(),'\n'); //getline(m_fstr,readline); -- try } return file; } after parsing complete, trying write comments m_comments_map file intermediate_file.cpp above logic, able print first line in file - /** comments */ . rest of contents not written file.
i have tried printing out contents of std::map console , verified comments stored in std::map. problem lies in printing out std::map contents file. can tell me part of code incorrect.?
i have implemented above using logic described here : in c++ there way go specific line in text file?
Comments
Post a Comment