CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2013
    Posts
    4

    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)
        {
         
        }

  2. #2
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: detect itemId and delete that line from file

    Quote Originally Posted by roidbeginner View Post
    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

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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
  •  





Click Here to Expand Forum to Full Width

Featured