sirisha
sirisha

Reputation: 41

unable to write the textfile?

i am new to iphone,i can read the txtfile using this code

-(NSString *)readfile:(NSString *)filename{
    NSString *path=[[NSBundle mainbundle] PathForResource:filename ofType:"txt"];
    NSData *mydata=[NSData dataWithContentsOfFile:path];
    NSString *str;
    if(mydata){
        str=[[NSString alloc] initWithData:mydata encoding:NSASCIIStringEncoding];
    }
    return str;
 }

Method Call::

 ownservices *readtxt=[[ownservices alloc] init];
 [readtxt readfile:@"textfile"];

By using this code i can read data from txtfile. By using these code i want to write add text to textfile

-(NSString *)writefile:(NSString *)filename:(NSString *)text{
    NSString *path=[[NSBundle mainbundle] PathForResource:filename ofType:"txt"];
    //NSData *mydata=[NSData dataWithContentsOfFile:path];
    [text writeToFile:path dataUsingEncoding:NSTUF8StringEncoding error:nil];
    return text;
 }

Method call:

  ownservices *write=[[ownservices alloc] init];
    [write writefile:@"txtfile":@"Hello"];

but text is not adding to txtfile save in resource folder anyone have an idea please help me,Thanks in advance

Upvotes: 0

Views: 190

Answers (2)

taskinoor
taskinoor

Reputation: 46037

You can not save anything in resource folder at run time. You can save the file in documents folder.

Upvotes: 1

Jhaliya - Praveen Sharma
Jhaliya - Praveen Sharma

Reputation: 31722

Change the function prototype

-(NSString *) writefile:(NSString *)filename  withText:(NSString *)text;

Definition :

-(NSString *)writefile:(NSString *)filename  withText:(NSString *)text
{
        //Put your code here
}

Calling place:

[write writefile:@"txtfile" withText:@"Hello"];

Upvotes: 0

Related Questions