开发者

C++ ofstream - only 1 string gets written to file, previous string is overwritten, why?

开发者 https://www.devze.com 2023-01-26 03:21 出处:网络
i wrote a command line program that shall clean and reorganize our archived server logs by piping line by line to new target files. Each target file has an according regEx filter item so if the line t

i wrote a command line program that shall clean and reorganize our archived server logs by piping line by line to new target files. Each target file has an according regEx filter item so if the line that is red from the sourcefile gets written to this specific target file if it matches the regEx.

I read the regEx strings and their target file strings from a config file and save these information in vectors to be able to dynamically resize them by one with every new taget/filter pair that is red from the config.

The following piece of code shows how I loop through all my source files and for every single of them I read line by line and for every line that could be red I cycle through all the filters defined in the config and if the regEx match the line I write this line to to ofstream. Each time I do this the ofstream gets close()d and clear()ed before I open the new target file in it.

My problem now is that everything works fine except of my target files only contain 1 single string after program ends. It contains the last string that I wrote to the file.

All the strings that I wrote to the file before seem to be overwritten. I think I'm doing something wrong but I don't see what it ist.

Here's the code extract:

    void StringDirector::redirect_all() {
 ifstream input;  //Input Filestream init
 ofstream output; //Output Filestream init
 string transfer; //Transfer string init
 //regex e;

 for (unsigned k = 0; k<StringDirector::v_sources_list.size(); k++) {  //loop through all sources in v_sources_list vector

  cout << endl << "     LOOP through sources! Cycle #" << k << " / string is: " << StringDirector::v_sources_list[k] << endl;

  input.close();  //close all open input files
  input.clear();  //flush
  input.open(StringDirector::v_sources_list[k].c_str());  //open v_sources_list[k] with input Filestream
  if (!input) {
   std::cout << "\nError, File not found: " <<  StringDirector::v_sources_list[k] << "\nExiting!";  //Throw error if file cannot be opened
   exit(1);
  }
  cout << endl << "     " << StringDirector::v_sources_list[k] << " opened" << endl;
  getline(input, transfer); //get a first line from input Filestream and write to transfer string
  while (input) {  //do that as long as there is input
    for (unsigned j = 0; j<StringDirector::v_filters_list.size(); j++) {  //loop through all filters in v_filters_list vectord
     cout << endl << "     LOOP through filters! Cycle #" << j << " / string is: " << StringDirector::v_filters_list[j] << endl;
     regex e(StringDirector::v_filters_list[j]);
     if (regex_search(transfer, e)) {
      reopen(output, StringDirector::v_targets_list[j].c_str());
      output << transfer << endl;
      cout << endl << "          -- MATCH! Writing line to: " << StringDirector::v_targets_list[j] << endl ;
     }
    }
    getline(input, transfer);
    if (input )cout << endl << "+ got another line: " << transfer << endl;
    else cout << endl << "End Of File!" << endl;
  }
 }
}

EDIT:

i forgot the reopen function I use

    template <typename Stream>
void reopen(Stream& pStream, const char * pFile,
       开发者_高级运维     std::ios_base::openmode pMode = ios_base::out)
{
    pStream.close();
    pStream.clear();
    pStream.open(pFile, pMode);
}


Try "append" open mode for your file, I guess it will be ios_base::app (see reopen function, 3rd argument).

std::ios_base::out | std::ios_base::app


You need to turn on append mode in this method by adding std::ofstream::app

input.open(StringDirector::v_sources_list[k].c_str());

should become

input.open(StringDirector::v_sources_list[k].c_str(), std::ofstream::app);

by default the mode is std::ofstream::out which starts at the beginning and overwrites everything else.

Source

0

精彩评论

暂无评论...
验证码 换一张
取 消