Kevin217
Kevin217

Reputation: 752

C++ - Modify a File Without Creating a New File

I'm trying to open a file, and modify some contents inside. My first code looks like this,

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main(int argc, char** argv) {
    int status = 0;

    //error if no filename
    if(argc == 1) {
        cerr << "No file specified. Please specify a filename" << endl;
        status = 1;
    }

    //open a file and modify
    for (int i = 1; i < argc; ++i) {
        string line = "";
        string filename = argv[i];
        ifstream infile;

        infile.open(filename);
        if(infile.fail()) {
            status = 1;
            cerr << filename << ": No Such File Exist" << endl;
        }
        else{
            while(getline(infile, line)) {
                auto index1 = line.find('(');
                auto index2 = line.find(')');
                cout << index1 << "   " << index2 << endl;
                auto itor = line.begin();
                if(index1 != string::npos)  line[index1] = '[';
                if(index2 != string::npos)  line[index2] = ']';
            }
            infile.close();
        }
    }


    return status;
}

I know it's wrong to directly modify line because it won't change the content in the file. Is there a way that I can modify the file directly?(Without creating a new file, and output line to that)

Upvotes: 2

Views: 4873

Answers (2)

Some programmer dude
Some programmer dude

Reputation: 409472

If the data you want to change doesn't change the size of the file in any way (i.e. you're not trying to write data that is longer or shorter than the existing data in the file) then yes it's possible, just overwrite the existing data once you find it, by changing the write position.

If, on the other hand, the data is of a different size, then it's very hard to do (you need to read everything and reposition it within the file), it's much easier to just write a new file and rename it.

Changing one kind of brace to another will not change the size of the file, so then you can easily do it. Just go through the file one character at a time, when you find a character you want to change, set the write pointer to the position of the character and write the new character.

Upvotes: 1

R Sahu
R Sahu

Reputation: 206717

You can:

  1. Store the lines, modified and unmodified, in a std::vector<std::string>.
  2. Close the file.
  3. Open the file in write mode.
  4. Save the lines, stored in the std::vector<std::string> to the file
  5. Close the file.

It will be better to create separate functions for each step.

void readContents(std::string const& filename,
                  std::vector<std::string>& lines)
{
   ...
}

void updateContents(std::vector<std::string>& lines)
{
   ...
}

void WriteContents(std::string const& filename,
                   std::vector<std::string> const& lines)
{
   ...
}

and then call the from main.

int main(int argc, char* argv[])
{
   ...

   string filename = argv[i];
   std::vector<std::string> lines;
   readContents(filename, lines);
   updateContents(lines):
   writeContents(filename, lines):
}

Upvotes: 2

Related Questions