fstream

Pages: 12
hi there,
Currently im creating a login program. im using fstream to do this program, so i save my data in a text file. Im able to save data into the file, in the file you will see something like:
username:password:level

when i want to retreive the data from it, i will face a problem that:

ifstream login(FileName);
login >> username >> password >> level;

i'll get a result:
username = username:password:level
password = null
level = null (means nth)

so how i gonna solve this problem?
sorry for the weak English skill.
if they are all strings you can try this:
1
2
3
getline(login,username,':');
getline(login,password,':');
getline(login,level,':');
instead of >>
i have tried what you have said but i get error:
login undeclared identifier

this is my code:

1
2
3
4
5
6
ifstream login("filename");
ifstream.open("filename");
getline(login,username,':');
getline(login,password,':');
getline(login,level,':');
ifstream.close();


what wrong with it? please correct me thx alot.
On lines 2 and 6 you have to use login instead of ifstream
Are you including all the required headers?
Thx alot i have solve the problem. now i facing a new problem =P

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cout << "Enter username to search for password: ";
cin >> search;
ifstream login;
login.open("filename");
getline(login,username,':');
getline(login,password,':');
getline(login,level,':');

while (username != search)
{
getline(login,username,':');   <== i have tried put a "cout << username;" and get the same value with my "search" but still continue looping.
getline(login,password,':');
getline(login,level,':');
}

login.close();


and i get a result is non-stop looping...why?
in line 9.. that is not the way to compare std::string.. please see this http://cplusplus.com/reference/string/string/compare/

and what is the data type of your search variable?
my search is string.

and i have change my code to :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cout << "Enter username to search for password: ";
cin >> search;
ifstream login;
login.open("filename");
getline(login,username,':');
getline(login,password,':');
getline(login,level,':');

while (!login.eof() && username.compare(search) != 0)   <== changed this
{
getline(login,username,':');
getline(login,password,':');
getline(login,level,':');
}

login.close();


but i still cant get the pass it will search through all the data and dont get the username.
maybe the username has spaces on it.. getline(cin, search) to get input.. in line 2
while (username != search) is perfectly fine
http://cplusplus.com/reference/string/operators/
How did you declare search?
@Bazzy
oh yeah.. i forgot it has overloaded operators.. my mistake..
How did you declare search?
string search;

Thx for the help i have done those part.

Here come a new problem i get:
1
2
3
4
5
6
cout << "Username: ";
getline(cin,username);
cout << "Password: ";
getline(cin,password1);
cout << "Comfirm Password: ";
getline(cin,password2);


above is the code i use to create account. but when i run till this part i will get output like this:
1
2
Username: Password: abc
Comfirm Password: abc


'abc' is i key in myself but why i cant key in username?
Last edited on
show us more of code.. maybe you can give a sample text file.. i didn't understand your last post..
Are you mixing >> and getline with cin?
My code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
                              ifstream Create;
					Create.open("filename");

					cout << "New username: ";
					getline(cin,user);

					while (! Create.eof() && user.compare(username) != 0){
						getline (Create, username, ':');
						getline (Create, x);
						if (user.compare(username) == 0){
							cout << "The username already exist!" << endl;
							cout << "Please try again later!" << endl;
							system("PAUSE");
							return(0);
						}
					}
					Create.close();

					cout << "Password: ";
					getline(cin,pass1);
					cout << "Comfirn Password: ";
					getline(cin,pass2);

					count = 0;
					while (pass1.compare(pass2) != 0){
						cout << endl << endl << endl <<"THE PASSWORDS ARE NOT SAME!" << endl;
						cout << "PLEASE KEY IN AGAIN!" << endl;
						cout << "Password: ";
						getline(cin,pass1);
						cout << "Comfirn Password: ";
						getline(cin,pass2);
						if (count > 1){
							cout << "Please CHECK your password!" << endl;
							system("PAUSE");
							return(0);
						}
						count = count + 1;
					}


after my program run to this part, i will get an output:
New Username: Password:

The program will skip enter username part and ask for password. Hope you understand my sad English.
Last edited on
try to add this code cin.ignore(256); on top of cout << "New username: "; if it works Bazzy must be right, you're mixing >> and getline()
Last edited on
err i added cin.ignore(256); but when the program come to this part it will get nth,
it wont print New Username: and others. Just hang there but i can type smth @@

what do you mean by mixing >> and getline()
means that when i used getline() i can't use cin>> x; anymore ? ?
please see this..
http://www.cplusplus.com/forum/articles/6046/


and can you post the your whole code.. i can't guess your problem.
hi, its me again =)...
i have solve the problems and now i want to get a time for salting.
but all the website example are in printf( .... )

can show me how to get the time format?
Example 12:20:30(hh:mm:ss) i want to change it into format like this:
122030 just numbers ignore :

what i have tried is:
1
2
3
char times;
_strtime(times)
Saving << username << ":" << password << ":" << ("%h%m%s",times) << endl;     <= this part is fstream i wan to save into text file.


with the code above i will get time format hh:mm:ss and still apear :
hope you understanding my problem...
I assume you got the time in a tm pointer:
1
2
3
4
char times[7];
times = strftime ( times, 7, "%H%M%S",  mytm );

Saving << username << ":" << password << ":" << times << endl;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <stdio.h>
#include <time.h>
using namespace std;

int main() {
    time_t current_time = time(NULL);
    tm*  timex = localtime( &current_time );
    cout << "hour: "   << timex->tm_hour << endl;
    cout << "minute: " << timex->tm_min << endl;
    cout << "sec: "    << timex->tm_sec << endl;
    return 0;
}


http://cplusplus.com/reference/clibrary/ctime/tm/
http://cplusplus.com/reference/clibrary/ctime/time/
http://cplusplus.com/reference/clibrary/ctime/localtime/
Last edited on
Pages: 12