-
October 14th, 2013, 03:10 AM
#1
detect itemId and delete that line from file
I have a text file name fruit.txt that contains the following information of fruit id, fruitName and fruitQuantity.
Code:
1:pear:30
2:apple:20
3:banana:24
4:orange:15
5:watermelon:35
If let's say I key in 2, it will search for the id=2 and delete the whole line and the id of banana which is orignially 3, will become 2 and orange which is orignally 4 will become 3 etc.
I researched on how it can be done and most suggested to put inside a vector and fout the line.
I know how to put the values in the vector but I have no idea how should I go about searching for the id and if the id is found it will delete that line inside the file. and thus need help.
Code:
ofstream fout;
ifstream readFile("fruit.txt");
while (getline(readFile, line)) {
istringstream iss(line);
iss >> fruitId >> fruitName >> fruitQuantity
fruitConstructor fruitDetails(fruitId,fruitName,fruitQuantity);
fruitVector.push_back(fruitDetails);
}
cout << "Enter fruit id to remove" << endl;
cin >> input;
//duno how I should write to delete the id of the line that
matches my input
for(vector<fruitVector>::iterator it = fruitVector.begin(); it != fruitVector.end(); ++it)
{
}
-
October 14th, 2013, 09:45 AM
#2
Re: detect itemId and delete that line from file
Originally Posted by roidbeginner
I researched on how it can be done and most suggested to put inside a vector and fout the line.
Well, it depends on what skills you have to demonstrate with this assignment. In case this all about STL containers, vector would be fine.
However, this can be implemented a pure procedural way alright. No vector required to read file line by line, parse the line, and just skip it to put onto the resultant file once particular id is met.
I know how to put the values in the vector but I have no idea how should I go about searching for the id and if the id is found it will delete that line inside the file. and thus need help.
Again, you need to parse every line for its elements, one of them is the id you are to test.
Best regards,
Igor
-
October 14th, 2013, 11:05 AM
#3
Re: detect itemId and delete that line from file
What is the definiton of fruitConstructor? It would be prefable if you could post all the code rather than just a small snippet. Are the ids in the file always consecutive and start at 1? If so, there's no need to store the ids in the vector and to find a specific id, just index into the vector.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|