[英]Fstream gives error when reused on a file
I have two text files. 我有两个文本文件。 Original Output:
原始输出:
Log.txt
Joe hello
Joe gargabash
Joe random unnecessary text
Hello
How are you?
Log2.txt is another text file that is initially blank. Log2.txt是另一个文本文件,最初是空白的。
When I run this code, it succesfully copies all of the lines that don't start with Joe. 当我运行此代码时,它成功复制了所有不以Joe开头的行。 However, I want to copy the text back to the original .txt.
但是,我想将文本复制回原始.txt。 When I uncomment out the selection I commented to try to do that, I get errors.
当我取消注释选择时,我发表评论以尝试执行此操作,但出现错误。 Anyone know what i'm doing wrong?
有人知道我在做什么错吗? Thanks so much for reading all of this mess.
非常感谢您阅读所有这些烂摊子。 For clarification, the bool STRINGCONTAINS(int, char, char, int) checks if a char array matches with another char array.
为了澄清起见,布尔值STRINGCONTAINS(int,char,char,int)检查一个char数组是否与另一个char数组匹配。
#include <WinSock2.h>
#include <Windows.h>
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>
using namespace std;
bool STRINGCONTAINS(bool CaseSensitive, //If this is true, we are checking a case sensitive string, if it's false, we're not.
char * input1, // First string [Type: Char Array]
char * input2, //Second String [Type: Char Array]
int MAXSTRINGLENGTH) // Integer representing max possible length of string.
{
if (CaseSensitive)
{
for(int i=0;i<MAXSTRINGLENGTH;i++)
{
if (*input1 == *input2)
{
input1++;
input2++;
} else
{
return 0;
}
}
} else
{
int char1, char2;
for(int i=0;i<MAXSTRINGLENGTH;i++)
{
char1 = *input1;
char2 = *input2;
if (char1 == char2 || char1 == (char2+32) || char2 == (char1+32))
{
input1++;
input2++;
} else
{
return 0;
}
}
}
return 1;
}
int main() {
int input;
char * loadedline = new char[192];
ifstream log;
ofstream templog;
log.open("log.txt");
templog.open("log2.txt");
while(log.getline(loadedline,sizeof(log)))
{
if (!STRINGCONTAINS(0,loadedline,"joe",3))
{
cout << loadedline << endl;
templog << loadedline << endl;
}
}
log.close();
templog.close();
/*ifstream templog2;
ofstream log2;
templog2.open("log2.txt");
log2.open("log.txt");
while(templog2.getline(loadedline,sizeof(templog2)))
{
log2 << loadedline << endl;
}
templog2.close();
log2.close;*/
delete[] loadedline;
cin >> input;
return 0;
}
The error in your program is that you are missing a parenthesis in your log2.close.
程序中的错误是您在
log2.close.
中缺少括号log2.close.
Change it to log2.close();
将其更改为
log2.close();
and it will run! 它将运行!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.