problem with fstream

Hey guys. So I am trying to make a simple password diary program.
The program will open password.txt and if there is no password written, he will ask for one and write it there. If there is a password written, he will ask the user to write it.
If this is completed, the program closes password.txt and opens diary.txt. From here it reads to the user what is written. After that the user can write what he wants. The problem is that whenever I write a new password or something in the diary, it does not save. Can anyone help me please? I have been searching wikipedia and forums for hours. Thanks in advance.
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <windows.h>

using namespace std;

int main(int argc, char *argv[])
{
    string pass,passin,texti,texto;
    bool checking=true,right=false;
    
    fstream myfile;
    myfile.open("password.txt");
    {
                                                         getline(myfile,pass);
                                                         if(pass=="")
                                                         {
                                                                     cout << "Enter a new password: ";
                                                                     cin >> pass;
                                                                     myfile << pass;
                                                                     right=true;
                                                                     checking=false;
                                                                     myfile.close();
                                                         }
                                                         else
                                                         {
                                                             while(checking)
                                                             {
                                                                            cout << "Enter password: ";
                                                                            cin >> passin;
                                                                            if(passin==pass)
                                                                            {
                                                                                            system("color a");
                                                                                            system("cls");    
                                                                                            cout << "Access granted.";
                                                                                            system("cls");
                                                                                            Sleep(3000);
                                                                                            checking=false;
                                                                                            right=true;
                                                                                            myfile.close();
                                                                            }
                                                                            else
                                                                            {
                                                                                system("color c");
                                                                                system("cls");
                                                                                cout << "Access denied."; 
                                                                                Sleep(3000);
                                                                                system("cls");
                                                                            }
                                                             }   
                                                         } 
    }
    while(right)
    {
                fstream myfile;
                myfile.open("diary.txt");
                getline(myfile,texto);      
                cout << "Current text in diary:\n" << texto << ".\n\nWhat do you want to write today?:\n";
                cin >> texti;
                myfile << texti;
                right=false;
                cout << endl;
                myfile.close();
    }                                         
    system("PAUSE");
    return EXIT_SUCCESS;
}


To let you know, I did create both password.txt and diary.txt in the root folder.
To let you know, I did create both password.txt and diary.txt in the root folder.

You need to put these files in the same directory as your executable program.

This may or may not be the same as the folder where your source code resides.
Yes. All .exe and the .txt files are in the same folder. Doesn't work.
Ok, That was the simple theory.

Now things get more interesting. There are various things that can cause the status of a file to change. There are various flags you can test, including myfile.fail(), myfile.good() and myfile.eof().

You will need to check the status after each action. Firstly, was the file opened successfully? So check good() immediately after opening. If necessary, print out a suitable error message.

We'll assume the file opened correctly. Now what happens when the password is read? If the file is empty, the file will enter an error state. You can clear this by using myfile.clear() before attempting to write out the new password.

In the case of the diary.txt file, there may be the eof() (end of file) or fail() conditions to handle. You may also want set the file to write new text to the end, by using myfile.seekp (0,ios_base::end);

See the reference section for more details,
http://www.cplusplus.com/reference/iostream/ostream/seekp/
http://www.cplusplus.com/reference/iostream/ios/good/
etc.
Last edited on
Topic archived. No new replies allowed.