can someone tell me how can i read data from text files line by line and store it on a variable.


here is my the data in my textfile.

Username: blahblah
Password: wewewe

my problem is how can i get the blahblah and wewewe and store it in a variable. so if i make pass = wewewe, user = blahblah. so i can use that user and pass for my login program

6 Years
Discussion Span
Last Post by NathanOliver

well if that is the only data in the text file and there is a space between the : and the info you want then it is pretty simple. You read in the first part of the line into a dummy variable and then the you read in the second part into the variable you want.

string trash, username, password;
ifstream fin("userinfor.dat);
fin >> trash >> username;
fin >> trash >> password;

The reason this works is because the >> operator reads until it see white space. So the first part reads in Username: and then there is only the username left. The same thing goes for the second line. If you have multiple usernames and password then you can also put this in s loop but the data has to be continuous. Thinking of getting info from a file as the opposite of displaying data to the screen.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.