Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > Mac OS X Development Forums > Mac OS X Development

Reply
 
LinkBack Thread Tools Display Modes
Old 04-06-2009, 07:54 AM   #1 (permalink)
New Member
 
Join Date: Mar 2009
Location: Syd, Australia
Posts: 12
zer0 is on a distinguished road
Question NSFileHandle Issue creating a file

Hi,

I have search google on how to append text to a file and got that working but it only works if the file is already created.

heres a section of my code

Code:
NSFileHandle *fh = [NSFileHandle fileHandleForWritingAtPath:@"/Users/ahug4122/RSItemDataLog.txt"];
	[fh seekToEndOfFile];
	
	for (int i = 0; i < [items count]; i++)
	{
		RunescapeItem *rsi = [items objectAtIndex:i];
		NSString *str = [[NSString alloc] initWithFormat:@"\n%@:%@", [rsi name], [rsi price]];
		NSData *data = [str dataUsingEncoding:NSASCIIStringEncoding];
		[fh writeData:data];
		[str release];
		
	}
Now that code works great but only if the file is there. I couldn't find how to create a file before adding data into it, in the API documentation.

and should i release the data object in that loop as well, kinda new to the whole memory management?
zer0 is offline   Reply With Quote
Old 04-06-2009, 08:03 AM   #2 (permalink)
Former NeXTStep Developer
 
Join Date: Mar 2009
Posts: 997
FlyingDiver will become famous soon enough
Default

The NSFileHandle class description does say:

Quote:
Return Value

The initialized file handle, or nil if no file exists at path.
I think you need to use NSFileManager to check for the existence of the file, and create it if needed.

joe
FlyingDiver is offline   Reply With Quote
Old 04-06-2009, 03:54 PM   #3 (permalink)
New Member
 
Join Date: Mar 2009
Location: Syd, Australia
Posts: 12
zer0 is on a distinguished road
Default

I have done that check now but the NSFileHandler documentation doesn't show how to create the file if its not there, only how to create a file desciptor.

...Zer0...
zer0 is offline   Reply With Quote
Old 04-06-2009, 04:01 PM   #4 (permalink)
Former NeXTStep Developer
 
Join Date: Mar 2009
Posts: 997
FlyingDiver will become famous soon enough
Default

Quote:
Originally Posted by zer0 View Post
I have done that check now but the NSFileHandler documentation doesn't show how to create the file if its not there, only how to create a file desciptor.

...Zer0...

Code:
[NSFileManager createFileAtPath: blah contents: blahblah attributes: morerblah];
joe
FlyingDiver is offline   Reply With Quote
Old 04-06-2009, 04:06 PM   #5 (permalink)
New Member
 
Join Date: Mar 2009
Location: Syd, Australia
Posts: 12
zer0 is on a distinguished road
Default

Thanks Joe,

I will look at the rest of the api for NSFileManager later.

...Zer0...
zer0 is offline   Reply With Quote
Old 08-05-2009, 05:49 AM   #6 (permalink)
Registered Member
 
85joshua@gmail.com's Avatar
 
Join Date: Apr 2009
Posts: 21
85joshua@gmail.com is on a distinguished road
Send a message via Yahoo to 85joshua@gmail.com
Default how to create file handling read and write operation in iphone

Code:
NSFileHandle *fh = [NSFileHandle fileHandleForWritingAtPath:@"/Users/ahug4122/RSItemDataLog.txt"];
	[fh seekToEndOfFile];
	
	for (int i = 0; i < [items count]; i++)
	{
		RunescapeItem *rsi = [items objectAtIndex:i];
		NSString *str = [[NSString alloc] initWithFormat:@"\n%@:%@", [rsi name], [rsi price]];
		NSData *data = [str dataUsingEncoding:NSASCIIStringEncoding];
		[fh writeData:data];
		[str release];
		
	}
when i have insert in this code.

i am writing data into one file(a.xml) in iphone default directory to put xml file.As the same file i have read them , to be change the data with directly for iphone default directory in that file(a.xml).

do you know any idea for file handling .reply this concept .
__________________
thanks and regards,
pradeep joshua
85joshua@gmail.com is offline   Reply With Quote
Old 08-05-2009, 05:53 AM   #7 (permalink)
Registered Member
 
85joshua@gmail.com's Avatar
 
Join Date: Apr 2009
Posts: 21
85joshua@gmail.com is on a distinguished road
Send a message via Yahoo to 85joshua@gmail.com
Default

Quote:
Originally Posted by FlyingDiver View Post
The NSFileHandle class description does say:



I think you need to use NSFileManager to check for the existence of the file, and create it if needed.

joe
i am writing data into one file(a.xml) in iphone default directory to put xml file.As the same file i have read them , to be change the data with directly for iphone default directory in that file(a.xml).

do you know any idea for file handling .reply this concept .
__________________
thanks and regards,
pradeep joshua
85joshua@gmail.com is offline   Reply With Quote
Old 08-05-2009, 03:53 PM   #8 (permalink)
Former NeXTStep Developer
 
Join Date: Mar 2009
Posts: 997
FlyingDiver will become famous soon enough
Default

Quote:
Originally Posted by 85joshua@gmail.com View Post
i am writing data into one file(a.xml) in iphone default directory to put xml file.As the same file i have read them , to be change the data with directly for iphone default directory in that file(a.xml).

do you know any idea for file handling .reply this concept .
What so you mean "iphone default directory". If it's the main bundle of the application, you can't write there. You can write to the documents directory.

joe
FlyingDiver is offline   Reply With Quote
Old 08-05-2009, 11:50 PM   #9 (permalink)
Registered Member
 
85joshua@gmail.com's Avatar
 
Join Date: Apr 2009
Posts: 21
85joshua@gmail.com is on a distinguished road
Send a message via Yahoo to 85joshua@gmail.com
Default how to create application settings and how to retrieve,write and edit data from the s

Quote:
Originally Posted by FlyingDiver View Post
What so you mean "iphone default directory". If it's the main bundle of the application, you can't write there. You can write to the documents directory.

joe
hi FlyingDriver

I had already done with in this concept using array function.
Code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentsDirectory = [paths objectAtIndex:0];
	return [documentsDirectory stringByAppendingPathComponent:kFilename];
but "how to edit the file(a.xml) in the phone simulater from the document directory" and "how to read the data from the iphone simulator in the document directory".

I have store the application configuration file(.xml) in the iphone then i have edit,read and write the file from the iphone document directory and automatically change the settings.

Where to go and find the document directory in iphone simulator.
do u have understand.if you have any idea send reply me for example or documentation
__________________
thanks and regards,
pradeep joshua
85joshua@gmail.com is offline   Reply With Quote
Reply

Bookmarks

Tags
nsfilehandle

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 473
15 members and 458 guests
alexeir, David-T, Dj_kades, foslock, iAppDeveloper, jeroenkeij, LunarMoon, Mijator, myach, pipposanta, QuantumDoja, robsmy, sacha1996, SLIC, usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,679
Threads: 94,129
Posts: 402,928
Top Poster: BrianSlick (7,990)
Welcome to our newest member, xzoonxoom
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:26 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0