Carbon - Reading / Writing Text Files

dave05
Unregistered
 
Post: #1
is there a better way than fopen/fread/fwrite to open, read from, and write to a text file that is located in the project bundle? I don't know how CGDataproviders work, and I don't understand how to retrieve the bundle path for fopen().

Any suggestions?

Thanks so much in advance.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
CFBundle will get you paths to your resources.

I'd stick with fopen & friends personally, though there are *many* other options.
Quote this message in a reply
Moderator
Posts: 698
Joined: 2002.04
Post: #3
dave05 Wrote:[...]I don't understand how to retrieve the bundle path for fopen()[...]

I'm not absolutely sure what you meant by the above, but the code below will switch the CWD to the Resources directory within your application's bundle...

Code:
CFURLRef resourcesURL;
char resourcesPath[ 4096 ];
resourcesURL = CFBundleCopyResourcesDirectoryURL( CFBundleGetMainBundle());
CFURLGetFileSystemRepresentation( resourcesURL, 1, resourcesPath, 4096 );
chdir( resourcesPath );

Mark Bishop
--
Student and freelance OS X & iOS developer
Quote this message in a reply
dave05
Unregistered
 
Post: #4
as always, this forum delivers. I posted the thread late last night and later realized that CGDataproviders are for "core graphics".
Quote this message in a reply
dave05
Unregistered
 
Post: #5
hey everyone.

I used the suggestions made above in the thread to implement file "loading". That is, there are a set # of player slots and 1 text file in my project bundle. The text file is read when the application is launched:
Code:
FILE *pfile = fopen("PlayerFile.txt", "r");
etc, etc, etc
fclose(pfile);

this part works without a problem. writing to the file proves otherwise:
Code:
FILE *pfile = fopen("PlayerFile.txt", "w");
etc, etc, etc
fclose(pfile);

the above code did not work, and in fact resulted in an error when attempting to load the file on my next debug-test-run. I also tried opening and writing to a completely new text file, but after writing to the file (called newfile.txt) and quitting my application, no file was created! Not only was there no file called newfile.txt in my project directory in the Resources folder (where i'd like to load and save my file), but a harddisk search also found no such file.

additional question(s):
when writing a string to a file, is the size_t of the string just the length of the string, or the length * sizeof(char)? does this differ between using "wb" and "w" / "rb" and "r"? For text files, i'm pretty sure i should be sticking with non-binary reads and writes. i know someone can shed some light. thanks in advance as always
Quote this message in a reply
Moderator
Posts: 522
Joined: 2002.04
Post: #6
sealfin Wrote:I'm not absolutely sure what you meant by the above, but the code below will switch the CWD to the Resources directory within your application's bundle...

Code:
CFURLRef resourcesURL;
char resourcesPath[ 4096 ];
resourcesURL = CFBundleCopyResourcesDirectoryURL( CFBundleGetMainBundle());
CFURLGetFileSystemRepresentation( resourcesURL, 1, resourcesPath, 4096 );
chdir( resourcesPath );

Shouldn't you be using PATH_MAX instead of 4096?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #7
PATH_MAX is actually less than 4096, I think, so it's probably not an issue. It is the correct thing to do, though.

I'm sure this is not your problem, but you should *never* write to a file inside your application bundle. The user may not have permission to do so. This is what ~/Library is for Smile

"w" says to stdio, "please feel free to screw around with what I write in any way you feel like", where "wb" says "please preserve exactly what I write". On Mac OS X (and all unixes) there's no difference between the two. On Windows, "w" will translate "\n" to "\r\n", and "r" will translate "\r\n" to "\n".

If your file is not being created, you need to check that fopen() is returning a non-NULL pointer, and that you are actually calling fclose.
Quote this message in a reply
dave05
Unregistered
 
Post: #8
so just to be clear, if i were to call chdir("~/Library/somefoldername") and the folder did not exist, how would i determine it did not exist? does mkdir work in C? (i read the man file and it seems highly unlikely)

[ps i think that is my problem...]


... also, i want the save file to be kind of hard to hack... so that's why i wrote the file to the bundle in the first place, but i guess that's not a good way to do it? .. guess i could encode it. Sad
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #9
dave05 Wrote:does mkdir work in C? (i read the man file and it seems highly unlikely)
man 2 mkdir is probably closer to what you want.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #10
I think you have worse problems than people hacking your data file Rasp

mkdir() is a C function -- read "man 2 mkdir"

Note that ~ is a shell expression; you need to find the user's home directory yourself. The unix function getpwuid can do that, or there may be an easier Carbon equivalent.

Also note that you should put your file in ~/Library/Application Support/YourAppName/
Quote this message in a reply
dave05
Unregistered
 
Post: #11
worse problems indeed...

anyways i searched the net and found my solution/

posted by OSC himself! haha

http://www.idevapps.com/forum/archive/in...-2817.html

Code:
#define _RWXAccessAll (S_IRWXU | S_IRWXG | S_IRWXO)
void FH_SetLibraryDirectory()
{
    char *libraryPath;
    
    libraryPath = malloc(128 * sizeof(char));
    libraryPath = strcat(getenv("HOME"), "/Library/Application Support");
    chdir( libraryPath );
    if (mkdir("My App", _RWXAccessAll) == 0)
        printf("Created a new directory in /Library/Application Support");
    chdir("My App");
}
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Writing libraries vs writing apps. Najdorf 8 4,897 Nov 13, 2008 02:32 PM
Last Post: backslash
  dynamic arrays, file reading/writing, etc. wyrmmage 6 3,805 Nov 10, 2006 06:08 PM
Last Post: akb825
  Reading/writing files in Carbon? ia3n_g 2 2,524 Jul 23, 2006 06:54 PM
Last Post: ia3n_g
  Reading Binary Files Nick 15 6,106 Jul 12, 2005 03:53 AM
Last Post: sealfin
  Getting text from a window text field mars 7 4,817 Mar 26, 2005 06:06 PM
Last Post: OneSadCookie