Jump to content
  • Advertisement
Sign in to follow this  
polisasimo

strings and ifstream?

This topic is 4828 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello all I am stuck!! I am trying to read data from a txt file. The format of the file is two colums with different usernames and thier respective passwords seperated by a space. I need to compare an existing string to the username and is it matches read the next word and compare it to an existing password string. it looks somewhat like this: string username, password, temp_word; cin >> ... My problem is reding the text file and setting the string temp_word = to the first "word" in the file, so I may compare them. any help would be much appreciated... so far i have kicked around some getline ideas but nothing seems to work out for me :)

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
std::ifstream in("my.txt");
std::string desiredname = "hi";
std::string txtname, txtpassword;
while(in >> txtname && in >> txtpassword)
{
if(txtname == desiredname && txtpassword == mypassword)
{
//name and password found
}
}

Share this post


Link to post
Share on other sites
Maybe this will help explain my problem, this has been my best idea thusfar:


bool password()
{
bool verified = true;
char next_char;
string password, user_name, temp_line;
ifstream ifile;

user_name = get_username();
password = get_password();
ifile.open("password.txt");
do
{
ifile.get(next_char);
if(next_character != '\r')
{
cout << (next_character);
temp_line += next_character;
}
}while(next_character != '\r');


return verified;//just a stub for now
}






I will then compare using a simple compare string function. The problem is I can not read the txt file to the temp line string.

Share this post


Link to post
Share on other sites
The line ending might not be \r.

And line-at-a-time reading functionality is provided:


ifile.open("password.txt");
getline(ifile, temp_line); // 'getline' is a free function in namespace std

Share this post


Link to post
Share on other sites
This is the error that led me down the path to the previous code above

getline(ifile, temp_line);

c:\Documents and Settings\Owner\My Documents\Visual Studio Projects\p1150\p1150.cpp(41): error C3861: 'getline': identifier not found, even with argument-dependent lookup

I did not recognize it so... I tried to make the code work in another way.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!