Search code examples
c++filevectorfinderase

Searching a vector for a string, and then find the position of the string in c++


I am trying to erase a string from a text file. To do this, I want to read the file into a vector, then I want to search for the position of this string, so I can use vector::erase to remove it. After the string has been erased from the vector, I can write the vector into a new file.

So far, I have made all of that, but finding the position of the string. I've found all sorts of solutions using < algorithm > 's std::find, but those answers were trying to check if this string exists, not its position.

Here is an example of how the text file is set up. With a string, followed by an integer, followed by .txt without spaces. Each string is on a newline.

file123.txt
Bob56.txt'
Foo8854.txt

In this case, the vector would be "file123.txt", "bob56.txt", "Foo8854.txt".

This is the code I have made already:

std::vector<std::string> FileInVector;
std::string str;
int StringPosition;

std::fstream FileNames;
FileNames.open("FileName Permanent Storage.txt");
while (std::getline(FileNames, str)) {
  if(str.size() > 0) {
      FileInVector.push_back(str); // Reads file, and this puts values into the vector
  }
}

//This is where it would find the position of the string: "bob56.txt" as an example

FileInVector.erase(StringPosition); // Removes the string from the vector
remove("FileName Permanent Storage.txt"); // Deletes old file
std::ofstream outFile("FileName Permanent Storage.txt"); // Creates new file
for (const auto &e : FileInVector) outFile << e << "\n"; // Writes vector without string into the new file

Solution

  • Below is the working example. There is no need to store the string into a vector or search for the position of the string inside the vector because we can directly check if the read line is equal to the string to be searched for, as shown.

    main.cpp

    #include <iostream>
    #include <fstream>
    
    int main()
    {
        
        std::string line, stringtobeSearched = "Foo8854.txt";
    
        std::ifstream inFile("input.txt");
        
        std::ofstream outFile("output.txt");
        if(inFile)
        {
            while(getline(inFile, line, '\n'))        
            {
                std::cout<<line<<std::endl;
                //if the line read is not same as string searched for then write it into the output.txt file
                if(line != stringtobeSearched)
                {
                    outFile << line << "\n";
                }
                //if the line read is same as string searched for then don't write it into the output.txt file
                else 
                {
                    std::cout<<"string found "<<std::endl;//just printing it on screen
                }
                
                
            }
        }
        else 
        {
            std::cout<<"file could not be read"<<std::endl;
        }
        inFile.close();
        outFile.close();
        
       
        return 0;
    }
    
    

    input.txt

    file123.txt
    Bob56.txt'
    Foo8854.txt
    file113.txt
    Bob56.txt'
    Foo8854.txt
    file223.txt
    Bob96.txt'
    Foo8814.txt
    

    output.txt

    file123.txt
    Bob56.txt'
    file113.txt
    Bob56.txt'
    file223.txt
    Bob96.txt'
    Foo8814.txt