I'm trying to read information from a text file in an Obj. C program and whenever I try to NSLog the output, I'm getting (null). I've made sure that the text files I'm working with are in my Copy Bundle Resources, which is what all of the other answers I've found have suggested. I'm using the following to access the files:
NSString *rightsPath = [[NSBundle mainBundle] pathForResource:#"Sample Maze Rights" ofType:#"txt"];
NSString *rightsContent = [NSString stringWithContentsOfFile:rightsPath encoding:NSUTF8StringEncoding error:NULL];
Can anyone make any suggestions?
That's what error argument is for.
NSError *error = nil;
NSString *rightsContent = [NSString stringWithContentsOfFile:rightsPath
encoding:NSUTF8StringEncoding
error:&error];
if (rightsContent == nil)
NSLog(#"error: %#", [error description]);
Related
I'm trying to make an xcode plugin that needs to write something to the .h file from the .m file. I haven't been able to find a stackoverflow post with an answer on how to do this. I've tried this code
NSData *data = [NSData dataWithContentsOfFile:filePath];
NSString *contents = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding];
I've also tried using the StringWithContentsOfFile method, however they both return nil. Is it because you can't read an .h file with this code or is it something else I'm missing.
filePath is this and it's a correct filepath, my ftp client can read it atleast file:///Users/myUserName/Documents/code/macOsDev/XcodePlugIns/XcoderPlugin/XcoderPlugin/XcoderPlugin.h
So my question is, how do I read and write to a .h file? Thanks in advance.
EDIT as requested, some more code however this is as far as I've gotten to the read/write part of my plugin.
NSString *filePath = path;
NSData *data = [NSData dataWithContentsOfFile:filePath];
NSString *contents = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding];
NSError *error;
NSString *contents1 = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
contents is #"" because data is nil
and contents1 is just nil;
error is error
NSError * domain: #"NSCocoaErrorDomain" - code: 260
in the debugger, but I'm not sure I'm using this error thing correctly.
Use "/Users/myUserName/Documents/code/macOsDev/XcodePlugIns/XcoderPlugin/XcoderPlugin/XcoderPlugin.h".
Dont use 'file://' prefix
Following is the code for reading and writing
NSString *path = #"your/path/tp/.h/file";
NSData *data = [NSData dataWithContentsOfFile:path];
//Get the contents of the file into the mutable string
NSMutableString *contents = [[NSMutableString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding];
//Make changes to your mutable string
[contents appendString:#"abc"];
//Write it back to the file
[contents writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
I'm just trying to write into a .txt file in Objective C. Here is the code:
BOOL success = [str writeToFile:#"tmp/cool.txt" atomically:YES encoding:NSUTF8StringEncoding error:&error];
if(success)
{
NSLog(#"done writing!");
}
else
{
NSLog(#"writing failed: %#", [error localizedDescription]);
}
The output of this code is "The folder cool.txt does not exist". I dont understand this, since the ".txt" would deem it to be file.
What am I doing wrong?
I wrote a demo for you, assume that you use iOS.
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:#"tmp"]; //get tmp path
NSString *filePath = [path stringByAppendingPathComponent:#"cool.txt"];
NSString *str = #"hello world";
NSError *error;
[str writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
NSString *str1 = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
NSLog(#"%#", str1);
Assume you use iOS as the platform...
You need to first get application's document directory so that you can write into that directory (you can use library or temporary directory but document directory is most common)
You have to make sure that 'tmp' directory exists under the document directory.
I'm using quite a simple method of storing file names in a text file. For some reason when I initiate the writeToFile I get a crash:
pathString = [NSString stringWithFormat:#"New FileName - %#.png", identifier];
NSString *currentContents = [NSString stringWithContentsOfFile:saveFilePath encoding:NSUTF8StringEncoding error:nil];
NSString *newContents = [NSString stringWithFormat:#"%#:::%#",currentContents, pathString];
NSData *newData = [newContents dataUsingEncoding:NSUTF8StringEncoding];
[newData writeToFile:saveFilePath options:NSDataWritingAtomic error:nil];
It reads the file, places it's contents into a variable called currentContents, then adds the new string to the file, and re-writes it. What's going wrong here.
Without the writeToFile line it works, with it, I get a crash.
Origin of saveFilePath
NSString *saveDocument = [NSString stringWithFormat:#"SavedFile.txt"];
NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
saveFilePath = [docsDirectory stringByAppendingPathComponent:saveDocument];
An NSLog of saveFilePath reveals a correct path
I think your problem might actually be a missing null character ('\0') at the end of your NSData object. So you finally end up with messed up data. You might want to use -writeToFile:atomically:encoding:error: on your new string right away anyway.
It turns out that the reason the file wasn't writing was because of an unallocated variable:
NSString *currentContents = [NSString stringWithContentsOfFile:saveFilePath encoding:NSUTF8StringEncoding error:nil];
should have been:
NSString *currentContents = [[NSString alloc] initWithContentsOfFile:saveFilePath encoding:NSUTF8StringEncoding error:nil];
NSString *adress = [NSString stringWithFormat:#"ftp://%#:%##%#/%#x050-pegel.txt", benutzer.text, passwort.text, server.text, pfad.text];
NSURL *aURL = [[NSURL alloc]initWithString:adress];
NSString *theData = [NSString stringWithContentsOfURL:aURL];
textview.text = theData;
can some one help me please ?
AFAIK, FTP support is not part of the Cocoa frameworks. You could get more information by using +[NSString stringWithContentsOfURL:encoding:error:] and looking at the NSError object, like so:
NSError *error = nil;
NSString *theData = [NSString stringWithContentsOfURL:aURL encoding:NSUTF8StringEncoding error:&error];
if (error != nil) {
NSLog(#"received error: %#", error);
}
A quick google search for "cocoa ftp" shows that there are several approaches to connecting to an FTP server from Objective-C.
I am new to developing OSX Applications. I normally do iOS Apps, so alot of the concepts carry over. However, I cannot quite seem to figure out why I cannot retrieve the data of a file on my system.
Is there something that needs to be done first in order to read files on the users system?
Here is what I have:
- (IBAction)btnBrowse:(id)sender {
NSOpenPanel *panel = [NSOpenPanel openPanel];
[panel beginWithCompletionHandler:^(NSInteger result){
if (result == NSFileHandlingPanelOKButton) {
//Get the file url user selected
NSURL *file = [[panel URLs] objectAtIndex:0];
//Get the file data
NSError *error;
NSString *fileData = [NSString stringWithContentsOfFile:[NSString stringWithFormat:#"%#", file] encoding:NSUTF8StringEncoding error:&error];
//This returns null
NSLog(#"%#", fileData);
//This says that the file does not exist
NSLog(#"Error: %#", error.localizedDescription);
}
}];
}
NSOpenPanel returns NSURL objects, the easiest solution is to use the NSURL related API
NSString *fileData = [NSString stringWithContentsOfURL:file encoding:NSUTF8StringEncoding error:&error];
If you don't know the text encoding you could use this API, if the reading succeeds, encoding contains the used encoding.
NSStringEncoding encoding;
NSString *fileData = [NSString stringWithContentsOfURL:file usedEncoding:&encoding error:&error];
Use the -[NSURL path] method instead of calling 'file' directly, as it's a NSURL, not NSString.