简体   繁体   中英

replace and write to file c++

I want write code to find words in a file and replace words. I open file, next I find word. I have a problem with replace words.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
   string contain_of_file,a="car";
 string::size_type position;
   ifstream NewFile; 

 NewFile.open("plik1.txt");
while(NewFile.good())
    {
    getline(NewFile, contain_of_file);

    position=contain_of_file.find("Zuzia");  
        if(position!=string::npos)
        {
        NewFile<<contain_of_file.replace(position,5, a );

        }
    }
 NewFile.close();

 cin.get();
 return 0;
  }

How can I improve my code?

  1. lose the using namespace std ;

  2. don't declare the variables before needed;

  3. I think the English word you were looking for was content -- but I am not an English-native speaker;

  4. getline already returns NewFile.good() in boolean context;

  5. No need to close NewFile explicitly;

  6. I would change the casing on the NewFile variable;

  7. I don't think you can write to an ifstream , and you ought to manage how you are going to replace the contents of the file...

My version would be like:

#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>

int main() {
  std::rename("plik1.txt", "plik1.txt~");

  std::ifstream old_file("plik1.txt~");
  std::ofstream new_file("plik1.txt");

  for( std::string contents_of_file; std::getline(old_file, contents_of_file); ) {
    std::string::size_type position = contents_of_file.find("Zuzia");
    if( position != std::string::npos )
      contents_of_file = contents_of_file.replace(position, 5, "car");
    new_file << contents_of_file << '\n';
  }

  return 0;
}

There are at least two issues with your code:
1. Overwriting text in a file.
2. Writing to an ifstream (the i is for input , not output).

The File object
Imagine a file as many little boxes that contain characters. The boxes are glued front to back in an endless line.

You can take letters out of boxes and put into other boxes, but since they are glued, you can't put new boxes between existing boxes.

Replacing Text
You can replace text in a file as long as the replacement text is the same length as the original text. If the text is too long, you overwrite existing text. If the replacement text is shorter, you have residual text in the file. Not good in either method.

To replace (overwrite) the text, open the file as fstream and use the ios::in and ios::out modes.

Input versus Output
The common technique for replacing text is to open the original file for * i *nput and a new file as * o *utput.

Copy the existing data, up to your target text, to the new file.

Copy the replacement text to the new file .

Copy any remaining text to the new file.

Close all files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM