Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

c++ File I/O


yours31f's Avatar
Retired
10 0

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 …

  1. copy from the file
  2. decrypt the text (the file is encrypted to keep unwanted viewers)
  3. 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.


yours31f's Avatar
Retired
10 0
#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?


ghost's Avatar
0 0

Dude, forget all of that shit. Store your passwords in a .txt file, and hide it from plain sight, and you'll be fine.

attrib +h +s filename.txt

xD


ynori7's Avatar
Future Emperor of Earth
0 0

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.

yours31f's Avatar
Retired
10 0

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's Avatar
Retired
10 0

i agree but couldn't find a source.


Uber0n's Avatar
Member
0 0

Uhm… You couldn't find a tutorial for C++ File I/O?

Geez :right:


yours31f's Avatar
Retired
10 0

no i found a tutorial but not one that works like i need it to.


ynori7's Avatar
Future Emperor of Earth
0 0

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.

it's already set up into nice little methods for you. all you need to do is just copy and paste them into your program and give them return values.


yours31f's Avatar
Retired
10 0

ok cool. be back in 10 mins.


yours31f's Avatar
Retired
10 0

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");
}

ynori7's Avatar
Future Emperor of Earth
0 0

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.