Writing to a file in Objective C - objective-c

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.

Related

Can't find my file after writeToFile

I was trying to create a CSV file using xcode but I can't seem to find the file anywhere after creation.
One solution I found was going into products folder itself but it isn't there.
Another solution that didn't work: Window-> Devices-> look at the installed apps of the devices but the container is empty.
Here is my code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *root = [documentsDir stringByAppendingPathComponent:#"test.csv"];
// Append file here
temp = [NSString stringWithFormat:#"%.0f", (float)*samples];
if (![[NSFileManager defaultManager] fileExistsAtPath:root]) {
[[NSFileManager defaultManager] createFileAtPath: root contents:nil attributes:nil];
NSLog(#"Route created");
}
NSError *error = nil;
NSLog(#"%#", root);
BOOL success = [temp writeToFile:root atomically:YES encoding:NSUTF8StringEncoding error:&error];
if(!success){
NSLog(#"fail to write to csv file");
}
It always is successful, but yet I can't find the file, also the file also seem to exists as it does not enter the if(fileExistsAtPath) after the first time.
The root directory is : /var/mobile/Containers/Data/Application/DDA4F37D-8D61-43F9-809C-31B09852B77F/Documents/test.csv
Edit: to make it clearer, I'm trying to retrieve it via xcode. Through finder so that I can get the csv file.
Any help is appreciated.

Obj C Reading Files, txt file not found

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]);

.m file to string

I am trying to convert a .m file to string. I will search for files in a folder and then want to use each of its contents as a string. This is the code I am using:
- (IBAction)searchAction:(id)sender {
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *dirContents = [fm contentsOfDirectoryAtPath:folderLabel.stringValue error:nil];
NSPredicate *fltr = [NSPredicate predicateWithFormat:#"self ENDSWITH '.m'"];
NSArray *onlyMs = [dirContents filteredArrayUsingPredicate:fltr];
for (int i=0; i<[onlyMs count]; i++) {
NSString* text = [[NSString alloc] initWithContentsOfFile:[onlyMs objectAtIndex:i]
encoding:NSUTF8StringEncoding
error:nil];
NSLog(#"string: %#", text);
}
}
2013-02-13 02:38:05.700 LocalizedStringSearch[19001:303] string: (null)
Except here, all the log is returning is null even though it will find all the .m file correctly.
Anyone know what I'm doing wrong?
Thanks a lot!
I think contentsOfDirectoryAtPath: gives you an array of filenames only, not full path names, so you need to prepend the path before you open files. EDIT: I think I might be confusing that with enumeratorAtPath:... if so continue using the filenames you have rather than appending them to the original folder name.
Here's an example (untested):
NSString *fullPath = [folderLabel.stringValue stringByAppendingPathComponent:[onlyMs objectAtIndex:i];
NSError *error = nil;
NSString *text = [[NSString alloc] initWithContentsofFile:fullPath
encoding:NSUTF8StringEncoding
error:&error];
if (text == nil)
NSLog(#"%#", error);
else
NSLog(#"%#", text);
The above will only work if the files actually are encoded using UTF-8. If you are unsure of the encoding, you can let the framework try and figure it out for you with:
NSString *fullPath = [folderLabel.stringValue stringByAppendingPathComponent:[onlyMs objectAtIndex:i];
NSError *error = nil;
NSStringEncoding enc;
NSString *text = [[NSString alloc] initWithContentsofFile:fullPath
usedEncoding:&enc
error:&error];
if (text == nil)
NSLog(#"%#", error);
else
NSLog(#"%#", text);

NSApplicationSupportDirectory invalid CFStringRef

I'm trying to store a file in the NSApplicationSupportDirectory, since i have a preloaded folder in my app, which i want to add files to once the app have been initialized. I'm trying to NSLog the content of the file, so i can see if it actually worked. From the debugger, i can see that the content is , which i don't not what means. Anybody?
NSString *document = [NSString stringWithFormat:#"%# %# %# %# %#", description, focus, level, equipment, waterDepth];
//NSLog(#"%#", document);
//get the documents directory:
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *supportDirectory = [paths objectAtIndex:0];
//filename
NSString *filename = [NSString stringWithFormat:#"%#/%#", supportDirectory,[_nameTextField.text stringByAppendingString:#".txt"]];
NSLog(#"%#", supportDirectory);
NSLog(#"%#", filename);
[document writeToFile:filename atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil];
NSString *content = [[NSString alloc]initWithContentsOfFile:filename usedEncoding:nil error:nil];
NSLog(#"%#", content);
You're not passing a string encoding, you are passing NSStringEncodingConversionAllowLossy which is an option that can affect how encoding conversion happens for certain methods (not the one you're using). You need to pass something like NSUTF8StringEncoding.
When you write or read files, it is highly recommended to use the error parameter and handle the cases where you get an error. Also, this is very useful to help you debug your code.
For example, in your case, you can do like this:
NSError *error = nil;
BOOL success = [document writeToFile:filename atomically:NO encoding:NSStringEncodingConversionAllowLossy error:&error];
if (!success) {
NSLog(#"Could not write file: %#", error);
} else {
NSString *content = [[NSString alloc]initWithContentsOfFile:filename usedEncoding:nil error:&error];
if (!content) {
NSLog(#"Could not read file: %#", error);
}
}
If you get an error that says that The operation couldn’t be completed. No such file or directory, that means that you did not previously create the folder. So you create it before trying to add content to it:
NSString *supportDirectory = [paths objectAtIndex:0];
NSError *error = nil;
BOOL success;
if (![[NSFileManager defaultManager] fileExistsAtPath: supportDirectory]) {
success = [[NSFileManager defaultManager] createDirectoryAtPath:supportDirectory withIntermediateDirectories:YES attributes:nil error:&error];
if (!success) {
NSLog(#"Could not create directory: %#", error);
}
}

Rename file in Cocoa?

How would I rename a file, keeping the file in the same directory?
I have a string containing a full path to a file, and a string containing a the new filename (and no path), for example:
NSString *old_filepath = #"/Volumes/blah/myfilewithrubbishname.avi";
NSString *new_filename = #"My Correctly Named File.avi";
I know about NSFileManager's movePath:toPath:handler: method, but I cannot workout how to construct the new file's path..
Basically I'm looking for the equivalent to the following Python code:
>>> import os
>>> old_filepath = "/Volumes/blah/myfilewithrubbishname.avi"
>>> new_filename = "My Correctly Named File.avi"
>>> dirname = os.path.split(old_filepath)[0]
>>> new_filepath = os.path.join(dirname, new_filename)
>>> print new_filepath
/Volumes/blah/My Correctly Named File.avi
>>> os.rename(old_filepath, new_filepath)
NSFileManager and NSWorkspace both have file manipulation methods, but NSFileManager's - (BOOL)movePath:(NSString *)source toPath:(NSString *)destination handler:(id)handler is probably your best bet. Use NSString's path manipulation methods to get the file and folder names right. For example,
NSString *newPath = [[oldPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:newFilename];
[[NSFileManager defaultManager] movePath:oldPath toPath:newPath handler:nil];
Both classes are explained pretty well in the docs, but leave a comment if there's anything you don't understand.
It's worth noting that moving a file to itself will fail. I had a method that replaced spaces with underscores and made the file name lowercase and renamed the file to the new name. Files with only one word in the name would fail the rename as the new name would be identical on a case-insensitive file system.
The way I resolved this was to do a two step rename, first renaming the file to a temporary name and then renaming it to the intended name.
Some pseudocode explaining this:
NSString *source = #"/FILE.txt";
NSString *newName = [[source lastPathComponent] lowercaseString];
NSString *target = [[oldPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:newName];
[[NSFileManager defaultManager] movePath:source toPath:target error:nil]; // <-- FAILS
The solution:
NSString *source = #"/FILE.txt";
NSString *newName = [[source lastPathComponent] lowercaseString];
NSString *temp = [[oldPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:[NSString stringWithFormat:#"%#-temp", newName]];
NSString *target = [[oldPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:newName];
[[NSFileManager defaultManager] movePath:source toPath:temp error:nil];
[[NSFileManager defaultManager] movePath:temp toPath:target error:nil];
I just wanted to make this easier to understand for a newbie. Here's all the code:
NSString *oldPath = #"/Users/brock/Desktop/OriginalFile.png";
NSString *newFilename = #"NewFileName.png";
NSString *newPath = [[oldPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:newFilename];
[[NSFileManager defaultManager] movePath:oldPath toPath:newPath handler:nil];
NSLog( #"File renamed to %#", newFilename );
here's a more recent example for iOS, the NSFileManager method is a little different:
NSString *newFilename = [NSString stringWithFormat:#"%#.m4a", newRecording.title];
NSString *newPath = [[newRecording.localPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:newFilename];
[[NSFileManager defaultManager] moveItemAtPath:newRecording.localPath toPath:newPath error:nil];
For the icing on top, a category on NSFileManager:
#implementation NSFileManager (FileManipulations)
- (void)changeFileNamesInDirectory:(NSString *)directory changeBlock:(NSString * (^) (NSString *fileName))block
{
NSString *inputDirectory = directory;
NSFileManager *fileManager = [NSFileManager new];
NSArray *fileNames = [fileManager contentsOfDirectoryAtPath:inputDirectory error:nil];
for (NSString *fileName in fileNames) {
NSString *newFileName = block(fileName);
NSString *oldPath = [NSString stringWithFormat:#"%#/%#", inputDirectory, oldFileName];
// move to temp path so case changes can happen
NSString *tempPath = [NSString stringWithFormat:#"%#-tempName", oldPath];
NSString *newPath = [[oldPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:newFileName];
NSError *error = nil;
[fileManager moveItemAtPath:oldPath toPath:tempPath error:&error];
if (error) {
NSLog(#"%#", [error localizedDescription]);
return;
}
[fileManager moveItemAtPath:tempPath toPath:newPath error:&error];
if (error) {
NSLog(#"%#", [error localizedDescription]);
}
}
}
#end