+ 4
FILE HANDLING
I am working on files in c++.But a problem is occurred first I searched for specific id in file and got the right one and the search is working but I want to change specified data in specific line.I want my program to ask for id and then search for id after searching it asks for the new name that is to be change for that id.Then it changes the old name by a new name but the program is not working I have spent 5 hours but still not working.Kindly tell me a way so that I can make this working.Thanks. Code given below: I have used dev c++ for pc.... https://code.sololearn.com/cI9X7bzN0r2r/?ref=app
18 Answers
+ 4
You can write whole file into vector, delete file, search for "ID" position, change specific element of the vector (probably Id's position+1), create new file (with the same name), and then write the vector to the file again. It may be not efficient, but it is an easy way to achieve your goal
+ 4
Martin Taylor Thanks for your answer.Your program looks a bunch of difficult to me.I am a newby in programming.I am learning c++ in my bachelor's degree and I am not used to it but I loved file handling so I started to search about it and I got an idea to make this program.I don't know about vectors and still learning classes so maybe if you can tell me a more easy way the way I have made my program.Yeah you are right that my code should not be in main.I will use struct then use some functions to do this job but the same problem I would face "How can I update a specific data in program and delete it".The idea you told me about removing old file and copying it to new temp file then exchanging there names seems good to me and easy for my level atleast.And its not an assignment still now I am only learning classes after 6 months I will learn this file handling.So its my own experiment.And facing this simple issue.Hope you will understand my simple maybe illogical question.Thank you.âïž
+ 4
Thanks for your frequent reply's I would learn these new things to me in future as you told me its essential.So maybe you are a far higher level from me in programming.So I would run your code and learn from it.Thank you for your code and every single drop of knowledge you gave me.Thank you very much.
+ 3
Thank you for replying and taking my question as an important one.I can explain you but I am not able to make program cause I am a bigginer.
The desired program is simple.
I will have a id of integer data time.
after 1 character space first name.
and after 1 character space second name.
Again after 1 character space the money a person have.
So basically I want to change that specified money any time when someone is using atm or using bank program to deposit money.
When depositing money I will change the value and increase it.
While atm will decrease the money.
Basically I am making a database as a text file for a bank and an atm.
Maybe in future I will add another function to edit user data who is already registered to text file.
Like his/her name and id etc.
But for now its enough if you could help.Thank you.
+ 3
But if you can assist me in any kind of way you can show me in a small program that you mentioned in your answers at start,you talked about making a temp file then changing to original name if you show me small example of it i will modify and work on it by putting to functions using structures and a lot more.
+ 2
Martin Taylor Sir thank you very much I appreciate your answer.So I am a biginner and I don't know these complexities while handling files.So can you please mention a program or edit my program which I have mentioned so I can improve??? I would appreciate if you help me with this its my current learning phase in c++ but there is not much about it on google or youtube that's why I asked here.Specifically the program I am working with has no simple explanation on google or youtube.Kindly help me if you can by sharing a program.Thanks.
+ 2
In easy words,the format is:
ID firstname lastname money
ID as integer.
firstname and lastname as string.
And money as double.
+ 2
Shaban Kiyani std::vector is a dynamic container from standard template library (STL), where you do not have to specify its size. I'm not good at explaining things, but you can learn more about it here https://en.cppreference.com/w/cpp/container/vector
There are more containers like map, list etc., each has pros and cons. However if you will continue learning C++, you will soon learn STL
+ 2
Thank you for your information but if I learn step by step I think it would be better.So vectors are far ahead by reference to this step by step learning đ
anyways thank you sir.I really appreciate your reply and I will check your link if I can get even a small knowledge.
+ 1
MichaĆ Doruch Sir I don't even know what vector is file handling seems a simple way to me for a bigginer
0
Your code is stunning.Thanks for this work and time for me.I will implement your code by learning it.Thank you.