File I/O help?

I was wondering how to write a username and password to a file then restart that program and read that profile. Here is what I have so far:

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	string member;

	cout<<"Do you have an account? ";
	cin>>member;
	cout<<endl;

	if(member == "no")
	{
		string name;
		string password;
		string filename;

		cout<<"What do you want your username to be? ";
		cin>>name;
		cout<<endl;

		filename = name + ".bin";
		
		//cout<<filename<<endl;

		ofstream profile (filename, ios::binary);

		cout<<"What do you want your password to be? ";
		cin>>password;
		cout<<endl;


	}

	else
	{
		string name;
		string password;

		cout<<"What is your username? ";
		cin>>name;
		cout<<endl;

		cout<<"What is your password? ";
		cin>>password;
		cout<<endl;
	}

	return 0;
}


- Kyle
Last edited on
You're never doing anything with password. And the else never writes at all. Where are you having trouble at?
I don't know how to go on from here. I don't know how to get the password to write nor when you already have an account, how to read the information and have the program use it.

- Kyle
To write onto a file with text already there, you said the append parameter to true. Not sure where your confusion is on reading
First of all, how do I start to write to a file? Second, how do I set the append parameter to true? Third how do I read that file? Finally, how do I get the program to use the stuff that it just read?

- Kyle
When you go to open a file, the computer first checks to see if that file exists. If it doesn't exist (in the directory you specify), it will create a file with that name.
1
2
3
4
ofstream myfile;   //Creates an output file stream object named myfile
myfile.open ("example.txt");     //Opens, or in this case creates, a file named example.txt
myfile << "Writing this to a file.\n";   //Now this writes to this file. You use the same operator as you would for cout
myfile.close();    //This closes your file. You always want to close the file when you're done with it 


Now with just a slight modification, we can tell your program to append (write to the end of the file).

1
2
3
...
myfile.open("example.text", ios::app);
...


Now if you want to input information from this file, you do it in almost the same way as cin.

1
2
3
4
5
6
7
8
9
10
11
  string line;
  ifstream myfile ("example.txt");
  if (myfile.is_open())      //Checks to make sure the file is indeed open
  {
    while ( myfile.good() )   //Remains true as long as you are not at the end of the file, or eof
    {
      getline (myfile,line);    //This will loop through, assign each line to line var, and cout it. 
      cout << line << endl;
    }
    myfile.close();               //Closes file
  }
Thank you!! Problem solved!

- Kyle
Topic archived. No new replies allowed.