在文件上重用时,Fstream 会出错
Fstream gives error when reused on a file
我有两个文本文件。原始输出:
Log.txt
Joe hello
Joe gargabash
Joe random unnecessary text
Hello
How are you?
Log2.txt 是另一个最初为空白的文本文件。
当我运行此代码时,它成功地复制了所有不以 Joe 开头的行。但是,我想将文本复制回原始.txt。当我取消注释掉我评论的选择以尝试这样做时,我收到错误。有人知道我做错了什么吗?非常感谢您阅读所有这些混乱。为了澄清起见,bool 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;
}
程序中的错误是log2.close.
中缺少括号
将其更改为log2.close();
,它将运行!
相关文章:
- 访问者访问变体并返回不同类型时出错
- 在Linux for Windows上编译C++代码时出错
- 读取文件的最后一行并输入到链接列表时出错
- 重载操作程序时出错>>用于类中的字符串 memebr
- 调用专用模板时出错"no matching function for call to [...]"
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- 在fstream期间忽略空行
- LINK 尝试使用 OpenSSL evp aes 256 c++ 时出错
- 在Google Kick start中提交时出错
- 在c++中访问int到类对象的映射时出错
- 分段错误当我试图运行程序时出错
- 使用dynamic_cast和构造函数时出错
- CHECK(调用)函数在Google Colab中出错
- 用pybind11包装C++抽象类时出错
- 为x86而非x64编译时出错
- 从R调用C++函数并对其进行集成时出错
- 使用ofstream或fstream-open不会出错,也不会打开文件
- 在文件上重用时,Fstream 会出错
- "No such file or directory" 使用 makefile 时在 c++ 中执行简单 fstream 时出错
- std::getline函数(fstream _Lock())出错