打开文件,用不同的名称保存重复项

open file, save duplicate with different name

本文关键字:保存 文件      更新时间:2023-10-16

这里的c++新手您好!!!

我正在开发一个非常简单的程序,打开保存到矢量中的文件,反转每个文件的内容文本,然后保存新文件。我正在使用代码块

示例:

  • 打开"test.txt">
  • 反向线路
  • 创建"m_test.txt">
  • 删除"test.txt">
  • 对矢量中的其他文件执行相同操作

我在以下内容中出错:outFile.open( foundFiles[i].insert(3,"m_"), fstream::in);

" no matching function for call to 'std::basic_fstream::open(std::basic_string&, std::_Ios_Openmode)'| "

here is the code so far:

 string reverseLine(string message);
 string reverseLine(string message)
 {
    string reversed;
    int i = message.length()  -1;
    while (i>=0)
        {
        reversed = reversed + message[i];
        i = i -1;
        }
    return reversed;
 }

 void filesToModify();
 void filesToModify() // put files into vector
 {
 fstream filesInVector;  // files open from the vector
 string  openFLine;      // lines read from the files read from the vector
 fstream outFile;        //files saved after reversing the files
 vector <string> foundFiles;   //vector holding all the files to be reversed

 foundFiles.push_back("e:/file_1.txt");
 foundFiles.push_back("e:/file_2.txt");
 foundFiles.push_back("e:/file_3.txt");
 for (int i=0; i<foundFiles.size(); i++) // read the files inside the vector
        {
            filesInVector.open(foundFiles[i],  fstream::in);
            while (!filesInVector.eof())
            {
            getline(filesInVector,openFLine);
            outFile<<reverseLine(openFLine);
            outFile.open( foundFiles[i].insert(3,"m_"),  fstream::in); // add "m_" to the created file
            foundFiles[i].close();    // close the  file
            std::remove(foundFiles[i]); // remove the original file
           }
        }
  int main()
 {
 filesToModify();
 }

outFile.open( foundFiles[i].insert(3,"m_").c_str (),  fstream::in);

C++过去没有字符串数据类型。它后来被添加到STL(标准模板库(中。这些早期的C++版本使用char*(指向字符的指针(。字符串简单地由一个字符数组表示,以0字符(代码为0的字符(结束。指针包含此数组的第一个字符的地址。

有些函数仍然需要char*而不是字符串。STL类字符串的方法c_str((将STL字符串转换为char*。