0

I am new to Xcode and I an currently learning C. How does file opening work here?

When I was using Windows and I declared that I want to write to file e.g.:

FILE *fr;
fr = fopen("test.txt","w");

it created file if it did not exist. But here in Mac I don't see file anywhere.

Any help about where will file be created on mac would be really appreciated.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 1
    "*i don't see file anywhere*" are you sure you are looking at the right place? Consider specifying an absolute path, for debugging at least. – alk Feb 25 '18 at 14:48
  • 1
    If `fopen` isn't working it would return `NULL` You should probably check that. – Yonlif Feb 25 '18 at 15:39
  • i open finder and to search bar i write test.txt and it just find nothing.. you mean something like /diskname/desktop/test.txt? – Dušo Morháč Feb 25 '18 at 15:41
  • Yah yonlif, but also when you want to create file you make it "w" for write and then it should create itself. I heard that it will write NULL when file does not exist.. but declaring that i want file to be for write should create new file – Dušo Morháč Feb 25 '18 at 15:42
  • I'm aware that you are trying to create and write to a new file, but instead of searching for it you should check if it was created by this `NULL` check. – Yonlif Feb 25 '18 at 15:46
  • Please read [this](https://stackoverflow.com/questions/7370892/fopen-not-working-in-c). – Yonlif Feb 25 '18 at 15:47
  • I readed that and what i found out is that file exist. But i couldn't find where.. – Dušo Morháč Feb 25 '18 at 16:30
  • I used perror for that tho – Dušo Morháč Feb 25 '18 at 16:30
  • I also tryed NULL but suprisingly this one was not working... code was: FILE *fr = fopen("test.txt","r"); (r because w was not longer necessary since file was created as perror wrote.) then if (fr=='NULL') {printf ("File doesn't exist} else {printf("File exist!") } – Dušo Morháč Feb 25 '18 at 16:33
  • Is there any code that will say me file location? You know like when i used ascii it told me that "A" is 65 etc.. – Dušo Morháč Feb 25 '18 at 16:33
  • You can try find it via terminal, or for some reason it's saved as an hidden file, or try to save it in `\.` directory and see if it works... I truly have no idea right now why you can't find it in this directory. – Yonlif Feb 25 '18 at 17:37
  • Thanks for your kind advices, I will go ahead and try it. Will tell you what was problem if I will find out. Thanks – Dušo Morháč Feb 25 '18 at 17:54
  • The file, given the `fopen()` statement, will be in the same directory as where the executable is executed. To properly check for errors when calling `fopen()` use: `FILE *fr; fr = fopen("test.txt","w"); if( !fr ) { perror( "fopen failed" );` Note: error messages are to be output to `stderr`, not `stdout`. The `perror()` will output the enclosed text AND the reason the system thinks the function failed. – user3629249 Feb 26 '18 at 10:35

0 Answers0