Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Visual c++ 如何修改文本文件中的文本_Visual C++ - Fatal编程技术网

Visual c++ 如何修改文本文件中的文本

Visual c++ 如何修改文本文件中的文本,visual-c++,Visual C++,我有一个包含以下行的文本文件: C:\Program Files\app\ 我想把它读成这样: C:\\Program Files\\app\\ 我知道如何在VisualC++中读取文件,但是如何在每次创建斜杠时添加SLASH()。 char str[200]; fstream file_op("C:\\path.txt",ios::in); file_op >> str; file_op.close(); 在VB中,你可以使用S

我有一个包含以下行的文本文件:

    C:\Program Files\app\
我想把它读成这样:

    C:\\Program Files\\app\\
我知道如何在VisualC++中读取文件,但是如何在每次创建斜杠时添加SLASH()。
    char str[200];
    fstream file_op("C:\\path.txt",ios::in);
    file_op >> str;
    file_op.close();

在VB中,你可以使用
String.Split()
来替换一些字符,你可能想试试这个或谷歌使用“regex”(现在也不知道propper的用法,但我知道它是用来替换和编辑字符串的)

使用Boost:

#include <boost/algorithm/string/replace.hpp>
#include <fstream>
using namespace std;
int main(int argc, char const* argv[]) {
  string line;
  ifstream file_op("D:\\path.txt");
  ofstream file_out("D:\\out.txt");
  while( getline(file_op, line) ) {
    boost::replace_all(line, "\\", "\\\\");
    file_out << line << '\n';
  }
  // file_op and file_out are closed on exit   
  return 0;
}
#包括
#包括
使用名称空间std;
int main(int argc,char const*argv[]{
弦线;
ifstream文件_op(“D:\\path.txt”);
输出流文件(“D:\\out.txt”);
while(getline(file_op,line)){
boost::全部替换(行“\\”、“\\\”;

文件输出最简单的方法是通过循环:

char newPath[MAX_PATH];
int newCount = 0;
for(int i=0; i < strlen(str); i++)
{

    if(str[i] == '\')
    {
        newPath[newCount++] = str[i];
    }
    newPath[newCount++] = str[i];
}
charnewpath[MAX_PATH];
int newCount=0;
对于(int i=0;i

请注意,您无法就地更改文件。您必须将新字符串写入新文件。我没有使用boost或任何其他库,因为默认情况下,它们不是VisualC++的一部分,您的标签上说,VisualC++需要这些字符串。

是的,在VS中,您拥有每种语言中所有优秀的.NET功能…如字符串函数,regex,等等…即使它没有直接实现,你也可以在Google上找到它的“regex c++”,第一个链接是“…对不起,如果这听起来像是胡扯,但我周围全是白痴!?在另一个问题上与一些混蛋进行了类似的讨论。。。