打开文件,用不同的名称保存副本 你好C++新手!p>
我正在开发一个非常简单的程序,它打开保存到向量中的文件,反转每个文件的内容文本,并保存新文件。 我正在使用代码块 例子:打开文件,用不同的名称保存副本 你好C++新手!p>,c++,file-io,C++,File Io,我正在开发一个非常简单的程序,它打开保存到向量中的文件,反转每个文件的内容文本,并保存新文件。 我正在使用代码块 例子: 打开“test.txt” 反向线 创建“m_test.txt” 删除“test.txt” 对向量中的其他文件执行相同的操作 我在以下方面出错: outFile.open(foundFiles[i].insert(3,“m”),fstream::in) “没有用于调用'std::basic_fstream::open(std::basic_string&,std::_Ios
- 打开“test.txt”
- 反向线
- 创建“m_test.txt”
- 删除“test.txt”
- 对向量中的其他文件执行相同的操作
outFile.open(foundFiles[i].insert(3,“m”),fstream::in)
“没有用于调用'std::basic_fstream::open(std::basic_string&,std::_Ios_Openmode)'的匹配函数”
以下是迄今为止的代码:
字符串反向线(字符串消息);
字符串反向线(字符串消息)
{
字符串反转;
int i=message.length()-1;
而(i>=0)
{
反向=反向+消息[i];
i=i-1;
}
反向返回;
}
void filesToModify();
void filesToModify()//将文件放入向量
{
fstream fileinvector;//从向量打开的文件
字符串openFLine;//从向量读取的文件中读取的行
fstream outFile;//反转文件后保存的文件
vector foundFiles;//包含所有要反转的文件的vector
foundFiles.push_back(“e:/file_1.txt”);
foundFiles.push_back(“e:/file_2.txt”);
foundFiles.push_back(“e:/file_3.txt”);
对于(int i=0;i
C++没有字符串数据类型,它是在STL(标准模板库)中添加的。这些早期的C++版本使用字符(字符指针)。字符串只由字符数组表示,由0字符(字符0)终止。指针保存了该数组的第一字符的地址。
有些函数仍然需要char*而不是字符串。
STL类字符串的方法c_str()将STL字符串转换为char*
C++没有字符串数据类型,它是在STL(标准模板库)中添加的。这些早期的C++版本使用字符(字符指针)。字符串只由字符数组表示,由0字符(字符0)终止。指针保存了该数组的第一字符的地址。
有些函数仍然需要char*而不是字符串。
STL类字符串的方法c_str()将STL字符串转换为字符*对于较旧的标准,请使用std::string::c_str()
作为std::ofstream:open()
的参数传递。请解释,先生,我还不习惯使用指针:)读取应该有帮助。此处不需要任何Further解释。对于较旧的标准,使用std::string::c_str()
作为std::ofstream:open()
的参数传递。请解释,先生,我还不习惯使用指针:)读取应该有帮助。此处不需要任何Further解释。
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);