How could I make it so that I can split up a string read from a file into separate strings or make it only read certain line like if i wanted it to only read the first line one the text file and store it into a string. Here is the code i use to read a text file.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pday11 = [documentsDirectory stringByAppendingPathComponent:#"day11.txt"];
NSString *stringFromFileday11 = [NSString stringWithContentsOfFile:pday11 encoding:NSUTF8StringEncoding error:nil];
//If i used this to read the file how could i split up the "stringFromFileday11" into different strings where every line is a string from the file
Read to a string and split it up
NSString *str = [NSString stringWithContentsOfFile:<YOUR FILEPATH> encoding:NSUTF8StringEncoding error:nil];
NSArray *array = [str componentsSeparatedByString:#"\n"];
(e.g. Read a first line.)
NSString *firstline = [array objectAtIndex:0];
Related
I have an editable uitextview in which the user types in Russian or English. I then save this text as a .txt file in the files directory, and read it later.
Here is the portion to save the text
-(IBAction)saveText:(id)sender{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"testing.txt"];
[self.textBox.text writeToFile:path atomically:YES];
NSLog(path);
}
There is a separate button to display this text in a popover:
-(IBAction)readText:(id)sender{
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"testing.txt"];
NSError *error = nil;
myText = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
NSLog(#"myText: %#", myText);
self.textBox.text= myText;
}
with this declaration in the .h file
NSString *myText;
it works fine if I only type English, but if I type Russian characters then nothing saves/displays.
So, say I type "hello" it works, but ifI then type " some Russian phrase " to append self.textBox.text it doesn't work.
How can I make this Russian (or UTF8/charcode) compatible?
writeToFile:atomically: is deprecated and writes the string in the default encoding
(whatever that is, probably MacRoman). Better use
[self.textBox.text writeToFile:path
atomically:NO
encoding:NSUTF8StringEncoding
error:&error];
to write the string UTF-8 encoded.
I have text file text.txt in my xcode project and I need to write NSString init from my iOS app. I've tried this code
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex: 0];
NSString *docFile = [docDir stringByAppendingPathComponent: #"text.txt"];
NSString *content = #"Test";
[content writeToFile: docFile atomically: NO];
But it doesn't work for me... So can you help me with it?
this worked for me
//Method writes a string to a text file
-(void) writeToTextFile{
//get the documents directory:
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//make a file name to write the data to using the documents directory:
NSString *fileName = [NSString stringWithFormat:#"%#/text.txt",
documentsDirectory];
//create content - four lines of text
NSString *content = #"Test";
//save content to the documents directory
[content writeToFile:fileName
atomically:NO
encoding:NSStringEncodingConversionAllowLossy
error:nil];
}
I'm working on an app that's a bit like a notebook of typed pages.
I have a UITextView *documentText and an int currentPage to keep track of multiple pages. Back and forward buttons add 1 or subtract 1 from currentPage and then they set the text in documentText to match the new value of currentPage.
However, when I run it, nothing saves, there's just a blank text field. I think it's a problem with [saveDocs writeToFile:path atomically:YES encoding:NSUnicodeStringEncoding error:NULL];
, but I'm not sure what to change, or if that's even the problem.
Anyways, here's the code I'm using to save the text:
- (IBAction)saveDocs:(id)sender {
NSString *saveDocs = documentText.text;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat: #"%#-%d.txt", #"document", currentPage]];
[saveDocs writeToFile:path atomically:YES encoding:NSUnicodeStringEncoding error:NULL];
[[NSUserDefaults standardUserDefaults] setInteger:currentPage forKey:#"CurrentDocument"];
}
Also, in ViewDidLoad, I'm using this code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
currentPage = [[NSUserDefaults standardUserDefaults] integerForKey:#"CurrentDocument"];
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat: #"%#-%d.txt", #"document", currentPage]];
NSString* doc = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
[documentText setText:doc];
Thanks for your help,
-Karl
You write with NSUnicodeStringEncoding. But you read with NSUTF8StringEncoding. Perhaps you should stick with NSUTF8StringEncoding for both writing and reading.
I want to save a text string from a file when a user presses a button. Could this file be a .plist? Then, later, I want another function to read the text from the file and turn it into a variable. How is this possible?
Snarky
Saving:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingString:#"/myFile.plist"];
//Create the file if it doesnt exists
if (![[NSFileManager defaultManager] fileExistsAtPath:path]){
NSDictionary *emptyDic = [NSDictionary dictionary];
[emptyDic writeToFile:path atomically:YES];
}
//Save the text using setObject for key or something similar, you could even use a NSArray instead
NSDictionary *dic = [[NSDictionary alloc]init];
[dic writeToFile:path atomically:YES];
Loading:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [NSString stringWithFormat:#"%#/myFile.plist", documentsDirectory];
NSString *contents = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
I think you can't use a .txt file directly but I have never tried it.
If you just want to save some user default setting for future reference, check out NSUserDefaults. Easier than dealing with files if you're really just trying to save some setting.
If you really want to read a string from a file, go to your Xcode organizer, go to documentation, click on the search icon, and type in "Reading Strings From" and one of the top links will be how to read and write to files.
I'm trying to make a txt log file with the actions that happens in my app. I want to save some text whenever the app is connecting to the server o displaying new info.
Well, how do I write to a file? I've tried using the method writeToFile: but it's not working because fileExistsAtPath: is returning NO.
My code is:
NSString *file_path = #"mylog.txt";
NSString *log = #"Hello World!!\n";
[log writeToFile:file_path atomically:YES encoding:NSUnicodeStringEncoding error:nil];
Thanks!
PS: Oh, would it be readable through Organizer with the iPhone plugged in?
You should use the < App_Home >/Documents folder for storing Documents
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *file_Path = [documentsDirectory stringByAppendingPathComponent:#"log.txt"];
[log writeToFile:file_path atomically:YES encoding:NSUnicodeStringEncoding error:nil];
But normally if you want to see and dump things while running the app,
you could just use NSLog() which outputs it in the console.
Try this method I wrote:
+(NSString*)createPath:(NSString*)fileName{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *localizedPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#",fileName]];
//NSLog(#"%#",localizedPath);
return localizedPath;
}
It will return you a path for your file. You only need to give it a name.