Sign in to follow this  
supagu

set EOF

Recommended Posts

supagu    148
lets say i have a file with 2 lines in it, but ai want to reduce the file size to only a single line, i want to set the EOF bit after the first line. is this possible? or must i make a copy of the file? using std::ofstream if possible

Share this post


Link to post
Share on other sites
snk_kid    1312
reducing a file to single line is not going reduce the size of the file (okay except maybe for 1 byte), and EOF == end of file so you don't wont to set the first line with that! you wont to remove or replace EOL == end of line character,

if you really must do this then you can use this:


#include <cstdlib>
#include <algorithm>
#include <iterator>
#include <string>
#include <fstream>
#include <iostream>

const char EOL('\n'), WS(' ');

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

if(argc == 1) {
std::cerr << argv[0] << " error usage: " << argv[0] << " <file-name>\n";
return EXIT_FAILURE;
}

std::ifstream ifs(argv[1]);

if(!ifs) {
std::cerr << argv[0] << " error could not open file\n";
return EXIT_FAILURE;
}

std::string buf;

std::replace_copy(std::istreambuf_iterator<char>(ifs),
std::istreambuf_iterator<char>(),
std::back_inserter(buf), EOL, WS);

ifs.close();

std::ofstream ofs(argv[1]);

std::copy(buf.begin(), buf.end(), std::ostreambuf_iterator<char>(ofs));

ofs.close();

}


Oh no you don't have to make a temp file [smile]

[Edited by - snk_kid on November 24, 2004 6:41:29 AM]

Share this post


Link to post
Share on other sites
supagu    148
um i think you got confused over what i said. heres another example:

lets say i have a file with 50 characters in it.
now i want to reduce the size of the file to say 20characters,

if i put the EOF marker after 20 characters, will the OS reduce the size fo the file when i close it, or will it be left the same size after i close it.

OR must i make a copy of the file (either on disk or in memory) and re-write it out?

(nothing to do with EOL ;) )

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this