Nirbhay Tandon
Nirbhay Tandon

Reputation: 318

Read File Line by line and output the first coloumn

I am new to c++ and am trying to do a string search for 2 words(double and triple) in a file and print the line on which they are found. Some lines only have the word "double" and some only have the word "triple".

The code doesn't seem to output the words, it just prints the last line at the end of the loop.

I forgot to add that I need to print the first element of the line on which the word is found, I am able to locate the line where the word is found, however, it doesnt seem to print the first element on the file.

Here is my code.

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

Upvotes: 0

Views: 148

Answers (2)

anukul
anukul

Reputation: 1962

Try this,

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

Upvotes: 0

Bob__
Bob__

Reputation: 12779

I think you can simplify your code and write something like this:

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

One of the problems with your code is that you first read a line of the input file (with getline), put that in a string and then, when the word "double" or "triple" is found in the string, you try to read the ID from the file while you should read it from the same string.

Upvotes: 1

Related Questions