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
4/10/2021 2:51:49 PMShaban Kiyani
18 AnswersNew Answer
Here is an example that performs basic deposit and withdrawal operations on the data using a temporary file as discussed earlier. https://code.sololearn.com/ca17A23349A9 NOTE: The GNU compiler in the code playground issues a warning about the use of the tmpnam function and recommends using mkstemp instead. The mkstemp function is not part of the standard libraries and is a POSIX function, so it is not available on none POSIX systems. In other words it is not available under Windows. The use of tmpnam is not dangerous as this warning states if used in the way shown. It's only a problem if you use the internal pointer e.g. char * pname = tmpnam(NULL); since this returns a pointer to an internal static buffer, which is not thread safe.
Unless you are going to use fixed length fields and handle the file as a sequence of random access records you can't just change a line in a text file. To edit a line in a text file you must read each line in and write it to a new file. When you find the line you want to edit you edit the string then add that line to the new file. You then continue writing the rest of the lines to the new file. Once everything is in the new file you delete the original file then rename the new file to the original name. Alternative strategies include reading the entire file one line at a time into a list, or some other data structure, editing the record in memory then writing everything back to the original file, overwriting the old contents. Plain text files are sequential files and not intended for random access. I have no idea why someone would downvote this question. It's an interesting question regarding a common real world programming problem and you have provided your code for comment. An explanation of the text file format would have been helpful though.
Your code lacks any explanation of the task or the file format. I'd be more than glad to help if you provide the necessary information. It appears that your code is attempting to read the id and name from the file. Are the IDs numbers? do the names contain spaces? Is the data comma separated, fixed width or just separated by a space? Without this information I would be guessing. an example of the file format could be <integer ID><whitespace><string name> e.g. 123456 Thingy however it could also be any of the following ABC123-5 My amazing doodah ABC123-5, "My amazing doodah" 12 Object1 4567 Another Name I can't give a solution unless the problem is known.
Shabin Kiyana, that is the simplified version. I can't make it any simpler than that. I got carried away with the version on my computer. I was having fun creating an Account class to perform operations on the data and I used a list container to hold the account classes. This is how things are done in C++. My test code ended up being able to print the data tables sorted by the various fields and was able to perform searches. It went a bit too far for a simple file read demo. So I posted the simplified version. Note that all of the code in the createDataFile() function is simply to generate data for use in the SoloLearn code playground. It can be deleted if all you want to do is test it with your own file on your own machine. The readDataFile() function is where the reading takes place.
O.K I get the file format. The first problem with your code is that the file is sequential, not random access. Databases require random access to the file data. Secondly your read loop uses is_open on the file. The is_open member function returns whether the stream is currently associated to a file, so will return true even if you attempt to read past the end of a file. This will result in an error. Thirdly everything you are doing is in the main function. It is a "God Function" i.e. a single function that is trying to do everything. This is a seriously bad design. If this is a homework assignment or challenge then please provide the exact description of the task required otherwise I could take you down a more complex path than is required. If this is just your own project for educational purposes then we need to talk about software design and make some decisions on the direction your project will take. Usually the first thing to think about is the user interface. a simple text menu will usually suffice. The menu would require options for adding user accounts, closing user accounts, changing names, making withdrawals, and making deposits. The following is an example of student database record system I wrote in response to a question a while ago. https://code.sololearn.com/ca09A16a303A/#cpp This system uses a menu interface but works on a vector of student records in memory. Don't try to use this in the code playground as it is an interactive program. It should compile without any problems on your system though. A similar system could be used for what you require with a few modifications. You would need to read in the text file on startup and write it out at the end, or after any modifications if you prefer. Throw in a few adjustments for the menu and it would be done.
Shaban Kiyani, a vector is like an array that can grow or shrink as needed. List are just lists that can have things added to them or removed from them. These container classes, and others are essential to being productive in C++.
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
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.☁️
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.
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.
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.
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.
In easy words,the format is: ID firstname lastname money ID as integer. firstname and lastname as string. And money as double.
Here is a quick demo of reading a file of data such as you have described. https://code.sololearn.com/ca2080a07a17 It also demonstrates writing to a file as it creates a file of test data for use in the SoloLearn Code Playground. For a program to do as you wish it will be necessary to store the data read into a container class of some sort rather than just print it to the screen. You can then perform deposit and withdrawals on the data. Writing the data back to file when you are done.
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
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.
Michał Doruch Sir I don't even know what vector is file handling seems a simple way to me for a bigginer
Your code is stunning.Thanks for this work and time for me.I will implement your code by learning it.Thank you.