![](/img/trans.png)
[英]Using pipe() and fork() to read from a file and output to the console/new file
[英]Read from file, output in a new file, and compare outputs
我的代码需要一点帮助。 代码的第一部分很容易创建。 从两个文本文件中读取 -> Output 在一个新的文本文件中。 我的下一步是逐行比较这两个输出。
例子:
Textfile1:
2221112221
1122221112
2222221111
1111111111
Textfile2:
2222221111
2211222212
1111111111
2221112222
Result that should happen on Textfile3:
(like I'm overlapping textfile2 on textfile1)
2222221111
2211222212
1111111111
2221112222
所以我已经研究了str.compare
,但我无法 output 如上所述的结果。 我不知道这是否适用。
if((line2.compare(0, line2.length(), line1)) == 0)
string line1, line2;
ifstream read1, read2;
read1.open("test.txt", ios::in);
read2.open("test2.txt", ios::in);
ofstream outFile("outputfile.txt", ios::out | ios::app);
if(!read1 || !read2){
cerr << "One file is missing" << endl;
exit(1);
}
else {
while(getline(read1, line1))
{
outFile << line1;
}
while(getline(read2, line2))
{
outFile << line2 << endl;
}
非常感谢你们!
迭代字符串并比较字符与字符? 这将使您能够实现您正在寻找的东西。
这个例子显然没有异常处理(例如不同的字符串长度):
std::string corn = "I eat Corn!";
std::string rice = "I eat Rice!";
int main()
{
for (size_t i = 0; i < corn.length(); i++)
{
if (corn[i] != rice[i])
{
rice[i] = corn[i];
}
}
std::cout << rice;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.