0

I want to open a text file only ONCE. Let's say I open a text file, and I ask a user to input something, and I write this into the text file. Then, the user wants to modify something that require all the contents in the file to be rewritten. How to achieve this without closing the file and reopen it again?

phuclv
  • 37,963
  • 15
  • 156
  • 475
la lala
  • 41
  • 2
  • 5
  • Just curious, but why would you want to write something to a text file only to then delete it? – Adrian Mole Dec 03 '19 at 13:30
  • Does this answer your question? [clear data inside text file in c++](https://stackoverflow.com/questions/17032970/clear-data-inside-text-file-in-c) – David Rechtman Dec 03 '19 at 13:31
  • Why do you need to "remove all the contents"? What is the underlying problem that this is supposed to solve? – Some programmer dude Dec 03 '19 at 13:33
  • Look at the *modes* that you can open a file. At least one of the modes will "remove all the contents" and allow you to rewrite at the beginning. – Thomas Matthews Dec 03 '19 at 15:27
  • 1
    @ThomasMatthews the modes only apply when opening the file. The OP wants to open the file, write something, then clear and rewrite the file WITHOUT reopening it – Remy Lebeau Dec 03 '19 at 17:12
  • 2
    Why *not* reopen the file? It's the simplest solution and is guaranteed to work on all systems. What is the problem you have with reopening the file? – Some programmer dude Dec 04 '19 at 07:12

2 Answers2

1

It's only available since C++17 with std::filesystem::resize_file()

namespace fs = std::filesystem;
fs::path p = fs::current_path() / "inputfile";
std::fstream file(p);

// Read file content
std::string str;
std::getline(file, str);

// Truncate file
fs::resize_file::resize_file(p, 0);

Before that it's impossible in standard C++ with fstream. You need to use system-specific code like SetEndOfFile() on Windows or ftruncate() on Linux

See also

phuclv
  • 37,963
  • 15
  • 156
  • 475
  • But will it work if the file is already opened? Will it be portable across platforms (even if the `std::filesystem` namespace exits and is platform independent, the implementation of some functions might not be working the same on all platforms)? – Some programmer dude Dec 04 '19 at 07:11
  • Note that `ftruncate()` operates on a file descriptor, not a `FILE *` stream. If you truncate a file out from under a `FILE *` stream, that ignores any data potentially already buffered by the `FILE *` stream, and it may or may not affect what the actual `FILE *` stream implementation uses as the "current location" of its underlying file. – Andrew Henle Dec 04 '19 at 16:17
0

If a file already exists and you would like to open the file and overwrite all content within the file, I would suggest looking into ios_base::trunc().

Give this a quick read and let me know if you have any questions on using any specific function: http://www.cplusplus.com/reference/ios/ios_base/openmode/

Best of luck!

  • 2
    the OP wants to truncate the content **without** reopening the file. And don't link to cplusplus.com, it has many incorrect information and is often frowned upon on SO – phuclv Dec 04 '19 at 03:43