0

I cannot save a textfile with c++. It goes through the code with no error. My OS is Windows 11 (22H2). C++ and the g++ compiler have both version 12.1.0.

What I've tried:

  • checking with debugger, but no error was found
  • deactivated Firewall and any other antivirus software
  • checked folder rights, and run the .exe as admin
  • restarted the computer

My code should be OK, otherwise my previous efforts where unnecessary:

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ofstream MyFile("./test.txt");
    MyFile << "Test";
    MyFile.close();

    return 0;
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
  • 3
    You never check if the file opened successfully or not. And if it succeeds, then the file might not be saved in the location you expect. – Some programmer dude Oct 18 '22 at 17:54
  • 2
    First of all, try `ofstream MyFile("./test.txt"); if (MyFile) { MyFile << "Test\n"; MyFile.close(); } else { cout << "Failed to open file\n"; }` – Some programmer dude Oct 18 '22 at 17:59
  • you can check `std::filesystem::current_path` – apple apple Oct 18 '22 at 18:03
  • Also make sure that the [Current Working Directory](https://en.wikipedia.org/wiki/Working_directory) is where you think it is. – user4581301 Oct 18 '22 at 18:09
  • 1
    You don't need to call `MyFile.close();`. The destructor will do that. – Pete Becker Oct 18 '22 at 18:14
  • Regarding the above comment, this is an example of a bit of C++ magic called [RAII](https://stackoverflow.com/questions/2321511/what-is-meant-by-resource-acquisition-is-initialization-raii). The constructor acquires the resource, the file in this case, and the destructor releases it. RAII allows you to automate many otherwise complicated management tasks. – user4581301 Oct 18 '22 at 18:18
  • @Someprogrammerdude the file doesn't saved successfully. But the directory is the right one. Idk why it is not saved. – TrueStoryShort Oct 18 '22 at 18:21
  • 1
    Do you mean that the file is created, but it's empty without text inside it? Then how do you check its contents? And when do you check the contents? Also, if you remove the file and run the program again, is the file recreated (but empty)? – Some programmer dude Oct 18 '22 at 18:35
  • @TrueStoryShort if you are *sure* the **current working directory** is what you are expecting (since you are using a *relative* path instead of an *absolute* path), then most likely you simply don't have write permissions to that directory, or write permissions to that specific file (ie, maybe it is already open somewhere else?) – Remy Lebeau Oct 18 '22 at 18:35
  • @TrueStoryShort: Does it print the error message `"Failed to open file\n"` when you change the code to what was recommended to you? – Andreas Wenzel Oct 18 '22 at 18:50
  • @RemyLebeau the file isn't created. I've also doublechecked the directory. It should be also no problem with writepermissions which i also assumed at first. I've checked the folder permissions, run the exe as admin, and closed other possible troubleshooters like antivirus – TrueStoryShort Oct 18 '22 at 18:51
  • 1
    @TrueStoryShort "*the file isn't created*" - Then I suggest you ask Windows why it fails. What does `GetLastError()` report? You might have to use `CreateFile()` directly to get a reliable error code, at least for testing until you fix the root cause – Remy Lebeau Oct 18 '22 at 18:55
  • Well i've found the solution thx @RemyLebeau and all others. It's because ive opened the file in vs code. Normaly (with other languages) i don't had a problem with that. Well such an easy problem took me literally 2 hours... – TrueStoryShort Oct 18 '22 at 18:55

0 Answers0