reading from file

This is a discussion on reading from file within the C++ Programming forums, part of the General Programming Boards category; hi, I save a string value to a file, Now I want use this value again, but null return save ...

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    IR, Iran
    Posts
    103

    reading from file

    hi,

    I save a string value to a file, Now I want use this value again, but null return

    save function
    Code:
    int save_pass(void)
    {
    	/* function variables */
    	FILE *fp ;
    	int i ;
    	/* function process*/
    	fp = fopen("pass", "wb");
    	if(!fp)
    	{
    		return 1;
    	}
    	if(&newPassword)
    	{
    		fwrite(&newPassword, sizeof(string), 1, fp);
    	}
    	return 0;
    }
    comparing two strings
    Code:
    		if (password == newPassword){
    			cout << "corect";
    		}
    		else {
    			cout << "Invalid";
    		}
    load function
    Code:
    int load_pass(void)
    {
    	/* function variables */
    	FILE *fp ;
    
    	/* function process*/
    	fp = fopen("pass", "rb");
    	if(!fp)
    	{
    		return 1;
    	}
    
    	fread(&newPassword, sizeof(string), 1, fp);
    	if(feof (fp))
    	{
    		return 0;
    	}
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    If you're using std::string, you just can't point to it and use a low-level function like fwrite.
    All you'll get say is the pointer to the actual string, and maybe the allocated size of the string buffer.

    Use iostream and something like
    cout << password;
    to begin with, to make sure it works.
    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
    Registered User
    Join Date
    May 2008
    Location
    IR, Iran
    Posts
    103
    Now I'm using these
    Code:
    int save_pass(string opass)
    {
    	ofstream outClientFile( "pass.dat", ios::out );
    	if (!outClientFile)
    	{
    		cerr << "File could not be created" << endl;
    		return 1;
    		exit (1);
    	}
    
    	outClientFile << opass;
    
    	return 0;
    }
    
    string load_pass(void)
    {
    	string ipass;
    	ifstream inClientFile( "pass.dat", ios::in );
    	if (!inClientFile)
    	{
    		cerr << "File could not be Opened" << endl;
    		exit (1);
    	}
    
    	inClientFile >> ipass;
    
    	return ipass;
    }
    Code:
    newPassword = load_pass();
    but one problem, how could I write in file binnery? if I open pass.dat I will see the password

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Writing it out in binary won't make it magically unreadable.
    You have to do that yourself with some kind of encryption and decryption.

    Say
    char temp[100];
    strcpy( temp, opass.c_str() );
    // do something to temp, then fwrite it to a file.

    Something being anything you like.
    Start simple, say adding 1 to each char.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

Tags for this Thread


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