How to write a TEXT file to iPhone
+2
iphoniac
sw
6 posters
How to write a TEXT file to iPhone
I want to store the player's Score in a text file, but the iphone os did not let me wreite the file.
does iphone only allow us write the file in /var/root ?
would some one give me some advice .
thanks
SW.
does iphone only allow us write the file in /var/root ?
would some one give me some advice .
thanks
SW.
sw- Posts : 73
Join date : 2008-10-12
Re: How to write a TEXT file to iPhone
You should write it to the app documents folder. You can use something like this:
Cheers.
- Code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectoryPath stringByAppendingPathComponent:@"myfile.conf"];
NSString *settings = @"1.0,0.0,0.0,0,";
NSData* settingsData;
settingsData = [settings dataUsingEncoding: NSASCIIStringEncoding];
if ([settingsData writeToFile:filePath atomically:YES])
NSLog(@"writeok");
Cheers.
Re: How to write a TEXT file to iPhone
If you don't like objective-c here is my way of doing it:
First you need to find the path of the Documents directory(there you have write access). I added this function in sio2_utils.c remember to add it in sio2_utils.h if you do the same as me. I am sure you can find a way to improve this function, but since it only runs one time on startup I don't care.
Now you need to write the file, this should do:
First you need to find the path of the Documents directory(there you have write access). I added this function in sio2_utils.c remember to add it in sio2_utils.h if you do the same as me. I am sure you can find a way to improve this function, but since it only runs one time on startup I don't care.
- Code:
void sio2GetDocumentsDirectory( char *_ppath)
{
int i = 0;
while(i != SIO2_MAX_PATH) //copy app_path to _ppath
{
_ppath[i] = sio2->app_path[i];
++i;
}
i = 0;
bool done = false;
while(!done)
{
if(_ppath[i] == '.' && _ppath[i+1] == 'a' && _ppath[i+2] == 'p' && _ppath[i+3] == 'p') //search for ".app" in the path string
{ //note: dont give your app a name containing".app"
while(!done) //then this function will not work
{
if(_ppath[i] == '/') //replaces "appname.app" with "Documents/"
{
_ppath[++i] = 'D';
_ppath[++i] = 'o';
_ppath[++i] = 'c';
_ppath[++i] = 'u';
_ppath[++i] = 'm';
_ppath[++i] = 'e';
_ppath[++i] = 'n';
_ppath[++i] = 't';
_ppath[++i] = 's';
_ppath[++i] = '/';
++i;
while(_ppath[i] != '\0') //if the app name is longer than Documents/ we need to remove the rest of the name, we dont want to end up
{ //with sometinhg like this ".../Documents/me.app"
_ppath[i] = '\0';
++i;
}
done = true;
}
--i;
}
}
++i;
}
}
- Code:
typedef struct
{
char app_path[ SIO2_MAX_PATH ];
char doc_path[ SIO2_MAX_PATH ];
Now you need to write the file, this should do:
- Code:
FILE *f;
char fname[ SIO2_MAX_PATH ] = {""};
sprintf( fname, "%s%s", sio2->doc_path, "somefile.txt" );
f = fopen(fname, "w"); //open file for writing
char cscore[10];
sprintf( cscore, "%d", score);//converts int to char
fputs(cscore, f);//write cscore to file f
fclose(f);//close file f
- Code:
FILE *f;
char fname[ SIO2_MAX_PATH ] = {""};
sprintf( fname, "%s%s", sio2->doc_path, "somefile.txt" );
f = fopen(fname, "r"); //open file for reading
char cscore[10];
fgets(cscore, 10, f); //get 9/10 first chars in file f(I always get one less than the number in fgets, in this case I get the 9 first chars)
score = atoi(cscore); //convert char to int
fclose(f);
oioioi- Posts : 136
Join date : 2008-12-02
Location : Norway
Re: How to write a TEXT file to iPhone
hi, oioioi
thank you and the code let me try it .
but would you please tell me why my test code can't work as expect
the following code works, but the fscanf get the wrong number .
did i force to use the fgets functions ?
thanks again
SW
thank you and the code let me try it .
but would you please tell me why my test code can't work as expect
the following code works, but the fscanf get the wrong number .
did i force to use the fgets functions ?
- Code:
void ReadScore()
{
FILE * pFile;
char scoreFile[512];
NSString *tempDir = [NSHomeDirectory() stringByAppendingString:@"/Documents/"] ;
sprintf( scoreFile , "%s%s",[tempDir UTF8String] , "MyScore.igd" );
if((pFile = fopen ( scoreFile , "r+" )) != NULL)
{
int int_money ; ///My_Money
fscanf(pFile, "%d\n", &int_money);
fscanf(pFile, "%d\n", &My_Win);
fscanf(pFile, "%d\n", &My_Copter_Count);
fscanf(pFile, "%d\n", &My_Fast_Bullet);
My_Money=(float) int_money ;
}
fclose (pFile );
}
void WriteScore()
{
FILE * pFile;
char scoreFile[512];
NSString *tempDir = [NSHomeDirectory() stringByAppendingString:@"/Documents/"] ;
sprintf( scoreFile , "%s%s",[tempDir UTF8String] , "MyScore.igd" );
if((pFile = fopen ( scoreFile , "w+" )) != NULL)
{
My_Money =123.0f ; My_Win =4 ; My_Copter_Count =5 ;My_Fast_Bullet = 60 ;
int int_money ; ///My_Money
int_money = (int) My_Money ;
fprintf(pFile, "%d\n", &int_money);
fprintf(pFile, "%d\n", &My_Win);
fprintf(pFile, "%d\n", &My_Copter_Count);
fprintf(pFile, "%d\n", &My_Fast_Bullet);
}
fclose (pFile );
}
thanks again
SW
sw- Posts : 73
Join date : 2008-10-12
Re: How to write a TEXT file to iPhone
Great stuff, thanks.
-j
-j
meteors- Posts : 241
Join date : 2008-11-08
Location : Sunny Florida
Re: How to write a TEXT file to iPhone
Hmmmm. I am not sure, does the file open? Do you know if fscanf and fprintf works? I know fputc doesn't work, try setting a break point to see if it breaks somewhere. Does the NSString point to the right place? Maybe try to change fprintf with fputs and fscanf with fgets? Just keep up the good work and you will make it
oioioi- Posts : 136
Join date : 2008-12-02
Location : Norway
Re: How to write a TEXT file to iPhone
oioioi wrote:Hmmmm. I am not sure, does the file open? Do you know if fscanf and fprintf works? I know fputc doesn't work, try setting a break point to see if it breaks somewhere. Does the NSString point to the right place? Maybe try to change fprintf with fputs and fscanf with fgets? Just keep up the good work and you will make it
hehe, i have found the problem ,and the fputs fgets works perfect.
the following is the code :
btw: the fgets will only get n-1 character, and will return when he meet the '\n'
- Code:
void ReadScore()
{
FILE * pFile;
char scoreFile[512];
NSString *tempDir = [NSHomeDirectory() stringByAppendingString:@"/Documents/"] ;
sprintf( scoreFile , "%s%s",[tempDir UTF8String] , "MyScore.igd" );
if((pFile = fopen ( scoreFile , "r" )) != NULL)
{
int int_money ; ///My_Money
char _Money[256] , _Win[256], _Copter[256], _Bullet[256] ;
fgets(_Money,100, pFile);
fgets(_Win,100, pFile);
fgets(_Copter,100, pFile);
fgets(_Bullet,100, pFile);
sscanf( _Money , "%d", &int_money );
sscanf( _Win , "%d", &My_Win );
sscanf( _Copter , "%d", &My_Copter_Count );
sscanf( _Bullet , "%d", &My_Fast_Bullet );
My_Money=(float) int_money ;
}
fclose (pFile );
}
void WriteScore()
{
FILE * pFile;
char scoreFile[512];
NSString *tempDir = [NSHomeDirectory() stringByAppendingString:@"/Documents/"] ;
sprintf( scoreFile , "%s%s",[tempDir UTF8String] , "MyScore.igd" );
if((pFile = fopen ( scoreFile , "w" )) != NULL)
{
//My_Money =123.0f ; My_Win =431 ; My_Copter_Count =543 ;My_Fast_Bullet = 654 ;
int int_money ; ///My_Money
int_money = (int) My_Money ;
char _Money[256] , _Win[256], _Copter[256], _Bullet[256] ;
sprintf( _Money , "%d", int_money );
sprintf( _Win , "%d ", My_Win );
sprintf( _Copter , "%d ", My_Copter_Count );
sprintf( _Bullet , "%d ", My_Fast_Bullet );
fputs(_Money, pFile);
fputs("\n", pFile);
fputs(_Win, pFile);
fputs("\n", pFile);
fputs(_Copter, pFile);
fputs("\n", pFile);
fputs(_Bullet, pFile);
fputs("\n", pFile);
}
fclose (pFile );
}
sw- Posts : 73
Join date : 2008-10-12
Re: How to write a TEXT file to iPhone
fgets meets '\n' will it jump down one line so the next time you call fgets it will get the content on the second line?
oioioi- Posts : 136
Join date : 2008-12-02
Location : Norway
Re: How to write a TEXT file to iPhone
fgets will read n-1 characters before meets the '\n' and include the '\n' .then return .oioioi wrote:fgets meets '\n' will it jump down one line so the next time you call fgets it will get the content on the second line?
so next time it is the new line
sw- Posts : 73
Join date : 2008-10-12
Re: How to write a TEXT file to iPhone
Anyone know how to do this in the latest version of the engine? Considering theres no sio2Init anymore.
Don Jaffa- Posts : 49
Join date : 2009-02-19
Re: How to write a TEXT file to iPhone
Check the source of iZenGarden... the config is created / saved / loaded on the phone...
Re: How to write a TEXT file to iPhone
Don Jaffa wrote:Anyone know how to do this in the latest version of the engine? Considering theres no sio2Init anymore.
FILE, fputs and fgets is not part of the sio2 engine
oioioi- Posts : 136
Join date : 2008-12-02
Location : Norway
Similar topics
» How to convert a .blend file to a .sio2 file.
» Where is the sio2.cc file?
» Text Export Support
» Textures-related crashing (putpkt: write failed: Broken pipe)
» .WAV file support
» Where is the sio2.cc file?
» Text Export Support
» Textures-related crashing (putpkt: write failed: Broken pipe)
» .WAV file support
Permissions in this forum:
You cannot reply to topics in this forum