打开文件,用不同的名称保存重复项
open file, save duplicate with different name
这里的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*。
相关文章:
- 保存文件并使控制台不关闭:Visual Studio 2019
- 将值更改为 c++ 的数组并保存文件
- 如何保存文件中的所有数据?C++
- 更改代码::块上的默认保存文件类型
- 使用setW()函数保存文件时,如何获取每个属性的值
- 尝试使用 ostream 保存文件内容时出错
- 如何使用3d的max 2016 SDK(C++)在插件中添加保存文件对话框?
- 如何在不保存文件的情况下制作打印屏幕并将其发送到FTP服务器?我的工作代码将文件保存到HDD
- 视频编写器功能不会使用 OpenCV-3.0.0 保存文件
- 使用fstream明确保存文件,而无需在C 中关闭文件
- 位换档字符串以解码保存文件C
- QFile在程序退出时保存文件,根本不会保存~500MB文件
- 保存文件或文件夹的时间戳
- Fstream不会保存文件中的最后一个单词,也不会从文件中读取
- CFileDialog保存文件的名称为空
- 如何以正确的编码保存文件
- 在动态分配的 char 数组中保存文件的类内容
- 如何在win32中使用GetSave文件名保存文件
- 覆盖保存文件的非常安全的方法
- 除非先使用MS Paint打开/保存文件,否则带有QRCode位图的LoadImage()将失败