Click to See Complete Forum and Search --> : search a file


f15e
04-04-2002, 09:59 PM
I am writing a program that gets a login ID from a user and then a password. Within a file is login ID's and its password in this form:

abc1234 logcabin
def5678 computer
etc.........

I will open the file, inFile.open("idfile.txt"); I have already prompted the user to enter their login ID and password. Now I want to search the file to validate that the ID and password is within the idfile file. What do I use to search a file for the matching ID and password until it is either found or not found in which case I will display a message to the user that the info. was invalid.
Would I use getline() or whatever function and a loop until the ID and password is found?
What is the best thing to use to search a file until whatever you are looking for is found?

dighn
04-04-2002, 10:21 PM
an easy way is to use a loop in which you extract the string, compare to login. if equal you extract the next string in the same line which is the password, else you getline() and go to the next iteration

to extract a string you use the >> operator for std::string which is used like this "the_file_object >> a_string;" it stops at tabs and spaces and enters.