-2

I wanted to remove the last char in file and

  • Not writing a new file and rename it

Because the file will grow larger over time (From a 1 byte file to 1 GB file, increase by 1 byte over time (1000000000 rewrites cycle)) and the rewrite will damage the hard disk

Khoo Hao Yit
  • 13
  • 1
  • 4
  • 1
    I don't think there's a different way in c++. I don't know how Unix operator `>>` works, but assuming you're on a Unix system you could do `echo "" >> "filename` with `system` in c++. `>>` will append the char to the file, but I don't know if it rewrites it to be honest. – magicleon94 Sep 16 '17 at 09:26
  • 1
    Possible duplicate of https://stackoverflow.com/questions/9529027/c-delete-last-character-in-a-txt-file – Vinayak Singla Sep 16 '17 at 09:34
  • http://en.cppreference.com/w/cpp/io/basic_ostream/seekp – Artemy Vysotsky Sep 16 '17 at 09:44
  • @VinayakSingla Are copy file data and trunc file and then write the same data to the file damage the hard disk or not? – Khoo Hao Yit Sep 16 '17 at 09:45
  • [truncate(2)](https://linux.die.net/man/2/ftruncate) (Unix/Linux-specific) – myaut Sep 16 '17 at 09:49
  • @ArtemyVysotsky OMG thanks, you saved me a ton of time! <3 – Khoo Hao Yit Sep 16 '17 at 09:51
  • Editing the title to end in "closed" is NOT the way to close a question. Please either delete the question, or make a suitable answer, or indicate the duplicate question. – Yunnosch Sep 16 '17 at 11:03
  • @ArtemyVysotsky Please make an anwer from your comment, so that this a) becomes a Q/A instead of a Q/comment b) gets out of the list of unanswered questions. Sadly, this does not really otherwise qualify for a close-vote. – Yunnosch Sep 16 '17 at 11:08
  • @Yunnosch Idk how to choose comment as answer so I said "closed" because this may be helpful to other people – Khoo Hao Yit Sep 16 '17 at 11:33
  • You cannot choose a commen as an answer, that is why I often ask people to make their answering comment into a question. – Yunnosch Sep 16 '17 at 11:43

1 Answers1

0

You can use

en.cppreference.com/w/cpp/io/basic_ostream/seekp

Artemy Vysotsky
  • 2,374
  • 8
  • 18