FORUM CLOSED, PLEASE REGISTER AT FORUM.SIO2INTERACTIVE.COM
Would you like to react to this message? Create an account in a few clicks or log in to continue.

How to write a TEXT file to iPhone

+2
iphoniac
sw
6 posters

Go down

How to write a TEXT file to iPhone Empty How to write a TEXT file to iPhone

Post  sw Wed Feb 11, 2009 8:44 am

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.
sw
sw

Posts : 73
Join date : 2008-10-12

Back to top Go down

How to write a TEXT file to iPhone Empty Re: How to write a TEXT file to iPhone

Post  iphoniac Wed Feb 11, 2009 9:11 am

You should write it to the app documents folder. You can use something like this:

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.

iphoniac

Posts : 62
Join date : 2008-12-11

http://thecaveaniphonegame.blogspot.com/

Back to top Go down

How to write a TEXT file to iPhone Empty Re: How to write a TEXT file to iPhone

Post  oioioi Wed Feb 11, 2009 10:57 am

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.
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;
   }
}
I also added a variable inside sio2 struct for the documents path, like this:
Code:
typedef struct
{
   char            app_path[ SIO2_MAX_PATH ];
   char            doc_path[ SIO2_MAX_PATH ];
If you do this make sure you add this call to sio2Init: sio2GetDocumentsDirectory(doc_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
note that using "w" in fopen will delete all content of the file! Don't use "w" for writing high scores! If you want more info about this look here fopen. You may also want to read a text file this is how to do it whit FILE*:
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);
You may also need this function if you want to writ a high score fseek

oioioi

Posts : 136
Join date : 2008-12-02
Location : Norway

Back to top Go down

How to write a TEXT file to iPhone Empty Re: How to write a TEXT file to iPhone

Post  sw Wed Feb 11, 2009 11:52 am

hi, oioioi

thank you and the code Smile 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
sw

Posts : 73
Join date : 2008-10-12

Back to top Go down

How to write a TEXT file to iPhone Empty Re: How to write a TEXT file to iPhone

Post  meteors Wed Feb 11, 2009 11:59 am

Great stuff, thanks.



-j
meteors
meteors

Posts : 241
Join date : 2008-11-08
Location : Sunny Florida

Back to top Go down

How to write a TEXT file to iPhone Empty Re: How to write a TEXT file to iPhone

Post  oioioi Wed Feb 11, 2009 12:04 pm

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 Smile

oioioi

Posts : 136
Join date : 2008-12-02
Location : Norway

Back to top Go down

How to write a TEXT file to iPhone Empty Re: How to write a TEXT file to iPhone

Post  sw Wed Feb 11, 2009 12:43 pm

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 Smile


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' Smile



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
sw

Posts : 73
Join date : 2008-10-12

Back to top Go down

How to write a TEXT file to iPhone Empty Re: How to write a TEXT file to iPhone

Post  oioioi Wed Feb 11, 2009 1:42 pm

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

Back to top Go down

How to write a TEXT file to iPhone Empty Re: How to write a TEXT file to iPhone

Post  sw Wed Feb 11, 2009 8:43 pm

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?
fgets will read n-1 characters before meets the '\n' and include the '\n' .then return .
so next time it is the new line
sw
sw

Posts : 73
Join date : 2008-10-12

Back to top Go down

How to write a TEXT file to iPhone Empty Re: How to write a TEXT file to iPhone

Post  Don Jaffa Tue Aug 04, 2009 10:11 pm

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

Back to top Go down

How to write a TEXT file to iPhone Empty Re: How to write a TEXT file to iPhone

Post  sio2interactive Tue Aug 04, 2009 10:24 pm

Check the source of iZenGarden... the config is created / saved / loaded on the phone...
sio2interactive
sio2interactive

Posts : 1526
Join date : 2008-08-26
Age : 45
Location : Shanghai

http://sio2interactive.com

Back to top Go down

How to write a TEXT file to iPhone Empty Re: How to write a TEXT file to iPhone

Post  oioioi Wed Aug 05, 2009 2:44 am

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

Back to top Go down

How to write a TEXT file to iPhone Empty Re: How to write a TEXT file to iPhone

Post  Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum