简体   繁体   English

替换并写入文件c ++

[英]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 ; 丢失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; getline已经在布尔上下文中返回NewFile.good()

  5. No need to close NewFile explicitly; 无需显式关闭NewFile

  6. I would change the casing on the NewFile variable; 我将更改NewFile变量的NewFile

  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... 我不认为您可以写入ifstream ,而应该管理如何替换文件的内容...

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. 1.覆盖文件中的文本。
2. Writing to an ifstream (the i is for input , not output). 2.写入ifstreami用于输入 ,而不用于输出)。

The File object File对象
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. 要替换(覆盖)文本,请以fstream格式打开文件,并使用ios::inios::out模式。

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. 替换文本的常用技术是打开* i * nput的原始文件,并打开* 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. 关闭所有文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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