New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Development and Darwin Discussion and questions about development for Mac OS X.

creating a text file


Post Reply New Thread Subscribe

 
Thread Tools
simon77

 
Member Since: Sep 11, 2013
Posts: 10
simon77 is on a distinguished road

simon77 is offline
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 ...
QUOTE Thanks
vansmith

 
vansmith's Avatar
 
Member Since: Oct 19, 2008
Location: Toronto
Posts: 17,817
vansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond repute
Mac Specs: 2012 13" MBP (2.5 i5, 8GB)

vansmith is offline
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).

Important Links: Community Guidelines : Use the reputation system if you've been helped.
M-F Blog :: Write for the blog
Writing a Quality Post
QUOTE Thanks
simon77

 
Member Since: Sep 11, 2013
Posts: 10
simon77 is on a distinguished road

simon77 is offline
thank yout Very Much .. and do you know how to redirect it anywhere "nearer" ?
QUOTE Thanks
vansmith

 
vansmith's Avatar
 
Member Since: Oct 19, 2008
Location: Toronto
Posts: 17,817
vansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond repute
Mac Specs: 2012 13" MBP (2.5 i5, 8GB)

vansmith is offline
Be explicit about where you want the input instead of just locating it where the binary is run from.

Important Links: Community Guidelines : Use the reputation system if you've been helped.
M-F Blog :: Write for the blog
Writing a Quality Post
QUOTE Thanks
McBie

 
McBie's Avatar
 
Member Since: Apr 26, 2008
Location: Belgium
Posts: 2,290
McBie is a name known to allMcBie is a name known to allMcBie is a name known to allMcBie is a name known to allMcBie is a name known to allMcBie is a name known to allMcBie is a name known to all
Mac Specs: 2013 MBA 13" - 10.9.3 & iPad - iOS 5.1

McBie is offline
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

A computer lets you make more mistakes faster than any invention in human history - with the possible exceptions of handguns and tequila.
The problem is not the problem. The problem is your attitude towards the problem. You understand ?
QUOTE Thanks
simon77

 
Member Since: Sep 11, 2013
Posts: 10
simon77 is on a distinguished road

simon77 is offline
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..
QUOTE Thanks
McBie

 
McBie's Avatar
 
Member Since: Apr 26, 2008
Location: Belgium
Posts: 2,290
McBie is a name known to allMcBie is a name known to allMcBie is a name known to allMcBie is a name known to allMcBie is a name known to allMcBie is a name known to allMcBie is a name known to all
Mac Specs: 2013 MBA 13" - 10.9.3 & iPad - iOS 5.1

McBie is offline
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

A computer lets you make more mistakes faster than any invention in human history - with the possible exceptions of handguns and tequila.
The problem is not the problem. The problem is your attitude towards the problem. You understand ?
QUOTE Thanks
vansmith

 
vansmith's Avatar
 
Member Since: Oct 19, 2008
Location: Toronto
Posts: 17,817
vansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond repute
Mac Specs: 2012 13" MBP (2.5 i5, 8GB)

vansmith is offline
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.

Important Links: Community Guidelines : Use the reputation system if you've been helped.
M-F Blog :: Write for the blog
Writing a Quality Post
QUOTE Thanks
simon77

 
Member Since: Sep 11, 2013
Posts: 10
simon77 is on a distinguished road

simon77 is offline
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?
QUOTE Thanks
McBie

 
McBie's Avatar
 
Member Since: Apr 26, 2008
Location: Belgium
Posts: 2,290
McBie is a name known to allMcBie is a name known to allMcBie is a name known to allMcBie is a name known to allMcBie is a name known to allMcBie is a name known to allMcBie is a name known to all
Mac Specs: 2013 MBA 13" - 10.9.3 & iPad - iOS 5.1

McBie is offline
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

A computer lets you make more mistakes faster than any invention in human history - with the possible exceptions of handguns and tequila.
The problem is not the problem. The problem is your attitude towards the problem. You understand ?
QUOTE Thanks
vansmith

 
vansmith's Avatar
 
Member Since: Oct 19, 2008
Location: Toronto
Posts: 17,817
vansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond repute
Mac Specs: 2012 13" MBP (2.5 i5, 8GB)

vansmith is offline
Quote:
Originally Posted by simon77 View Post
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).

Important Links: Community Guidelines : Use the reputation system if you've been helped.
M-F Blog :: Write for the blog
Writing a Quality Post
QUOTE Thanks
simon77

 
Member Since: Sep 11, 2013
Posts: 10
simon77 is on a distinguished road

simon77 is offline
thank you very much ..
QUOTE Thanks

Post Reply New Thread Subscribe


« xcode and array help | AppleScript for checking value »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
Diff tool for comparing text file version in Time Machine??? callagga OS X - Apps and Games 2 07-09-2013 09:59 PM
Want to combined data of all XML files located in a folder in 1 text file mrindia OS X - Operating System 5 05-30-2013 01:13 PM
Add text to photo in iPhoto Cuclean Images, Graphic Design, and Digital Photography 52 09-22-2012 04:56 PM
Get file name from Text edit in Automator rounak OS X - Apps and Games 3 09-12-2008 10:59 PM
What is the best way to convert a text file into audio MacGal Music, Audio, and Podcasting 1 04-10-2008 06:01 PM

All times are GMT -4. The time now is 10:24 PM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?