Skip to main content
GameDev.net gamedev.net
🔒 Locked

set EOF

Started by supagu Nov 24, 2004 at 6:09 AM 1 replies 700+ views
Original Post
supagu
supagu
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
snk_kid
snk_kid
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]
supagu
supagu
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 ;) )

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.