-1

How do I change, replace or deleting a line in a text file using c++?

I have a text file that contains login information for users ( username and password ) , for example :

//file 
Jimmy jim1236
tom  tommy545

Now how can I write a program that allows users to change their own password after they log into the system? I have already done the login part.

Samuel Harmer
  • 4,264
  • 5
  • 33
  • 67
Blue Sky
  • 29
  • 4
  • 2
    That doesn't strike me as a very secure way to store passwords, especially if the file is going to be readable by everyone. – NPE Dec 21 '11 at 12:50
  • 3
    Off topic: Storing plaintext passwords == bad idea. Implementing your own authentication == bad idea. – ArjunShankar Dec 21 '11 at 12:51
  • 3
    Implementing something expected to be used for 'securing' something when still a beginner to C++ == bad idea. – ArjunShankar Dec 21 '11 at 12:52
  • 1
    This is just an assignment for the uni , so far I have managed to allow users to register themselves and log into the system , but now the teacher has asked me to allow them to change their passwords too , the security is not that much important to me , I'm just stuck because I can't do this without damaging the source file and I don't know what codes I have to use to replace a line in a text file , any help is really appreciated , thanks – Blue Sky Dec 21 '11 at 13:37

3 Answers3

2

You cant' do it directly without problems: you can't remove information from file, you will have problems with replacing strings by longer ones in file directly an so on.

You may load the file contents in memory, manage the information with your requirements and save the information in correct way.

Maybe you need an User class to fill, an User container to manage all information and class to encapsule file access and container filling.

Tio Pepe
  • 3,071
  • 1
  • 17
  • 22
2

Try the following pseudo-code:

Open the source file
Open a temporary file
Read a line from the source file

While Not EOF Do
    Search the line for the user name
    If found, replace the password in the line
    Write the line to the temporary file
    Read a line from the source file
Wend

Close the source file
Close the temporary file
Delete the source file
Rename the temporary file to the source file name.
Abel
  • 56,041
  • 24
  • 146
  • 247
Martin James
  • 24,453
  • 3
  • 36
  • 60
  • But what happens to the rest of the data which is stored in the file ?? I'm gonna lose them all this way , aren't I ? – Blue Sky Dec 21 '11 at 13:31
  • No you're not, because you write all lines from the source file to the temporary file. The "If found" only applies to the replace, not to the Write. – zennehoy Dec 21 '11 at 14:17
  • Not working anyway , I actually did it my own way , it works but it always replaces the first line , for example if I want to change the third line , it keeps the third line but instead it changes the first line ???!! – Blue Sky Dec 21 '11 at 14:50
  • @pouya: Of course it works, if you implement the structure above correctly ;) Your code might have had an error. If you have problems with your new actual code, show what you got (possibly in a new question) and we see if we can help. – Abel Jan 16 '12 at 18:01
-3

1/ open the file with write mode (fopen)

2/ go the wanted position (fseek)

3/ write at the current position (fwrite)

4/ close the file (fclose)

rlods
  • 465
  • 2
  • 6