Results 1 to 12 of 12
  1. #1


    Member Since
    Sep 11, 2013
    Posts
    10
    creating a text file
    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 ...

  2. #2

    vansmith's Avatar
    Member Since
    Oct 19, 2008
    Location
    Toronto
    Posts
    19,737
    Specs:
    2012 13" MBP (2.5 i5, 8GB)
    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

  3. #3


    Member Since
    Sep 11, 2013
    Posts
    10
    thank yout Very Much .. and do you know how to redirect it anywhere "nearer" ?

  4. #4

    vansmith's Avatar
    Member Since
    Oct 19, 2008
    Location
    Toronto
    Posts
    19,737
    Specs:
    2012 13" MBP (2.5 i5, 8GB)
    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

  5. #5

    McBie's Avatar
    Member Since
    Apr 26, 2008
    Location
    Belgium
    Posts
    2,732
    Specs:
    2013 MBA 13" - OS X 10.11
    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 bitterness of poor quality remains long after the sweetness of low price is forgotten.

  6. #6


    Member Since
    Sep 11, 2013
    Posts
    10
    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..

  7. #7

    McBie's Avatar
    Member Since
    Apr 26, 2008
    Location
    Belgium
    Posts
    2,732
    Specs:
    2013 MBA 13" - OS X 10.11
    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 bitterness of poor quality remains long after the sweetness of low price is forgotten.

  8. #8

    vansmith's Avatar
    Member Since
    Oct 19, 2008
    Location
    Toronto
    Posts
    19,737
    Specs:
    2012 13" MBP (2.5 i5, 8GB)
    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

  9. #9


    Member Since
    Sep 11, 2013
    Posts
    10
    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?

  10. #10

    McBie's Avatar
    Member Since
    Apr 26, 2008
    Location
    Belgium
    Posts
    2,732
    Specs:
    2013 MBA 13" - OS X 10.11
    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 bitterness of poor quality remains long after the sweetness of low price is forgotten.

  11. #11

    vansmith's Avatar
    Member Since
    Oct 19, 2008
    Location
    Toronto
    Posts
    19,737
    Specs:
    2012 13" MBP (2.5 i5, 8GB)
    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

  12. #12


    Member Since
    Sep 11, 2013
    Posts
    10
    thank you very much ..

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Quicktime QTtext, offset text file using text track.
    By Jesse Smith in forum OS X - Apps and Games
    Replies: 0
    Last Post: 03-14-2014, 04:05 PM
  2. MS Word Problem Creating Rule Above Text
    By PixelMage in forum OS X - Apps and Games
    Replies: 0
    Last Post: 10-22-2013, 11:14 AM
  3. creating ical event from selected text
    By ido.h in forum OS X - Apps and Games
    Replies: 0
    Last Post: 07-03-2010, 06:40 PM
  4. creating movable text boxes in iWeb
    By JoM in forum Web Design and Hosting
    Replies: 0
    Last Post: 01-20-2010, 09:50 AM
  5. Text to speech - Turn text into audio file
    By jimz in forum OS X - Apps and Games
    Replies: 1
    Last Post: 07-19-2006, 09:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •