0

I'm using Dev C++ as IDE, windows 10 and this is the code

#include<stdio.h>

main(){ 

        FILE *f;        
    f=fopen("C:\\Users\\min2\\Documents\\asd.txt", "w"); //(did not work)

        
        //f=fopen("C:/Users/min2/Documents/asd.txt", "w"); (did not work)
        //f=fopen("asd.txt", "w");  (did not work)
    
    if(f==NULL)printf("Did not work");  //this is what im getting in the console
    else printf("It worked");
    
            
        fclose(f);
        
}

It's not creating the .txt. I'm trying to do it in the documents. I tried on the desk but its not working either. It does not create the file.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
drop90prog
  • 45
  • 8
  • 5
    What does [`perror("fopen")`](https://en.cppreference.com/w/c/io/perror) print? – John Kugelman Feb 21 '21 at 16:56
  • Or `GetLastError()` in Windows. – 273K Feb 21 '21 at 16:57
  • Does the folder exist? if so, do you have permission to create a file there? – Kermit the Frog Feb 21 '21 at 17:02
  • Since you are using C++ why not try `fstream`? oh and btw `main` is missing the return type. – anastaciu Feb 21 '21 at 17:02
  • "fopen: Permission denied" I see, I'm not sure how to solve this – drop90prog Feb 21 '21 at 17:02
  • Are you logged in as `min2` when you run this? – Retired Ninja Feb 21 '21 at 17:10
  • yes, it is the only windows user as far as I know, I mean it's my pc and only have one user. What if I had to mark an option during the installation? – drop90prog Feb 21 '21 at 17:15
  • @RetiredNinja it really doesn't matter, the OP states `f=fopen("asd.txt", "w"); (did not work)` – anastaciu Feb 21 '21 at 17:21
  • You don't need to give yourself access to your own directory. You could try asking Windows to find your documents directory for you. https://stackoverflow.com/questions/2414828/get-path-to-my-documents I would use https://learn.microsoft.com/en-us/windows/win32/api/shlobj_core/nf-shlobj_core-shgetknownfolderpath but it will require you to work with wide strings. – Retired Ninja Feb 21 '21 at 17:22
  • 1
    @anastaciu That really depends on where the working directory was set at the time. – Retired Ninja Feb 21 '21 at 17:23
  • @RetiredNinja in the shown code (with the commented fopen instead), the file is created in same directory where it's executed – anastaciu Feb 21 '21 at 17:28
  • @anastaciu You can't know that from the information given. The only information I see is "I'm using Dev C++ as IDE" which could be setting the working directory to anywhere if the program is being run inside it. At any rate, this is all speculation. If writing to their own documents directory is not working then there's either something terribly wrong in the Windows setup or the path supplied is actually not correct. – Retired Ninja Feb 21 '21 at 17:31
  • @RetiredNinja yes, I see, I was overlooking the IDE – anastaciu Feb 21 '21 at 17:32
  • @drop90prog do you really have to use Dev C++? The last version is almost 6 years old. – anastaciu Feb 21 '21 at 17:36
  • @anastaciu not really, im not sure what other IDE I can use for C++. Fun fact: I tried again the fopen but instead of "asd.txt" I simply put "asd" and it does create the file, the problem is not slolved yet, but it's just curious, another thing is I tried it with "asd.txt" in a pendrive it works there, I am not sure what other information I must provide. This is for a school project, I isolated the rest of the project's code since the problem is just this fopen function. – drop90prog Feb 21 '21 at 17:42
  • @drop90prog that is in fact quite strange, the txt extension shouldn't make any difference. As for the IDE, there are several, you can use, for example Visual Studio Community or VS Code which is a lighter option or Eclipse, among others, these are free. – anastaciu Feb 21 '21 at 17:47
  • Dont you have the file opened elsewhere? If the file is already opened, it may be locked and system is unable to rewrite it. – Martin Perry Feb 21 '21 at 18:53

1 Answers1

0

I'm just passing by to say I dropped this, I never found the solution

drop90prog
  • 45
  • 8