Bex
Bex

Reputation: 51

Remove parts of text file C++

I have a text file called copynumbers.txt that I need to delete some numbers after a number while using Example would be a text file containing the following

   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15

each integer should occupy 4 byte spaces.

I want to delete or get rid of number 7 to 15 while keeping 1 through 6 then adding the number 30 to it.

so then the file would keep 1 to 6 and get rid of 7 to 15 then after that I want to at 30 at the end of it.

my new file should look like this

1 2 3 4 5 6 30

my question is how would I do that without overriding the number 1 to 6? because when I use

std::ofstream outfile;
outfile.open ("copynumbers.txt");

it will override everything and leave only 30 in the file

and when I use

ofstream outfile("copynumbers.txt", ios::app);

It will just append 30 after 15 but does not delete anything.

Some of my code:

#include <iostream>
#include <fstream>
using namespace std;
int main() {
    ofstream outfile("copynumbers.txt", ios::app);

    outfile.seekp(0, outfile.end);

    int position = outfile.tellp();

    cout << position;

    //outfile.seekp(position - 35);

    outfile.seekp(28);
    outfile.write("  30",4);

    outfile.close();    

    return 0;
}

Upvotes: 0

Views: 11736

Answers (4)

Jonathan Mee
Jonathan Mee

Reputation: 38919

Would a std::istream_iterator help you here? If you know you just want the first 6 words you can do something like this:

std::istringstream input( "   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15" );
std::vector< int > output( 7, 30 ); // initialize everything to 30

std::copy_n( std::istream_iterator< int >( input ), 6, output.begin() ); // Overwrite the first 6 characters

If you wanted your output tab separated you could do something like this for output:

std::ofstream outfile( "copynumbers.txt" );

outfile << '\t';
std::copy( outfile.begin(), outfile.end(), std::ostream_iterator< int >( outfile, "\t" ) );

Upvotes: 1

WAQ
WAQ

Reputation: 2626

I would recommend read the original file in memory,make the required changes in memory and then write everything out to the file from scratch.

Upvotes: 1

Hodorogea Alexandru
Hodorogea Alexandru

Reputation: 46

You have to use seekp function. Check this out.

http://www.cplusplus.com/reference/ostream/ostream/seekp/

Upvotes: 1

Paul R
Paul R

Reputation: 212969

It's generally a bad idea to try to modify a file "in place" - if anything goes wrong then you end up with a corrupted or lost file. Typically you would do something like this:

  • open original file for input
  • create temporary file for output
  • read input file, process, write to temporary file
  • if successful then:
    • delete original file
    • rename temporary file to original file name

As well as being a safer strategy, this makes the process of modifying the contents easier, e.g. to "delete" something from the file you just skip over that part when reading the input (i.e. just don't write that part to the output file).

Upvotes: 4

Related Questions