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

example:

here is my the data in my textfile.


example.txt
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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.