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
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.
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 Documents
directory 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
Alamofire No Such Module (Cocoapods)
iPhone - Didselectrowatindexpath: Only Being Called After Long Press on Custom Cell
I am Getting Unsupported Parameter Combination Cgbitmap Error with Swift
Change the Color of a Link in an Nsmutableattributedstring
Info.Plist Utility Error: "Info.Plist Couldn't Be Opened Because There Is No Such File"
How to Import Existing Objective C Classes in Swift
Latitude and Longitude Points from Mkpolyline
Use Logical Operator as Combine Closure in Reduce
Set a Passwordfield to Securetextentry Give Me a Strange Behaviour
How to Debug an Issue with a Release Mode Build in iOS
How to Use Mbprogresshud with Swift
How to Clear All Cached Images Loaded from Sdwebimage
Header Displaced in Tableview with Uirefreshcontrol
While Mail Sending Process Using iOS 8 Simulators Generates Error
Set Different Activity Items for Uiactivityviewcontroller Swift