Where to Place a .Txt File and Read from It in a iOS Project

Where to place a .txt file and read from it in a IOS project

if let path = Bundle.main.path(forResource: "README", ofType: "txt") {
do {
textView.text = try String(contentsOfFile: path, encoding: .utf8)
} catch let error {
// Handle error here
}
}

Just drop the file anywhere into the project browser and make sure it is added to the right target.

Just to expand on the answer, you can also place them in a folder and use:
+ pathForResource:ofType:inDirectory:.

How to read text file form particular directory in ios app?

If you add files and folders with settings: Create group
Sample Image

Then you shouldn't setup folder in this case (system create ipa file and put all file together in one place).
When you add files with references it create folder inside ipa. Don't forget to check this files in the target list.

create simple txt file and read from it objective c

In iOS App Bundle is the main directory. If you put your file there you can read as such:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"myFile" ofType:@"txt"];
NSString *testString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",testString);

This will read file myFile.txt

How to input a default .txt file into an iPhone through Xcode in Swift

Since it seems you want the file included with your app, do not attempt to manually install the file somewhere on your computer. That only works with the Simulator if you happen to put the file in just the right place.

Instead, add the file to your Xcode project. Then the file will be added to your app's resource bundle. Then you can access the file using NSBundle to get its path. It will not be in the app's Documents folder. It will be in the app's bundle.

See Where to place a .txt file and read from it in a IOS project for more details.

My C program reads a text file. When I put it into Xcode it fails. What's happening?

Make sure that the input.txt file is not in the project folders, but in the same folder as the compiled program. You can find this by right selecting the executable under the 'Products' folder in the left pane of Xcode and selecting "Show in finder" and then move your .txt file there, and run it again.

example image

Where to put txt file when running iOS simulator

Drag it into your project. When asked if it should be part of the app target, make sure it is. The result is that when you build the app, the file will be copied into the app bundle and thus will make its way onto the target device as part of the app, where your code can retrieve it, along these lines:

NSString* f = [[NSBundle mainBundle] pathForResource:@"myfile" ofType:@"txt"];
NSError* err = nil;
NSString* s = [NSString stringWithContentsOfFile:f
encoding:NSUTF8StringEncoding
error:&err];

Read .txt file in .c file for iOS

The path to Documentsdirectory is accessible like this in objective-C:

NSString *documentsPath = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES)[0];

When using such a NSString path in C, you have to convert your NSString to UTF8 string:

NSString *fullPath = [documentPath stringByAppendingPathComponent:@"myFile.txt"];
File *f = fopen(fullPath.UTF8String, "r");

Be aware that fopen() C function need to be passed the absolute path to the desired file to be able to open it.

If fopen() returns NULL, you can check what went wrong by checking errno for the error number and strerror() function (from ) to have a human readable explanation:

#include <stdio.h>
#include <errno.h>
#include <string.h>

File *fileHandler = fopen( fullPathToFileAsUTF8String, openMode );
if( fileHandler == NULL ) {
fprintf(stderr, "File open error: %s\n", strerror(errno));
}


Related Topics



Leave a reply



Submit