VC++ file I/O help.
Results 1 to 7 of 7

Thread: VC++ file I/O help.

  1. #1
    Senior Member
    Join Date
    Jan 2004
    Location
    Hawaii
    Posts
    351

    Question VC++ file I/O help.

    Okay, I have this program in VC++ 6. It consists of 2 dialogs as of now. The first has you provide a username and password for future logins. The second just verifies that you typed your password correctly. If the two match, then it saves to a file called 'userssave.ska' which is nothing more than simple text as of now. If they don't match, you're supposed to get a dialog box stating so. Neither of these are happening...can someone give me some help?

    I will post that particular function here, and include the entire project for download.
    Code:
    void PassConf::OnCheckpass() 
    {
    
    	ofstream userssave("c:\\usersave.ska", ios::trunc, ios::binary); 
    	if (m_passwordconf != ((CWRITERS_BLOCKDlg*)parent)->m_password) //they don't match
    	{
    		MessageBox("The passwords do not match.", "Invalid Password", MB_ICONEXCLAMATION);
    		
    	}
    	else
    	{
    		userssave << ((CWRITERS_BLOCKDlg*)parent)->m_username << endl << flush;
    		userssave << ((CWRITERS_BLOCKDlg*)parent)->m_password << endl << flush;
    		userssave.flush();
    		OnOK(); //exit this dialog
    	}
    }

    The file attached does not include the contents of the debug folder, as that is huge, so you will have to compile it on your own.


    [EDIT]
    I have since updated the function, but to no avail:
    Code:
    void PassConf::OnCheckpass() 
    {
    
    	ofstream userssave("c:\\usersave.ska", ios::trunc, ios::binary); 
    	if (m_passwordconf != ((CWRITERS_BLOCKDlg*)parent)->m_password) //they don't match
    	{
    		MessageBox("The passwords do not match.", "Invalid Password", MB_ICONEXCLAMATION);
    		
    	}
    	else
    	{
    		userssave << (LPCTSTR)((CWRITERS_BLOCKDlg*)parent)->m_username << endl << flush;
    		userssave << (LPCTSTR)((CWRITERS_BLOCKDlg*)parent)->m_password << endl << flush;
    		userssave.flush();
    		userssave.close();
    		OnOK(); //exit this dialog
    	}
    [/EDIT]
    Geek isn't just a four-letter word; it's a six-figure income.

  2. #2
    Senior Member
    Join Date
    Jul 2002
    Posts
    339
    Hi, I'm not doing C++ that much anymore but...

    A.
    if (m_passwordconf != ((CWRITERS_BLOCKDlg*)parent)-&gt;m_password) //they don't match

    I believe you're comparing two strings. If so, I think you need to use a string compare function (like strcmp() in C).

    B.
    userssave.flush();

    I think you also need userssave.close();

    Peace always,
    &lt;jdenny&gt;
    Always listen to experts. They\'ll tell you what can\'t be done and why. Then go and do it. -- Robert Heinlein
    I\'m basically a very lazy person who likes to get credit for things other people actually do. -- Linus Torvalds


  3. #3
    Senior Member
    Join Date
    Jan 2004
    Location
    Hawaii
    Posts
    351
    Yea, I was told that when using CStrings, you can just use !=, ==, =, instead of the string functions. i have also added userssave.close();, but nothing happens.
    Geek isn't just a four-letter word; it's a six-figure income.

  4. #4
    Senior Member
    Join Date
    Jul 2002
    Posts
    339
    Does usersave.ska get created? What happens if you change

    userssave &lt;&lt; ((CWRITERS_BLOCKDlg*)parent)-&gt;m_username &lt;&lt; endl &lt;&lt; flush;
    userssave &lt;&lt; ((CWRITERS_BLOCKDlg*)parent)-&gt;m_password &lt;&lt; endl &lt;&lt; flush;

    to

    userssave &lt;&lt; "user:" &lt;&lt; endl &lt;&lt; flush;
    userssave &lt;&lt; ((CWRITERS_BLOCKDlg*)parent)-&gt;m_username &lt;&lt; endl &lt;&lt; flush;
    userssave &lt;&lt; "pass:" &lt;&lt; endl &lt;&lt; flush;
    userssave &lt;&lt; ((CWRITERS_BLOCKDlg*)parent)-&gt;m_password &lt;&lt; endl &lt;&lt; flush;

    Peace always,
    &lt;jdenny&gt;
    Always listen to experts. They\'ll tell you what can\'t be done and why. Then go and do it. -- Robert Heinlein
    I\'m basically a very lazy person who likes to get credit for things other people actually do. -- Linus Torvalds


  5. #5
    Senior Member
    Join Date
    Jan 2004
    Location
    Hawaii
    Posts
    351
    When I do that, the file says:
    user:

    pass:

    That's it, no strings attached, literally.
    Geek isn't just a four-letter word; it's a six-figure income.

  6. #6
    Senior Member
    Join Date
    Jan 2004
    Location
    Hawaii
    Posts
    351
    By George, I've got it! ahahah.
    thanks you guys for your help, but i found the simple problem
    i never called UpdateData(true) to save the strings to the variable....
    stupid careless errors.

    A_T
    Geek isn't just a four-letter word; it's a six-figure income.

  7. #7
    Senior Member
    Join Date
    Jul 2002
    Posts
    339
    Well, that was a very basic debugging practice.

    By adding those "debugging code" you know that:
    - the file creation and writing works perfectly
    - it doesn't write the value of the variables as expected

    That gives you enough clue to check why their contents are empty.

    BTW, it wasn't that "nothing happens" as you suggested. It does create the file and write the static strings to the file, but it won't write the content of the variables. They are different things.

    Peace always,
    &lt;jdenny&gt;
    Always listen to experts. They\'ll tell you what can\'t be done and why. Then go and do it. -- Robert Heinlein
    I\'m basically a very lazy person who likes to get credit for things other people actually do. -- Linus Torvalds


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides