gpt4 book ai didi

c++ - 逐行读取文件并输出第一列

转载 作者:行者123 更新时间:2023-11-28 05:48:46 24 4
gpt4 key购买 nike

我是 C++ 的新手,我正在尝试对文件中的 2 个单词(双重和三重)进行字符串搜索,并打印找到它们的行。有的只有“双”字,有的只有“三”字。

代码似乎没有输出单词,它只是在循环结束时打印最后一行。

我忘了补充一点,我需要打印找到单词的行的第一个元素,我能够找到找到单词的行,但是,它似乎没有打印第一个元素文件。

这是我的代码。

int main(int argv, char *argc[]){
const string filen("test.txt");
ifstream inFile(filen.c_str());
string line = "";

char IDList[10];
string ID = "";
char* double_word = "double"; // test variable to search in file
char* triple_word = "triple";
stringstream ss;

string word = "";
unsigned int currentLine = 0;

// iterate through each line and check if the words double or triple exist.

while(getline(inFile, line)){
currentLine++;
if (line.find(double_word) != string::npos) {

cout << "found the word \"double\" on line: " << currentLine << endl;
// this part takes the input file and reads the first character of the line i.e. the ID and adds it to the IDList
// string array.
while(inFile >> IDList){
cout << "File Id: " << IDList << endl;
inFile.ignore(numeric_limits<streamsize>::max(), ' ');

for(int i=0;i<10;i++){
ss << IDList[i] << endl;
}
word = ss.str();
}
}
else if(line.find(triple_word) != string::npos){

cout << "found the word \"triple\" on line: " << currentLine << endl;

// now take the id of this file and add it to a different queue.

while(inFile >> IDList){
cout << "File Id: " << IDList << endl;
inFile.ignore(numeric_limits<streamsize>::max(), ' ');

for(int i=0;i<10;i++){
ss << IDList[i] << endl;
}
word = ss.str();
}
}
else if(line.find(double_word) && line.find(triple_word) != string::npos){

cout << "Found both words double and triple in line: " << currentLine << endl;

while(inFile >> IDList){
cout << "File Id: " << IDList << endl;
inFile.ignore(numeric_limits<streamsize>::max(), ' ');

for(int i=0;i<10;i++){
ss << IDList[i] << endl;
}
word = ss.str();
}
}
else{
cout << "neither word found, moving to next line" << endl;
}
}

inFile.close();
cout << "Id's added to the queue" << word << endl;
return 0;
}

最佳答案

我认为你可以简化你的代码并编写如下内容:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>

using std::string;
using std::vector;
using std::cout;
using std::cin;

int main(int argc, char* argv[]) {
const string filen("test.txt");
std::ifstream inFile(filen.c_str());
string line;

string double_word = "double"; // test variable to search in file
string triple_word = "triple";

vector<string> IDs;
unsigned int currentLine = 0;

// iterate through each line and check if the words double or triple exist.

while(getline(inFile, line)){
currentLine++;
bool found_d_word = line.find(double_word) != string::npos;
bool found_t_word = line.find(triple_word) != string::npos;
if ( found_d_word && !found_t_word )
cout << "found the word \"double\" on line: " << currentLine << '\n';
if ( found_t_word && !found_d_word )
cout << "found the word \"triple\" on line: " << currentLine << '\n';
if ( found_d_word && found_t_word )
cout << "Found both words double and triple in line: " << currentLine << '\n';
if ( found_d_word || found_t_word ) {

std::istringstream ss{line};

string ID;
ss >> ID;
cout << "File Id: " << ID << '\n';
// my guess: store all the IDs in one vector
IDs.push_back(ID);

} else {
cout << "neither word found, moving to next line\n";
}
}
inFile.close();
return 0;
}

您的代码的一个问题是您首先读取输入文件的一行(使用 getline),将其放入一个字符串中,然后,当在字符串中找到单词“double”或“triple”时,您尝试从文件中读取 ID,而您应该从同一个字符串中读取它。

关于c++ - 逐行读取文件并输出第一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35704193/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com