Advertise Books Events Forum News Social Networking Support Us

sdkIQ for iPhone
($4.99)

Shape Up
($0.99)

Your First iPhone App
($1.99)

iVidCam Free
(free)

Kid Art
($0.99)

iPUBQUIZ
(£1.19)

ArtStudio
($3.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-07-2010, 03:15 PM   #1 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 4
Question Why does my file write/read not work?

I've written the following code and single stepped through it with the debugger. By the time I get to the end, readData and readString are zero length, when I expect them to be "heading\r\ndata line 1\r\ndata line 2\r\n".

Can someone please suggest why the code doesn't work? Note that I plan on holding the file open and writing more later.

Also, do I need the synchronizeFile prior to the closeFile?

And finally, any recommendation on how to [or a utility app that can] look into this applications Documents folder to see the file I'm writing? I'll also want to transfer the file to a desktop (Windows PC).

Thanks,
Helmut

CODE:

// Hgf Create record file in <Application_Home>/Documents/ folder
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDire ctory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
if (documentsDirectory) {
NSString* myRecordPath = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat: @"%@.csv", @"Recorded Date"] ];
[myRecordPath retain];
//// Create recordfile
//NSFileManager *fileManger = [NSFileManager defaultManager];
//NSString* heading = @"heading";
//NSData* fileData = [heading dataUsingEncoding: NSASCIIStringEncoding];
//if ( ![fileManger createFileAtPath:myRecordPath contents:fileData attributes: nil] ) {
// NSLog (@"Error creating recording file!\n");
//}

NSString *recordFilePath = [documentsDirectory stringByAppendingPathComponent:myRecordPath];
NSFileHandle* recordFileHandle = [NSFileHandle fileHandleForWritingAtPath:recordFilePath];
NSString* heading = @"heading\r\n";
NSData* fileData = [heading dataUsingEncoding: NSASCIIStringEncoding];
[recordFileHandle writeData:fileData];
heading = @"data line 1\r\n";
fileData = [heading dataUsingEncoding: NSASCIIStringEncoding];
[recordFileHandle writeData:fileData];
heading = @"data line 2\r\n";
fileData = [heading dataUsingEncoding: NSASCIIStringEncoding];
[recordFileHandle writeData:fileData];
[recordFileHandle synchronizeFile];
[recordFileHandle closeFile];
//NSData* readData = [recordFileHandle readDataToEndOfFile];
NSData *readData = [[[NSData alloc] initWithContentsOfFile:recordFilePath] autorelease];
NSString* readString = [[NSString alloc] initWithData:readData encoding:NSASCIIStringEncoding];
[self broadcastChatMessage:readString fromUser:/*HgF*/@"recorder"];

} else {
NSLog (@"Documents folder not found!\n");
}
Helmut Forren is offline   Reply With Quote
Old 02-07-2010, 07:36 PM   #2 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 39
Default

1. You should not "retain" objects you did not "alloc".
Remove "[myRecordPath retain];"

2. check if recordFileHandle != nil

3. restart your phone, may be file was not released properly before.

4. File should exist before you can open it with "[NSFileHandle fileHandleForWritingAtPath:recordFilePath];"
softak is offline   Reply With Quote
Old 02-08-2010, 04:59 AM   #3 (permalink)
Registered Member
 
manicaesar's Avatar
 
Join Date: Feb 2009
Location: Poznań, Poland
Age: 23
Posts: 52
Default

Quote:
And finally, any recommendation on how to [or a utility app that can] look into this applications Documents folder to see the file I'm writing? I'll also want to transfer the file to a desktop (Windows PC).
You can view your application Documents folder by creating temporary web server within your app and connecting to it via browser (so it does not matter whether you have PC or Mac). To achieve this:
- Download free lightweight server written in C - Mongoose:
- Add its source files to your app files
- Use Objective-C wrapper for this server which you can find here
- Use localIPAddress method to get address for your server
- Enter the address in web browser on your computer

By default, if you do not set any uri callback function (in the wrapper it is not set), connecting to the server from web browser (iPhone and the computer you connect from should be in the same WiFi net) will result in printing contents of Documents folder of your app. You can browse it and download the files you are interested in

I hope this will be useful.
__________________
Do you like Age Of War? Have you played 'The Wars' and are not fully satisfied? Try Empires At War - we bet you will love it!


------------------
Full Version
------------------
Lite Version
------------------
YouTube Game Trailer
manicaesar is offline   Reply With Quote
Old 02-08-2010, 01:49 PM   #4 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 4
Default

Quote:
Originally Posted by softak View Post
1. You should not "retain" objects you did not "alloc".
Remove "[myRecordPath retain];"

2. check if recordFileHandle != nil

3. restart your phone, may be file was not released properly before.

4. File should exist before you can open it with "[NSFileHandle fileHandleForWritingAtPath:recordFilePath];"
Thanks. This got me going to the next step.

1. FYI, I didn't think the retain was necessary, but got it from borrowed code.

2. recordFileHandle was indeed nil, but because I never created file. I put back in the commented-out code to create file. Then recordFileHandle still nil due to a snafu creating pathnamepathname [sic]. After fixing pathname, recordFileHandle came back non-nil

3. I did restart the phone, but the above was the cause.

4. File not previously existing plus filename snafu were my problems.

Thanks.
Helmut Forren is offline   Reply With Quote
Old 02-08-2010, 01:54 PM   #5 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 4
Default

Quote:
Originally Posted by manicaesar View Post
You can view your application Documents folder by creating temporary web server within your app and connecting to it via browser (so it does not matter whether you have PC or Mac). To achieve this:
- Download free lightweight server written in C - Mongoose:
- Add its source files to your app files
- Use Objective-C wrapper for this server which you can find here
- Use localIPAddress method to get address for your server
- Enter the address in web browser on your computer

By default, if you do not set any uri callback function (in the wrapper it is not set), connecting to the server from web browser (iPhone and the computer you connect from should be in the same WiFi net) will result in printing contents of Documents folder of your app. You can browse it and download the files you are interested in

I hope this will be useful.
Thanks for the advice. I have been programming for 25+ years, but only a week on the iPhone. I'm sure I could figure out the above, but it's WAY too complicated for such a simple thing. I'm sure part of it is A**le security.

Perhaps you or someone else can provide an easier solution. Here's what I'm looking for...

(A) Just during development, I need to be able to look inside the iPhone, if for no other reason, to delete my junk files with junk names that I've created during testing. It would also be nice to be able to inspect file contents back on my desktop, rather than having to modify the app to not only write but also read back the file. [EDIT: per scottiphone at iphonedevsdk dot com, "To get file from the device just use organizer. Select your app in the list and twirl it down. Select and drag the app data to your desktop." This indeed works for READING my test files off the iPhone. It won't working for PUTTING test files onto the iPhone, but I'm not doing that. It also won't work for DELETING junk files off the iPhone. I guess I can just remove the app and reinstall for that... Still need to solve (B) below.]

(B) For release, I need users to have a simple solution for getting the file I wrote back over to there desktop. I've seen many existing iPhone apps for that. I was thinking of Readdle Docs. That *would* require my customers to also get another [paid] app. On the other hand, if I went through your suggestion, miniceasar, or perhaps some other that integrated the support into my app itself, then hopefully my customers could much more easily get their data from the iPhone to the desktop. [EDIT: I'm looking into http://groups.google.com/group/diddyFtpServer at the moment, to include FTP capability into my App for this purpose.] [EDIT: Learning further. diddyFtpServer uses GNU GPL, which seems problematic with apple's developer agreement. Looking back at maniceasar's mongoose suggestion, which uses MIT license, which appears for now compatible with apple and iPhone; note I've found comments about BSD license being better than MIT license.] [EDIT: Looks like maniceasar's "here" link is a one-stop-shop with both the wrapper and the mongoose code itself. So other readers of this post can go straight to his third line.]

-Helmut

Last edited by Helmut Forren; 02-08-2010 at 03:56 PM. Reason: found partial answer to (A) and (B)
Helmut Forren is offline   Reply With Quote
Old 02-08-2010, 04:17 PM   #6 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 4
Default

Maniceasar,

Thanks very much for the mongoose suggestion. It's looking better and better.

If I do the mongoose thing, I'm sure that it will work using an iPhone WiFi connection that eventually reaches a PC.

If there's no WiFi around, will it work through the iPhone USB? I'm concerned it won't because I've read that apple doesn't give developers USB access.

If it will only work via WiFi, do you know if one can do an [adhoc WiFi network] between a laptop with WiFi and the iPhone, but with NO access point like my Linksys devices in my office? If not, then my users will have to carry not only a laptop, but also an access point. (I may be using the wrong word when I say 'access point'. I have a linksys wireless router in my office.)

Thanks,
Helmut
Helmut Forren is offline   Reply With Quote
Old 02-10-2010, 02:53 AM   #7 (permalink)
Registered Member
 
manicaesar's Avatar
 
Join Date: Feb 2009
Location: Poznań, Poland
Age: 23
Posts: 52
Default

Quote:
Originally Posted by Helmut Forren View Post
Maniceasar,

Thanks very much for the mongoose suggestion. It's looking better and better.

If I do the mongoose thing, I'm sure that it will work using an iPhone WiFi connection that eventually reaches a PC.

If there's no WiFi around, will it work through the iPhone USB? I'm concerned it won't because I've read that apple doesn't give developers USB access.

If it will only work via WiFi, do you know if one can do an [adhoc WiFi network] between a laptop with WiFi and the iPhone, but with NO access point like my Linksys devices in my office? If not, then my users will have to carry not only a laptop, but also an access point. (I may be using the wrong word when I say 'access point'. I have a linksys wireless router in my office.)

Thanks,
Helmut
Well, Apple has just accepted our application update with mongoose server added. In new version of the application, using mongoose, user can both upload and download particular files within application documents folder (not all of course, only those, which we allow to be downloaded). But achieving this is not as simple as viewing application documents folder contents...
If you are interested in how it works, download SuperMemo app (it's free), go to New Exercise menu, click '+' in topright corner and choose Import / Export, then just follow the information listed.

I'm afraid, that you are right with the USB issue :/

And concerning AdHoc WiFi: in work I use iMac, which is connected to the internet via LAN, not WiFi. So I can use iMac's AirPort to share this internet connection, which results in creating local WiFi net. Then i just connect to this WiFi with iPhone. I'm not sure if you can do similar thing on your laptop (if it is MacBook - probably yes , but why don't give it a try?

Hope this helps,
Regards
__________________
Do you like Age Of War? Have you played 'The Wars' and are not fully satisfied? Try Empires At War - we bet you will love it!


------------------
Full Version
------------------
Lite Version
------------------
YouTube Game Trailer
manicaesar is offline   Reply With Quote
Reply

Bookmarks

Tags
initwithcontentsoffile, nsfilehandle, writedata

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


Enter the iPhone App Challenge!  Win $500!
» Advertisements
» Stats
Members: 24,143
Threads: 38,921
Posts: 170,763
Top Poster: smasher (2,565)
Welcome to our newest member, Pajolo
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 08:57 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0