Sign in to follow this  
polisasimo

strings and ifstream?

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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this