Jump to content
  • Advertisement
Sign in to follow this  
polisasimo

Ifstream ?

This topic is 4891 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. When reading data from a .txt file is it possible to reset the pointer where the file is being read. Perhaps a visual aid will help clarify the questtion. I currently use the: getline(ifile, temp_line, ' '); ifile.ignore(1000, '\n'); to get the username from a txt data file ... When I find a username that matches the user input, I then want to read the whole line as a string, if possible. I have a bool function that compares the usernames already written, so when I find a match I break out of the search loop. Usernames Passwords jon rxhjk bill jdislso george idsisl tom hjkhks Any ideas would be helpful.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by polisasimo
When I find a username that matches the user input, I then want to read the whole line as a string, if possible. I have a bool function that compares the usernames already written, so when I find a match I break out of the search loop.

Usernames Passwords

jon rxhjk
bill jdislso
george idsisl
tom hjkhks



I think you mean:


#include <algorithm> // find
#include <iterator> // istream_iterator
#include <string> // string
#include <fstream> // ifstream
#include <iostream> // cout

int main() {

typedef std::istream_iterator<std::string> isstr_itr;

std::ifstream ifs("temp.txt");

isstr_itr result, end;

if((result = std::find(isstr_itr(ifs), end, "george")) != end) {
std::string usr_name(*result), password;
ifs >> password;
std::cout << "username: " << usr_name << ", password: " << password << std::endl;
} else
std::cout << "user not found";

}


If the format of the file is exactly like that then you don't really need to read in the whole line.

If you want to know how to move the ifstream "get pointer" look up std::basic_istream::seekg method.

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!