creating a text file

Joined
Sep 11, 2013
Messages
10
Reaction score
0
Points
1
hello,

after long time of searching I came here for your help.

This is my program:
#include <stdio.h>

int main()
{
FILE *fr;

fr=fopen("cool.txt", "w");
if (!fr) { perror("fopen"); } else { printf("Nothing Wrong"); }

fprintf(fr, "hello");

fclose(fr);

return 0;
}

I am using Xcode. After I compile it, I got printed: Nothing Wrong.. But when I want to find my text file, I can not.. I tried even searching through spotlight, but I could not find it..

Please help ...
 

vansmith

Senior Member
Joined
Oct 19, 2008
Messages
19,924
Reaction score
559
Points
113
Location
Queensland
Your Mac's Specs
Mini (2014, 2018, 2020), MBA (2020), iPad Pro (2018), iPhone 13 Pro Max, Watch (S6)
It does get created but it does so in a Debug folder for your Xcode project. On my machine, it ended up here: /Users/vansmith/Library/Developer/Xcode/DerivedData/Test-hegcxqhvaepxatdplarpdfunsfrb/Build/Products/Debug/cool.txt. Go to /Users/vansmith/Library/Developer/Xcode/DerivedData/ and start working your way down through the folders to find it (note - the folders in there should contain your project name).
 
OP
S
Joined
Sep 11, 2013
Messages
10
Reaction score
0
Points
1
:D thank yout Very Much :D .. and do you know how to redirect it anywhere "nearer" ? :D
 

vansmith

Senior Member
Joined
Oct 19, 2008
Messages
19,924
Reaction score
559
Points
113
Location
Queensland
Your Mac's Specs
Mini (2014, 2018, 2020), MBA (2020), iPad Pro (2018), iPhone 13 Pro Max, Watch (S6)
Be explicit about where you want the input instead of just locating it where the binary is run from.
 
Joined
Apr 26, 2008
Messages
2,963
Reaction score
120
Points
63
Location
Belgium
Your Mac's Specs
iPad Pro 12.9 latest iOS
You can specify a full path name in the open() instead of just the file name.
Alternatively, there are functions to set the current path to something " nearer " before you do the open() :)
Don't forget to set the path back to what it was before the program terminates.
So the sequence can be get path(), set path(), open() etc.... and then set path() to what it was before.

Cheers ... McBie
 
OP
S
Joined
Sep 11, 2013
Messages
10
Reaction score
0
Points
1
okey.. that sounds logical.. so, example could be: fr=fopen("\user\documents\hello\cool.txt", "w")? .. and when I use
if((fr=fopen("cool.txt", "r")) == NULL)
printf("hi");

it prints hi, even though I put the text file in the same directory as my project..
 
Joined
Apr 26, 2008
Messages
2,963
Reaction score
120
Points
63
Location
Belgium
Your Mac's Specs
iPad Pro 12.9 latest iOS
You might be mixing things up .....
you opened a file in a specific path.
Then you open a file for " read " in the path where you are running the binary from ... does that file exist in that directory ? I assume not and that is why your file pointer = NULL and it prints " hi "

Edit .... you are also using the same file pointer for 2 different files :)

Cheers ... McBie
 

vansmith

Senior Member
Joined
Oct 19, 2008
Messages
19,924
Reaction score
559
Points
113
Location
Queensland
Your Mac's Specs
Mini (2014, 2018, 2020), MBA (2020), iPad Pro (2018), iPhone 13 Pro Max, Watch (S6)
Be careful with your paths as well - \user\documents\hello\cool.txt is not a valid Unix path. You'd be better off using something to get the home directory of the user.

It's here that I've exhausted the little C/C++ I know since I'm not sure how you do that.
 
OP
S
Joined
Sep 11, 2013
Messages
10
Reaction score
0
Points
1
okey... to sum this up:

my project is saved in: users/documents/

I want to create there my new text file called cool.txt .. I would do it:
fr=fopen("users/documents/cool.txt", "w");
And in other program I want to read this file, so I would write:
fr=fopen("users/documents/cool.txt", "r");

where is my mistake?
 
Joined
Apr 26, 2008
Messages
2,963
Reaction score
120
Points
63
Location
Belgium
Your Mac's Specs
iPad Pro 12.9 latest iOS
Assuming that the pathname convention is correct for UNIX then there is no error if you are using 2 different programs :)
Similar to VanSmith, I have no knowledge of the UNIX pathname conventions in C/C++
Also be prepared that you might run into ' authorisation ' issues if you ( as a user ) do not have the correct permissions to create/write/read a file in a specific path.

Cheers ... McBie
 

vansmith

Senior Member
Joined
Oct 19, 2008
Messages
19,924
Reaction score
559
Points
113
Location
Queensland
Your Mac's Specs
Mini (2014, 2018, 2020), MBA (2020), iPad Pro (2018), iPhone 13 Pro Max, Watch (S6)
fr=fopen("users/documents/cool.txt", "w");
Again, as a caution, users/documents/cool.txt is not a valid directory. OS X home directories are located at /Users/<your username> (replace this with your short hand username).
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top