简体   繁体   中英

how to iterate over txt file and store lines in variables c++

I am looking for an easier way to get lines from a txt and store them in variables in c++. In my txt file, a group of 3 lines represents 1 idea An example of my txt file is the following: ( contains one idea)

John Smith

cars, beaches, swimming

My name is john and i am a teacher

here is my following code that stores each line in a vector

vector <string> inputText;
    string name, prop, keyword, cont;
    ifstream file;
    string line;
    file.open("input.txt");
if (file.is_open()) {
    string line;
    while (getline(file, line)) {
       inputText.push_back(line);
    }

    file.close();
    name = inputText[0];
    keyword = inputText[1];
    cont = inputText[2];

    idea.setID();
    idea.setProposer(name);
    idea.setKeywords(keyword);
    idea.setContent(cont);
    newIdea.push_back(idea);

}

I am seeking a more efficient to store the lines in variables because i would like to have multiple ideas. using multiple ideas in my current code will result in an extremely long and inefficient code. which idea from my txt file will use up 3 lines. My question is, is there an efficient way to store multiple ideas from a txt file in variables without having to use the index.

EDIT: this is what i mean by long and inefficient code for example say my txt file contains the following

John Smith

cars, beaches, swimming

My name is john and i am a teacher

John apples

dogs, cats, swimming

My name is john and i am a tradsman

John loin

cars, cleaning, zoo

My name is john and i am a fisher

than in my code i would need the following

idea.setProproser(inputText[0];
idea.setKeyword(inputText[1]);
idea.setContent(inputText[2]);
newIDea.push_back(idea);

idea.setProproser(inputText[4];
idea.setKeyword(inputText[5]);
idea.setContent(inputText[6]);
newIDea.push_back(idea);

then if i have say 10 ideas in the file i would need to repeat it a lot of times.

You do not need the vector of strings if you populate the vector of Idea s directly. With minimal changes on your code, you can change the while loop to

std::string name_line;
std::string keyword_line;
std::string content_line;
while (std::getline(file, name_line) && 
       std::getline(file,keyword_line) &&
       std::getline(file,content_line)) {
    Idea idea;
    idea.setID();
    idea.setProposer(name);
    idea.setKeywords(keyword);
    idea.setContent(cont);
    newIdea.push_back(idea);
}

However, you should use a constructor to construct an Idea . If you write a constructor that takes those three strings as parameters then the loop could look like this:

std::string name_line;
std::string keyword_line;
std::string content_line;
while (std::getline(file, name_line) && 
       std::getline(file,keyword_line) &&
       std::getline(file,content_line)) {
    newIdea.emplace_back(name_line,keyword_line,content_line);
}

PS: Even if you stay with the vector of strings, you do not need to write "an extremely long and inefficient code". Just as you use a loop to populate the vector of strings you could use a loop to construct the Idea s:

for (size_t i=0; i< inputText.size(); i+=3) {
    name = inputText[i];
    keyword = inputText[i+1];
    cont = inputText[i+2];
    // ...etc...
}

Always when you think you need to write the same code several times and the only difference is an index, the answer is a loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM