Thread: how can i replace someting in a simple text file with its new value

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    1

    Question how can i replace someting in a simple text file with its new value

    i have wrote a client-server program in C. part of this program is dealing with users loggin in with ther id and passwrod. sever opens a file called 'passwords.txt' and ckecks to make sure id and password exist and ten it will send client a message to report successful login. (i've used fscanf to read from file and strcmp to compare)

    now i need to come up with a soloution on how to implement password change in my program. user can request changing password. server should look up for that password same as before and then replace it with the new passwrod.

    i dont have to write the code for this, i only have to propose a soloution. however i need a solotion that is possible to do in C and also efficient to use. what is the best way of lookin for the old password and then replacing it with the new password in the 'password.txt' file?

    can you please help me with this, i am running out of time. i am not an experienced programmer so i would appreciate if you could explain in simple terms. the soloution does not have to be in form of C code, as long as it explains how it can be done and some reference on what code can be used to do it (eg.fprintf etc) its fine.

    i would appreciate any help from you guys. thank you

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well you simply read the file line by line, and copy to a new file
    Code:
    FILE *in = fopen( "password.txt", "r" );
    FILE *out= fopen( "password.new", "w" );
    char buff[BUFSIZ];
    while ( fgets( buff, BUFSIZ, in ) != NULL ) {
      fputs( buff, out );
    }
    fclose( in );
    fclose( out );
    remove( "password.txt" );
    rename( "password.new", "password.txt" );
    This updates the file, but without making any changes.
    Replace the fputs() with your code which detects the old password and replace with new password 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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 19
    Last Post: 05-30-2007, 05:47 PM
  2. Deleting / Changing a record in a text file
    By clearrtc in forum C Programming
    Replies: 9
    Last Post: 08-21-2006, 12:09 AM
  3. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM