Saving my Password in a file

This is a discussion on Saving my Password in a file within the C++ Programming forums, part of the General Programming Boards category; Hi. I am trying to safe a password but I fail because he don't seems to read it, I can't ...

  1. #1
    Banned Yuri's Avatar
    Join Date
    Aug 2005
    Location
    Breukelen, The Netherlands
    Posts
    133

    Saving my Password in a file

    Hi.
    I am trying to safe a password but I fail because he don't seems to read it, I can't figure it out. Here is some code:

    Code:
    First of I gave it some kind of 'default' setting:
    
    #include <fstream>
    #include <iostream>
    #include <math.h>
    #include <windows.h>
    
    using namespace std;
    
    //here is some other code, not necessary now
    
    char ThePassword[] = "password";
    
    int main()
    {
        ifstream A ( "Startup.txt" );
        A.close();
    //here is some other code, not necessary now
    }
    
    then later in the code you can change the password in a option ( if
     you want to change the password )
    
    //here is some other code, not necessary now
    
        ofstream B ( "Startup.txt" );
        B<< ThePassword<<endl;
        B.close();
    
    //here is some other code, not necessary now
    
    but the problem is he doesn't seem to read the file he wrote, first
     he get's the 'default' password then he read's the file, so if there
     isn't a file, he wont read it, then you can give it an other password
     he save's that then if you close program and open up again he
     read's 'default' password but then he read's the file that now Does 
    exist so the ThePassword should get a other value but he doesn't 
    read the file and I don't know why, please help me.
    ( if there's isn't enough code plz tell me, i can add some more, thank you )

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    Well unless you post the code which tries to read and compare the password, it could be anything.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Banned Yuri's Avatar
    Join Date
    Aug 2005
    Location
    Breukelen, The Netherlands
    Posts
    133
    Here is my source, hopes that it would help you:
    Last edited by Yuri; 08-07-2005 at 07:29 AM.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    I'm not rummaging in 17K of source code just for that.
    Practice with a small test program to help you figure out the answer, and if you're still stuck post your test code, not your main code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Quote Originally Posted by Salem
    I'm not rummaging in 17K of source code just for that.
    Practice with a small test program to help you figure out the answer, and if you're still stuck post your test code, not your main code.
    I agree. Jeez the source has no indentation what-so-ever (I was going to try and help...).

    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  6. #6
    Banned Yuri's Avatar
    Join Date
    Aug 2005
    Location
    Breukelen, The Netherlands
    Posts
    133
    The only problem is, is that if you changed your password he write's a file with the new password so if you return to the begin and he ask for your password and enter the new password it works but doesn't make any sence, if you change your password and he wrote on a file and then quit the program (shut it down) and open it for the next time (so you should have a new password) he read's the file (just like I said in the first post) and should use the new password but he doesn't, that's my problem if you insert a string like "test" for a new password he write's it into the file as just: test he can read that if you don't quit the program but it seems he doesn't read it if you quit the program and start the program again as a "save the new password for next time I use this program". Maybe there is something wrong with the write file function:

    ofstream B ( "Startup.txt" );
    B<< ThePassword<<endl; //ThePassword is a string for... the password
    B.close();

    or with my read file function:

    ifstream A ( "Startup.txt" );
    A.close();

    (I don't get compile error's!)
    I can't figure it out why he can't read it as a new password, maybe one of you know, thx anyway.
    Last edited by Yuri; 08-07-2005 at 07:46 AM.

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    >> or with my read file function: <<

    Yes, indeed. The problem, of course, would be that you open the file, and then close it. Something should happen between those two events, namely, reading the password stored in the file. If you are using std::string, then have a look here: http://cppreference.com/cppstring/getline.html
    and, for c-style strings: http://cppreference.com/cppio/getline.html
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  8. #8
    Banned Yuri's Avatar
    Join Date
    Aug 2005
    Location
    Breukelen, The Netherlands
    Posts
    133
    Sorry but I really don't get it ( I am a beginner at C++ ) and the site too.
    Last edited by Yuri; 08-07-2005 at 08:58 AM.

  9. #9
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Try reading the "C++ Text File Input & Output" tutorial on my tutorial page for some instruction on how to properly read/write text files in C++. See my sig for the link.

    Good luck to ya.

  10. #10
    Banned
    Join Date
    Jun 2005
    Posts
    594
    how does a beginner not fimilar with anything,
    come up witha 17k source file.

  11. #11
    Banned Yuri's Avatar
    Join Date
    Aug 2005
    Location
    Breukelen, The Netherlands
    Posts
    133
    I think I started wrong, some (easy) things I already should know I don't know and the other way around. I still can upload it if you want but it isn't a big (I don't mean it in size) thing. Oh yeah I would like to thank LuckY (for the tutorial), this was my fix (if you would like to know):
    Code:
        A.getline(ThePassword, 100).good();
    Last edited by Yuri; 08-07-2005 at 04:10 PM.

  12. #12
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Basically, your problem is this (from what I've seen so far):
    Code:
    ifstream A ( "Startup.txt" ); // Opening the file...
    ? // Something missing here... namely, reading data...
    A.close(); // Closing the file...
    Take a look at the links I posted. Using getline is a very good method of reading in strings (particularly c-style strings where you have to worry about buffer length).

    *edit*
    Alright, I see you've figured it out. The only reason you'd call good( ) is to check the status, but it needs to be the conditional of an if statement.

    Cheers
    Last edited by Zach L.; 08-07-2005 at 04:14 PM.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  13. #13
    Banned Yuri's Avatar
    Join Date
    Aug 2005
    Location
    Breukelen, The Netherlands
    Posts
    133
    Ok thx, still I find it hard to read the links you gave (yes I am a noob)). But I got another problem:
    Code:
    If I write this: 
    
        ofstream C ( "Startup.txt" );
        C<< ThePassword<<endl;
        C<< TheConfirmCode<<endl;
        C.close();
    
    and read it like this: 
    
        ifstream A ( "Startup.txt" );
        
        if ( A.is_open() ){
        A.getline(ThePassword, 100);
        A.close();
        }
    
    and: 
    
        ifstream B ( "Startup.txt" );
        
        if ( B.is_open() ){
        B.getline(TheConfirmCode, 100);
        B.close();
        }
    
    he will read the same as ThePassword because he just read the
    first line, how can I get it like he will read the line under the first line
    or that he Doesn't read the first 5 characters and start reading after
    the first 5 and stop reading after the Size of the ThePassword, that
    would really help, thank you.

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    Don't open a second file (B), just call getline a second time with A before you close it.

  15. #15
    Banned Yuri's Avatar
    Join Date
    Aug 2005
    Location
    Breukelen, The Netherlands
    Posts
    133
    Hmm it doesn't seem to help if I do this it doesn't work:
    Code:
    If I write this: 
    
        ofstream B ( "Startup.txt" );
        B<< ThePassword<<endl;
        B<< TheConfirmCode<<endl;
        B.close();
    
    and read it like this: 
    
        ifstream A ( "Startup.txt" );
        
        if ( A.is_open() ){
        A.getline(ThePassword, 100);
        }
    
    and: 
    
        ifstream A ( "Startup.txt" );
        
        if ( A.is_open() ){
        A.getline(TheConfirmCode, 100);
        A.close();
        }
    
    Note:
    
        ifstream A ( "Startup.txt" );
        
        if ( A.is_open() ){
        A.getline(ThePassword, 100);
        }
    
    Is located in int Password()
    and:
    
        ifstream A ( "Startup.txt" );
        
        if ( A.is_open() ){
        A.getline(TheConfirmCode, 100);
        A.close();
        }
    
    is located in int ConfirmCode()
    
    Maybe that is why it doesn't work but I don't know, and still I can't
    figure it out how to read After a few characters in a file with
    getline() or something.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. saving screen to file
    By z0diac in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 02-23-2003, 06:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21