Remove parts of text file C++

こ雲淡風輕ζ 提交于 2020-01-30 03:29:51

问题


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;
}

回答1:


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).




回答2:


You have to use seekp function. Check this out.

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




回答3:


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.




回答4:


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" ) );


来源:https://stackoverflow.com/questions/26177738/remove-parts-of-text-file-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!