C++实现txt文件读到另一个txt文件,并替代其中默写特定字符

问题:出现fopen出现错误的原因:在预处理器中添加“_CRT_SECRE_NO_WARNINGS”

参考:http://jingyan.baidu.com/article/ce436649fd61543773afd32e.html

问题:将一个文件读取和写入做了一下午终于解决了:code如下:

ifstream myfile("F:\1.txt");  

ofstream outfile("F:\Pdataout.txt", ofstream::app);

 string temp;

 if (!myfile.is_open())  {   cout << "未成功打开文件" << endl;  }

 while (getline(myfile, temp,' '))  {   outfile << temp <<endl;  }  myfile.close();  return 0;

问题:本来读入一个文件无法实现换行,想了好多方法都无法实现,静下心来:发现用问题出在getline(myfile, temp,' '))  ‘ ’实现换行就结束。

问题:C++代码一次读取文本文件全部内容到string对象 

 ifstream in("readme.txt", ios::in);  istreambuf_iterator<char> beg(in), end;  string strdata(beg, end);  in.close(); 

即可将readme.txt的全部内容读取至字符串对象strdata中。

问题:将string全部字符替换

string::size_type pos = 0;
 string s2 = "|";
 string s3 = " "; 
 string::size_type a = s2.size();
 string::size_type b = s3.size();
 while ((pos = line.find(s2, pos)) != string::npos)
 {
  line.replace(pos, 1, s3);
  pos += b;
 }

问题:将string读入txt文件

outfile << line << endl;

问题:屏幕出现闪退的问题

添加#include <stdlib.h>和system("pause");

问题:c++ 无法查找和打开PDB文件

工具->选项->常规(打开源服务器)->打开符号miscrosoft 源服务器;

如果还是不行,添加getchar()或者system“pause” 

原文地址:https://www.cnblogs.com/cyychenyijie/p/5916302.html