Jump to content
  • Advertisement
Sign in to follow this  
supagu

set EOF

This topic is 4955 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!