逐行读取文件并输出第一个列

时间:2016-02-29 16:11:26

标签: c++

我是c ++的新手,我正在尝试在文件中对2个单词(双重和三重)进行字符串搜索,然后打印找到它们的行。有些行只有单词" double"有些人只有“#34; triple"”这个词。

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

我忘了添加我需要打印找到单词的行的第一个元素,我能够找到找到单词的行,但是,它似乎不打印第一个元素文件。

这是我的代码。

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;
}

2 个答案:

答案 0 :(得分:1)

我认为您可以简化代码并编写类似的内容:

#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),将其放在一个字符串中然后,当单词&#34; double&#34;或&#34;三重&#34;在字符串中找到,您尝试从文件中读取ID,同时应从同一字符串中读取它。

答案 1 :(得分:0)

试试这个,

#include "iostream"
#include "string"
#include "sstream"
#include "fstream"
using namespace std;

int main()
{
    stringstream y; string x; int lc=0, id;
    ifstream fin("file.txt");
    while (getline(fin, x))
    {
        ++lc;
        y.str(x);
        y >> id;
        bool d=x.find("double")!=string::npos;
        bool t=x.find("triple")!=string::npos;
        if (d and t)
            cout << "found words double and triple on line " << lc
            << ", id is " << id << endl; 
        else if (d)
            cout << "found word double on line " << lc
            << ", id is " << id << endl;
        else if (t)
            cout << "found word triple on line " << lc
            << ", id is " << id << endl;      
    }
}
相关问题