我面临着使用c ++逐行读取输入文件的问题

问题描述 投票:0回答:1

我编写了以下代码来逐行阅读文本:

#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可以在记事本中打开,则可以强制打开。

c++ string file-handling
1个回答
1
投票

这些错误实际上告诉您什么地方出了错。

© www.soinside.com 2019 - 2024. All rights reserved.