-1

I was just learning C++ file handling, but ran into an error immediately using this simple code to create a new file:

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

int main() {
  fstream file("test.txt");
  file << "test";
  file.close();
}

File doesn't show up. When I use .is_open(), it always gives 0. Program does compile though. Then I manually created a .txt file with some text and tried to read it, and it worked. I supposed it was permission thingy, but it seems like all files are available to be changed? I'm not sure I completely understand how to check the permission though (I'm a bit new to all of this...), so please do help with it as well! I use Atom and its terminal, my compiler is MingW, but I guess it might be a bit too old. I tried to include the whole path, but it didn't work. Thank you!

EDIT: Just tried this code:

  ofstream file;
  file.open("C:\\Users\\username\\Desktop\\my_folder\\test.txt");
  file << "test";
  file.close();

Doesn't help. .is_open() gives 0 before I even try to write to a file.

EDIT: Just tried this code:

  fstream fileW;
  fileW.open("write.txt", ios_base::in | ios_base::out | ios_base::trunc);
  cout<<fileW.fail()<<endl;
  cout<<fileW.is_open()<<endl;
  fileW<<"Edit";
  fileW.close();

Still doesn't work, returns 1 for .fail().

EDIT: The error is "Permission denied".

EDIT: Solved! I deleted Avast, it was blocking my program from accessing files. Avast, I hate you. You are the worst.

sgksmnv
  • 11
  • 1
  • Are you trying to write to your Program Files folder by any chance (or some other folder for which you do not have permissions?) I notice you don't specify a full path, so it will be in the current directory, which depends on how you launched it, but is likely the same dir as the application, which may not be writable. – Wyck Aug 08 '22 at 17:57
  • put a complete path – pm100 Aug 08 '22 at 17:57
  • 2
    Possible duplicate of [std::fstream doesn't create file](https://stackoverflow.com/questions/8835888/stdfstream-doesnt-create-file) given that you didn't specify a mode and the default is `ios_base::in|ios_base::out`, and specifying `in` (even by not specifying the mode at all) requires that the file exists. – Wyck Aug 08 '22 at 18:06
  • I tried to put a full path, but it didn't work. Anyways, I thought it was supposed to be created in the current directory without the full path. and I also tried to use ofstream, but it doesn't work. Ifstream works for files I just created though, so I can read them and output the line using cout and getline. – sgksmnv Aug 09 '22 at 03:00
  • I also checked for possible answers, but none of them helped me. I don't even get the error "No such file or directory", it compiles without any errors, and when I launch the program, it does nothing, ends without errors. – sgksmnv Aug 09 '22 at 03:05
  • Your last version works for me: https://onlinegdb.com/0JCkES0Yy (click the link and then hit run and the file is created) Perhaps your app doesn't have permission to the directory you are trying to write to. Maybe you need to see the actual error message `std::cout << "Error: " << strerror(errno);` - which I got from https://stackoverflow.com/questions/17337602/how-to-get-error-message-when-ifstream-open-fails – Jerry Jeremiah Aug 09 '22 at 04:29
  • Thanks! The error I get is "Permission denied". Now, how do I give permission?...Sorry if that's another question, I'm really new to this. – sgksmnv Aug 09 '22 at 05:08

1 Answers1

2

Use

ofstream file("test.txt"); 

Your version does not create the file if it does not exist.

Similarly for input you should really use ifstream. fstream is best reserved for files you want to read and write from.

john
  • 85,011
  • 4
  • 57
  • 81