Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added File IO in Objective-C #2135

Merged
merged 2 commits into from
Oct 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion archive/o/objective-c/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@ Welcome to Sample Programs in Objective-C!

## Sample Programs

- [Capitalize in Objective-C](https://github.com/TheRenegadeCoder/sample-programs/issues/1474)
- [Hello World in Objective-C](https://therenegadecoder.com/code/hello-world-in-objective-c/)
- [Even Odd in Objective-C](https://github.com/TheRenegadeCoder/sample-programs/issues/1486)
- [Factorial](factorial.m)
- [Fibonacci in Objective-C](https://github.com/TheRenegadeCoder/sample-programs/blob/master/archive/o/objective-c/fribonacci.m)
- [File IO in Objective-C](https://github.com/TheRenegadeCoder/sample-programs/issues/1482)
- [Fizz Buzz in Objective-C](https://github.com/TheRenegadeCoder/sample-programs/issues/1492)
- [Reverse a String in Objective-C](reverse-string.m)
- [Merge sort in Objective-C](merge-sort.m)
- [Capitalize in Objective-C](https://github.com/TheRenegadeCoder/sample-programs/issues/1474)

## Fun Facts

- Debut: 1984
- Objective-C is a superset of C, any C program can be compiled with an Objective-C compiler.

## References

Expand Down
66 changes: 66 additions & 0 deletions archive/o/objective-c/file-io.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#import <Foundation/Foundation.h>

NSString* readFileWith(NSString* path) {
NSFileHandle* fileHandle = [NSFileHandle fileHandleForReadingAtPath:path];
if (fileHandle) {
@try {
NSData* fileData = [fileHandle availableData];
return [[NSString alloc] initWithBytes:[fileData bytes] length:[fileData length] encoding:NSUTF8StringEncoding];
} @catch (NSException *e) {
NSLog(@"Error reading file %@", e);
return nil;
}
@finally{
[fileHandle closeFile];
}
}else{
NSLog(@"File to read not found %@", path);
}

};

BOOL writeFileWith(NSString* path, NSString* content) {
NSFileHandle* fileHandle = [NSFileHandle fileHandleForWritingAtPath:path];
if(fileHandle){
@try {
NSData* fileData = [content dataUsingEncoding:NSUTF8StringEncoding];
[fileHandle writeData:fileData];
return YES;
} @catch (NSException *e) {
NSLog(@"Error writing file %@", e);
return NO;
}
@finally{
[fileHandle closeFile];
}
}else{
NSLog(@"File to write not found %@", path);
}

};

BOOL createFileIfNotExistsAt(NSString* path) {
return [[NSFileManager defaultManager] createFileAtPath:path contents:nil attributes:nil];
};

int main(int argc, const char * argv[]) {
@autoreleasepool {

NSString* path = @"output.txt";
if(createFileIfNotExistsAt(path)){
BOOL result = writeFileWith(path, @"Hello!");
if (result) {
NSString* contents = readFileWith(path);
if (contents) {
NSLog(@"%@", contents);
}
}
}else{
NSLog(@"Unable to create file at %@", path);
}

return 0;

}
return 0;
}