PDA

View Full Version : Write to Text-file in Xcode (noob)




SkS
Dec 15, 2006, 04:36 AM
Hi
I dont know alot about programming i only know a little about Visual Basic Express and this is Xcode stuff.....

I have looked at the codes from Bruji's barcode scanner at http://www.bruji.com/cocoa/barcode.html and i now the barcodes is beeing written to a text windows inside the app but i want the app to write the Barcode to a text file on my harddisk.

If iam right the following codes add the Barcode to the Text window inside the application:

- (void)gotBarcode:(NSString *)barcode {
[resultsTextView replaceCharactersInRange:NSMakeRange([[resultsTextView string] length] ,0) withString:barcode];
[resultsTextView replaceCharactersInRange:NSMakeRange([[resultsTextView string] length],0) withString:@"\n"];

Hope you can help me, thanks
- SKS



kainjow
Dec 17, 2006, 09:41 PM
I think what you want is something like this:

[barcode writeToFile:@"/file.txt" atomically:YES];

Soulstorm
Dec 18, 2006, 03:24 AM
the barcode is an NSString. So, you can open the developer documentation in your XCode and search the NSString class explanation. In there you will find ways to write an NSString to a file, loading an NSString from a file, and many more.

garethlewis2
Dec 18, 2006, 09:30 AM
Before you really get yourself into a pickle, buy the following books

Programming in Objective-C by Stephen Cochan
Programming Cocoa by Aaron Hillegrass. <Hyped but good, glosses over controllers>
Cocoa - Anguish, et al. The def book on understanding Cocoa from a programmers point of view, very very heavy, but worth it. Only downside is, it is based on Jaguar code.

Without that grounding, you will always always get stuck.

For your question. Look at the docmentation for NSFileHandle and NSFileManager. These aren't the only ways, but they are conveniant and better. safe.

sikosis
Sep 22, 2010, 09:32 PM
Pity writeToFile is deprecated ...

kainjow
Sep 22, 2010, 10:51 PM
Pity writeToFile is deprecated ...

As the docs say:
Use writeToFile:atomically:encoding:error: instead.

sikosis
Sep 22, 2010, 11:05 PM
Ah worked it out ... you need to add a couple of extra params.


- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile encoding:(NSStringEncoding)enc error:(NSError **)error


@kainjow ... yep that's it -- guess I should have refreshed this page first before replying :)