0

I have a problem where ofstream isn't creating a text file. I compile and run the program just fine with no errors and I am 100 percent certain that I am in the right directory and that there are no spelling mistakes or errors. What is strange is when I move my program to my C drive or onedrive folder, then the text file is created, it just doesn't work in any of my other folders. I don't know what the problem is and I have scoured the internet for the past couple hours trying to find a solution, but I think it has something to do with my file permissions perhaps?

Here is my code, it's just a really simple program:

#include <iostream>
#include <fstream>

using namespace std;

int main() {

    ofstream myfile;
    myfile.open("newnumbers.txt");

}
asvalk1672
  • 105
  • 1
  • 7
  • 1
    "_and I am 100 percent certain that I am in the right directory_": Please specify how you are certain of this. In most cases where similar questions are asked it turns out that the directory was _not_ correct, usually because the asker wasn't aware of the concept of a _working directory_. In other words: How exactly are you running your program? In which path is the executable? And in which path are you looking? – user17732522 Dec 26 '22 at 01:17
  • I copy the path of my program into my terminal on visual studio and let it compile and run. It runs with no errors but no file is created. I then move the program to my C drive and do the same process and then the file is created for some reason. – asvalk1672 Dec 26 '22 at 01:21
  • "_I copy the path of my program into my terminal on visual studio and let it compile and run._": What exactly does that mean? Do you change the directory in the terminal first, then run the executable directly from the terminal with just the file name? Please explain step by step with the full commands you used in the question. – user17732522 Dec 26 '22 at 01:22
  • Yes I change the change the directory first, then I copy and paste the path of the program and remove the name of the program at the end so it would look like this: `cd C:\Users\David\Documents\cpp_scripts\database\` – asvalk1672 Dec 26 '22 at 01:24
  • OK, that's the first step. Then what exactly do you do for "_and let it compile and run._"? – user17732522 Dec 26 '22 at 01:26
  • "g++ numbers.cpp" - which is the name of the file, followed by ".\a.exe" – asvalk1672 Dec 26 '22 at 01:27
  • ok, that procedure will create the file in `C:\Users\David\Documents\cpp_scripts\database\newnumbers.txt`. If it doesn't, add error checking to your code, see https://stackoverflow.com/questions/17337602/how-to-get-error-message-when-ifstream-open-fails and see what it will tell you. – user17732522 Dec 26 '22 at 01:31
  • Ok. Add this line at the end: `std::cout << std::boolalpha << "Is file good: " << myfile.good() << std::endl;` if it say true. Then file has been created. Look where you have been when started your program. That should be your working directory. If false -> you have some issues. May be permissions. – Yuri Dolotkazin Dec 26 '22 at 01:33
  • yeah i'm getting false, I don't have this issue in any other folder besides this one – asvalk1672 Dec 26 '22 at 01:38
  • There may be issue with an access rights for your user then. Or maybe there is no space on drive. Have you tried to create file in this location by hand? Do you use some linux like terminals on windows? Like cygwin or something? Can you create file in this location with `touch` from this terminal or `echo 123 > filename`? Try to call `stat "dir_name"` and see what permission do you have there. Any way there is nothing wrong with your code here. Issues you are facing related to configuration of your system. – Yuri Dolotkazin Dec 26 '22 at 01:53
  • That's what I was thinking, the program creates the text file in my C drive and my onedrive folder but it can't do it in the folder shown here – asvalk1672 Dec 26 '22 at 03:19
  • try doing ``myfile.open("newnumbers.txt", std::ios::out)`` – ihsan Dec 26 '22 at 08:55

0 Answers0