You are here:

C++/file handling in c++


1st of All Happy New Year Sir!...My question is about file handling i want to make a data of users entering there user name and password and storing that to a file.txt....then after that adding data related only to that username ..after adding data that should be stored in that file..when user again comes and gives his username and password so after comparing if it comes to be true i mean if both are matching then program should display it's data.
i am a beginner to c++ and has just reached topic file handling i know about files like what streams should be used and how to open or close file and how to append or edit....but i just want to know how to register a new user and then storing data of that specific user.
sorry for such a long question i will be very thankful if u would answer this and satisfy me.

Hi Irshad, thank you for the question.

It sounds to me like you need a struct for your User and associated data in code. Here is a snippet of some pseudo code you can use to get started:

struct User
string name, password;
int someData;

// somewhere in code
string name, pword;
int data;
cout << "Enter the user name: ";
cin >> name;
cout << "Enter the password: ";
cin >> pword;

// open the file
ifstream in("nameOfFile.txt");
cout << "Failed to open the data file" << endl;

bool foundUser = false;
string tempName, tempPass;
int tempData;
in >> tempName;
in >> tempPass;
in >> tempData;
if(tempName == name)
User user; = tempName;
user.password = tempPass;
user.someData = tempData;

// Check the matching passwords here also
// and perform security and error checking

That is how I would approach the problem. Please let me know if you have any other questions.

I hope this information was helpful.

- Eddie


All Answers

Answers by Expert:

Ask Experts




I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by I also know a good deal about graphics via OpenGL, and GUIs.


I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2017 All rights reserved.