简体   繁体   中英

How to read a specific amount of lines in a file

I have a school project where I want to make a password holder(nothing special I will attach all the code down below) and I don't know how to read a specific amount of lines from a text file. More exactly I need the user to input the name of the account(something that can describe that account) and using that input I need to search in the file for the name and then print next 3 lines.

#include <iostream>
#include <fstream>
#include <stdlib.h>

using namespace std;

int main(){
    string choice;
    string allAccounts;//here i save all the files befor makeing a new txt file
    string accName;
    string email;
    string username;
    string password;
    string accInfo;
    string accountName;
    string textAccountInfo;

    while(true){
        cin>>choice;
        if(choice=="add"){

            cout<<"Account name(this is used to find your account):";
            cin>>accName;
            cout<<"Email:";
            cin>>email;
            cout<<"Username:";
            cin>>username;
            cout<<"Password:";
            cin>>password;

            email="Email:"+email;
            username="Username:"+username;
            password="Password:"+password;

            fstream accSaveFile1("accSaveFile");
            while(getline(accSaveFile1,accInfo)){
                allAccounts=accInfo+"\n";
            }



            allAccounts+=accName+"\n"+email+"\n"+username+"\n"+password+"\n\n";
            ofstream accSaveFile2("accSaveFile.txt");
            accSaveFile2<<allAccounts;
            accSaveFile2.close();
        }
        else if(choice=="read"){
            cout<<"What account you want to see(use the account name)?\n";
            cin>>accountName;
            ifstream accReadFile("accSaveFile.txt");
            while (getline(accReadFile,textAccountInfo)){
                if (textAccountInfo==accountName){
                    for(int i=1;i<=3;i++){
                        cout<<textAccountInfo<<endl;//That is what I have tried but while the code in the if statement is running the while loop doesn't give me new values 
                    }
                }
            }
        }
        else if(choice=="quit"){
            break;
        }
        else if(choice=="help"){

        }
    }
}

The problem is that your for(int i=1;i<=3;i++){ loop doesn't read anything at all.

All you need to do is add getline to that loop. Something like this

while (getline(accReadFile, textAccountInfo)) {
    if (textAccountInfo == accountName) {
        for (int i = 1; i <= 3 && getline(accReadFile, textAccountInfo); i++) {
             cout << textAccountInfo << endl;
        }
        break; // quit while loop
    }
}

You didn't say what you wanted to do after you had read the three lines. I assumed you wanted to quit the file reading loop, so I added a break; statement.

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