C++:无法将行从输入文件复制到输出文件
C++: Cannot copy line from an input file to an output file
这是一个
家庭作业问题,所以如果你不是我理解的那些的粉丝。这是我的代码:
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
fstream myfile1("datafile1.txt"); //this just has a bunch of names in it
fstream myfile2("cmdfile1.txt"); //has commands like "add bobby bilbums"
ofstream outputFile("outfile1.txt"); //I want to take the "add bobby" command and copy the name into this new file.
string line;
if (myfile1.is_open() && myfile2.is_open()) //so I open both files
{
if (myfile2, line == "add"); //If myfile2 has an "add" in it
{
outputFile.is_open(); //open outputfile
outputFile << line << endl; //input the line with add in it till the end of that line.
}
}
cout << "nPress Enter..."; // press enter and then everything closes out.
cin.ignore();
outputFile.close();
myfile2.close();
myfile1.close();
return 0;
}
问题是,尽管输出文件始终为空。它从不将任何行从 cmdfile1 复制到输出文件中。有谁知道我在这里错过了什么?
尝试更多类似的东西:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream myfile1("datafile1.txt");
ifstream myfile2("cmdfile1.txt");
ofstream outputFile("outfile1.txt");
string line;
if (/*myfile1.is_open() &&*/ myfile2.is_open() && outputFile.is_open())
{
while (getline(myfile2, line))
{
if (line.compare(0, 4, "add ") == 0)
{
outputFile << line.substr(4) << endl;
}
}
}
myfile1.close();
myfile2.close();
outputFile.close();
cout << "nPress Enter...";
cin.ignore();
return 0;
}
相关文章:
- C++文本文件输入
- C++ 中文件输入输出中的长长 v/s int
- 如何修复函数中的 fstream 文件输入以将正确的信息存储在结构数组中?
- 用空格填充 2D 数组(从文件输入)
- 流对象来表示文件输入,然后是标准输入?
- 用精神解析文件输入
- 从文件输入和输出
- 来自文本文件输入 c++ 的向量
- 如何通过键盘或分数文件输入分数
- 了解文件输入和输出,我可以写入文件,但无法从文件打印
- 词频程序 - 文件输入太大?
- 尝试从 csv 文件输入大量数据时出错
- 如何将文件从文件输入到C 中的矩阵中
- 如何从Windows CMD运行可执行文件并将其传递给文件输入的参数参数和重定向
- 如何解析文本文件并使用构造函数中的文件输入来创建对象的容器
- 这是读取文件输入的正确方法吗?
- 如何在 c++ 中使用二进制文件输入/输出读取/写入结构的字符串类型成员
- 如何创建一个具有空值、递减编号系统和.txt文件输入值的二维阵列网格
- C 文件输入流循环问题
- 使用对输入整数的文件输入