c++ File I/O
I'm building a program that can store passwords for people when they use more than one password like me (and forget alot of them). anyway i need to figure out how to take the text from a file and be able to …
- copy from the file
- decrypt the text (the file is encrypted to keep unwanted viewers)
- print the text in the console.
if you know how to do at least step 3 let me know please. i've been looking for the last 3 days and cannot find anyway to do it.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string pass;
cout << " Password saver Version 1, By Yours31f.\n\n\n";
cout << "Please enter your password: ";
cin >> pass;
if (pass == "letmein")
{
cout << "Access Granted\n";
ofstream myfile;
myfile.open ("pass.pswd");
menu:
cout << " Welcome, What would you like to do? \n\n 1. View passwords. \n 2. Add passwords \n";
int menu;
cin >> menu;
if (menu == 1){
// need to read it here
system("pause");
}
if (menu == 2){
log:
string location, username , password;
cout << "Whats the Location for the user/pass?";
cin >> location;
cout << "Whats the Username?";
cin >> username;
cout << "Whats the Password?";
cin >> password;
myfile << "location: "<<location<< " .\n";
myfile << "Username: "<<username<< " .\n";
myfile << "Password: "<<password<< " .\n";
cout << "Do you need to store another password? \n 1. Yes \n 2. No\n\n";
int again;
cin >> again;
if (again == 1){
goto log;
}
if (again == 2){
system("pause");
myfile.close();
return 0;
}}}
else
{
cout << "Access Denied\n";
system("pause");
return 0;}
}
What do i need to add to do that?
here's an example of reading and writing to files.
you need to have this stuff at the head of your program of course:
#include <string>
#include <fstream>
using namespace std;
void getStuff();
void outputStuff(string out);```
then here's the output to file part:
```markup//sends stuff to the data file
void outputStuff(string out)
{
ofstream outfile;
outfile.open("data.dat",ios::out);
if (outfile)
{
outfile<<out;
}
else
{
cout<<"An error occurred while opening the file.\n";
}
outfile.close();
}```
and here's the read from file part
```markup//retrieves stuff from the data file
void getStuff()
{
ifstream infile;
string s="", s1="";
infile.open("data.dat",ios::in);
if (infile)
{
while (!infile.eof())
{
getline(infile, s1);
s+=s1;
}
infile.close();
}
else
{
cout << "An error occurred while opening the file.\n";
}
}```
i dont know if that's what you're looking for.
ynori7 wrote: here's an example of reading and writing to files.
you need to have this stuff at the head of your program of course:
#include <string>
#include <fstream>
using namespace std;
void getStuff();
void outputStuff(string out);```
then here's the output to file part:
```markup//sends stuff to the data file
void outputStuff(string out)
{
ofstream outfile;
outfile.open("data.dat",ios::out);
if (outfile)
{
outfile<<out;
}
else
{
cout<<"An error occurred while opening the file.\n";
}
outfile.close();
}```
and here's the read from file part
```markup//retrieves stuff from the data file
void getStuff()
{
ifstream infile;
string s="", s1="";
infile.open("data.dat",ios::in);
if (infile)
{
while (!infile.eof())
{
getline(infile, s1);
s+=s1;
}
infile.close();
}
else
{
cout << "An error occurred while opening the file.\n";
}
}```
i dont know if that's what you're looking for.
could you help me edit this so it would fit in with my code since i really don't get how you set this code up.
yours31f wrote: could you help me edit this so it would fit in with my code since i really don't get how you set this code up.
I think it's better if you learn it instead of making others make such a simple application for you ;)
Links: http://www.google.com/search?hl=en&q=C%2B%2B+file+io http://www.cplusplus.com/doc/tutorial/files.html http://www.cprogramming.com/tutorial/lesson10.html
hey i finally got a great place for it.
http://www.gamedev.net/reference/articles/article1127.asp
check it out and see what you think
ok i have a problem, i cant see why it will not read the line.
txt file:
Yours31f
letmein
script:
#include <fstream>
#include <iostream>
using namespace std;
int main () {
ifstream fin("pass.pswd");
char username[101];
fin >> username;
char pass[101];
fin >> pass;
cout << username << " " << pass;
system("pause");
}
your declaration of the infile object is wrong.
you put: ifstream fin("pass.pswd";
you want to have: ifstream infile; infile.open("data.dat",ios::in);
look back at the sample i posted. i would reccommend using an if statement to make sure that the program successfully loaded the file. You also ought to use while(!infile.eof()) which makes it keep searching line by line in the file until it reaches the 'end of file' (that's what eof stands for).
also, you really ought to use getline()'s in your program to get the data from the file because the way you're doing it, you're just taking the info from the file and then it just sits there on the stack. if you use a getline() you can store it into a string.