【发布时间】:2020-04-14 22:42:45
【问题描述】:
我已经编写了以下代码来逐行读取文本:
#include <bits/stdc++.h>
using namespace std;
int main()
{
ifstream fin; // input and
ofstream fout; // output
string line; // syntax.
char filename[512];
cout << "Enter the absolute path to the file:";
cin >> filename;
fin.open(filename);
if ( fin.fail() ) {
cerr << "Could not open file " << filename << endl;
exit(1);
}
while (std::getline(fin, line)){
if((line[0] == "H" && line[1] == "E" && line[2] == "T" && line[3] == "A" && line[4] == "T" && line[5] == "M") ||
(line[0] == "A" && line[1] == "T" && line[2] == "O" && line[3] == "M"))
cout << "hello" << endl;
}
}
但是代码显示如下错误:
warning: comparison with string literal results in unspecified behaviour [-Waddress]|
error: ISO C++ forbids comparison between pointer and integer [-fpermissive]|
for the if statement.
如何克服这个错误?除此之外,如果您能告诉我如何读取格式为 .pdb 而不是 .txt 的文件,您将非常感激。如果 .pdb 在强制情况下可以在记事本中打开。
【问题讨论】:
-
即使我使用了#include
和#include ,同样的问题仍然存在。 -
我的评论不是对您问题的回答,而是一般性建议。
-
哦。我是新来的,所以我不知道。谢谢!
-
下次您发布带有错误消息的错误时,请在代码中包含注释,以便我们知道错误在哪里。类似
// <-- Error is here。
标签: c++ string file-handling