C FOpen() Open Documents Directory iOS - ios

I need help getting access to the documents directory using only C on iOS.
I have my .c file looking for a specific file in the application bundle. I have no problem accessing this file. It looks like this: fopen("filename",
Unfortunately, if I want to move that file to the documents directory, appending "/Documents/filename" doesn't work.
I know how to access the file using an objective-c class, easily, using filesystemrepresentation. But I don't know how to do it only in C. Any help would be greatly appreciated!
Thanks!

Fopen defaults to the directory that the executable file is in, ".app/" - on iOS.
I was able to get to the documents directory by making a char of the Current Working Directory and then removing the last couple characters of the char to get out of the ".app/" bundle and then appending the Documents path to the end of it.

Related

How do I put content into the iOS Documents folder at compile time, using Xamarin?

I have a data file that I need to include with my app when I distribute it. When loading any files in the app, I prefix the file name with:
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
This works great for anything I create within the app (and for reading back), like files I download in response to a user action. But I can't for the life of me figure out how to place files there when I build my app in Visual Studio.
I've tried making a "Documents" subdirectory in the special "Resources" folder, but that didn't work (I tried setting the "Build Action" to both BundleResource and Content). When I look at the folder for my app (from using the simulator) I can see that in the "Documents" folder there's all the files I downloaded, but I can't find my data file that I'm trying to bundle ahead of time. I even searched my entire hard drive on the Mac and still couldn't find said data file.
The data file isn't an image, if it matters. Just raw binary data. How do I set it up so that this file goes into the proper documents directory at compile time, so that I can read it using the SpecialFolder.MyDocuments prefix? Thanks.
You can't. You can include files in your app bundle, and then at startup copy them from the bundle into a user folder. But this won't happen automatically.

How to loading outside .love save file? (love2d+ LUA)

I write editor and game and it requires a save game option.
I Just want use a Save folder in game folder. not inside.love
How to get a bug.
Start game
Enter menu by Escape key
Press save game - this create savegame
Press Load game - you get an error "File not exist" but you write this file seconds ago.
I successful save a game in savesm2k folder but it folder outside .love file and i don't know how correct load saved game.
Used engine: Love2d 0.10.2
OS: Linux Mint 18.1 x64bit
I read many manuals and I'm stuck.
For loading save game i use this commands
lsg=love.filesystem.getSaveDirectory().."/M2k-Saves/m2ksave"
lsg=love.filesystem.getSourceBaseDirectory().."/M2k-Saves/m2ksave";
data, size = love.filesystem.read (lsg);
leveldatacopy=freadbin (data);
Why does the program not try read existing file? and report no exist? I try using another command but using GetSaveDirectory broke function WriteMAP (requires for binary map and data writing) etc. but it write files which cannot be loaded in load section.
Maybe I should use LUA for reading files direct from folder but I don't know how to correct DO it.
example with bug.
https://github.com/dj--alex/m2ktest
file m2ktest-load-savegame-test.love
At this moment on every saving i manually replace saved game inside love archive (!) . This is not normal.
Anybody can tell me how i get and correct open saved file? Not from inside .love file . from outside of course. If levels and configs can be readed from love file inside save files must be outside. I can only can create files outside love file . i know love file is a zip archive.
If required i can post a .love file but is game completely done and have 150kb of clean code.
You should not prefix paths with love.filesystem.getSaveDirectory() or love.filesystem.getSourceBaseDirectory(). This is done automatically, internally to the love.filesystem functions. Saying
lsg = "/M2k-Saves/m2ksave"
data, size = love.filesystem.read (lsg)
leveldatacopy = freadbin (data)
should work, and will place the file in the save directory / outside of the .love file or game directory. (Love forbids writing anywhere except in the save directory.)
Love's filesystem model is like a "stack" of filesystems. For file reading, it first looks in love.filesystem.getSaveDirectory() with whatever path you pass in. If it finds a file, it uses that; otherwise it looks inside the love.filesystem.getSourceBaseDirectory() (or – if packed – inside the .zip / .love file). Writing files always goes to love.filesystem.getSaveDirectory(), you cannot write to love.filesystem.getSourceBaseDirectory() (because it may not be an actual directory but a zip file).

Why does extracting file a file using TZipFile in iOS Simulator raise access violation?

I'm using TZipFile to extract a zip file and it's works ok in win32 but raise this exception in ios simulator. I dont know why , i've checked the location for the extraction is ok, passed the open file but when come to the extract it still raise that exception. Currently i'm not having any ios device for the real testing but please help on simulator, i'm frustrating with this.
ZipFile.Open(filePath, zmRead);//this line passed,
ZipFile.Extract(0,dirPath );//raise EAccess exception in this line
ZipFile.Close;
//the filePath and the dirPath is the location of file and location i want to extract, it's all correct.
Or use
Zipfile.ExtractZipFile(filePath,dirPath) //still that exception
Update:
OH i think i'm missing the information about my project, my working is to download a zip file which contain a .csv file from a server. I've downloaded it to the a folder(create at run time) in the ios simulator, the directory of folder i put in to variable dirPath = Tpath.GetHomePath() + SeparatorChar + 'csv' and the variable 'fileName' is the dirPath' + name of file zip i downloaded. And i about to extract it right in that folder. So i use TZipFile to extract it and it cause up the access violation error in ZipFile.Extract line. I putted my download and extract section code to a new project and it works perfectly. I dont know why but my main project is a large prj which contain many functions and come up before my download section.Thanks
Problem solved, in my Download.pas is a separated class and i use library System.Zip in there and i use another class to call it. So my work around is put uses System.Zip right in the Main form when project start and problem solved, there is no logical here and i think it's a bug from System.Zip. Thanks

NULL when using fopen with xcode

I am trying to initialize a multidimensional array from a file using C for a iPhone 4inch app but I can't open up the file using fopen.
Whenever I try this I get a NULL:
FILE *f;
f=fopen("/level1.rez", "r");
if (f == NULL)
{
printf("Error Reading File\n");
//exit (0);
}
I am not sure how to open files using C.
I tried this already:
I printed out the current working directory using getcwd but all I got was "/" and when I attached that to the file name I still got NULL.
I read that if you go to product > scheme > edit scheme then options you can change the current working directory but I don't see that option.
Also I read that you can use absolute paths like: /users/name/desktop/program
but I am new to iOS development so I don't know if that is a good idea.
So how do I get fopen to work?
You CAN specify absolute paths in iOS, but the path in your example is probably used in Mac OS, which is laid out a little differently. You can specify paths to fopen() as you say, but there is more work to finding out what the first part of that path really is.
The iOS puts all AppStore apps into folders with randomly generated sandbox directory names. It is basically the the hexadecimal string of a GUID. So you need to use methods from iOS frameworks to get the first part of the path (or URL) to your file.
If the file is part of the app bundle so it can ship with the app, then you will need to use NSBundle methods to find the path to the file.
If the file is generated or downloaded after the app starts up on the device, then you need to use NSFileManager methods to determine the path to the directory of the file. (Typically the Documents directory. You can build a directory structure of your choice within the sandbox.)

module not fond when call Lua file in ios

i am writing an ios game.i use lua to write the game logic.then i try to integrate lua files into ios program.i add the lua files to app resource.when i run the program, an error occur:
LUA_ERRRUN: a runtime error.
...4-489C-4A40-8582-F734FAAC428D/ChemLLK.app/llk_facade.lua:3: module 'lianliankan' not found:
no field package.preload['lianliankan']
no file '/usr/local/share/lua/5.2/lianliankan.lua'
no file '/usr/local/share/lua/5.2/lianliankan/init.lua'
no file '/usr/local/lib/lua/5.2/lianliankan.lua'
no file '/usr/local/lib/lua/5.2/lianliankan/init.lua'
no file './lianliankan.lua'
no file '/usr/local/lib/lua/5.2/lianliankan.so'
no file '/usr/local/lib/lua/5.2/loadall.so'
no file './lianliankan.so'
i wander whether "." in path is equal to the resource folder. and i don't know how to solve this problem.
who can help me?
When a script calls require, it searches a number of preset locations for the script. None of those preset locations are your application resources. Therefore, a script's require function will never find what it's looking for.
You need to register a handler for require that will look for the script in your resources.

Resources