How to read the contents of .doc file into string in XCode? - objective-c

How do I read the contents of .doc file [not from resource file] into NSString in Objective-C?
I tried doing it in this way:
NSString *str = [NSString stringWithContentsOfFile:#"/User/home/Documents/config.doc"];
NSLog(#"Contents of file : %#",str);
OUTPUT:
-+-% [encoded format]
output is in encoded format
How do I solve this problem? Is it not reading from file the proper contents or am I printing it wrong?
Thank You.

The file you are using is probably binary, so when viewed as a string it will not work like you think. You would need some sort of library or decoding function that would parse and display a binary .doc file.
Though you may have more luck with a .docx file which I believe is an XML based format that Word can save.

Related

Insert unicode symbol into label from array

I want to insert a unicode symbol for pi, which is \u03c0 into a label and for it to display the symbol. I am loading this in from an array which was read from a txt file. For example if I have a txt file that contains "\u03c0":
string = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]
array[i] = string;
label.text = array[i];
What am getting is "\u03c0" as an output in the textfield, but I want the symbol. What I am doing wrong?
Edit: it seems that my problems is with string encoding because I am reading in the array from a file. I was using NSUTF8StringEncoding. What should this be changed to to allow unicode?
My guess is the contents of your file contains \\u03c0 rather than the actual character. If you have control of the file contents, paste in the actual character, not the sequence, because the editor will save it with the escaping "\". If you don't have control, i suggest writing code to detect this escaping, strip the preceding "\" and then use the result in your format.

Why does a one line txt file take longer to open?

I have downloaded a book from project gutenberg that is about 3 mbs in size and I am annoyed with all line breaks and whitespace in the file so I used the following code on the content string of the txt file:
[[txtFileContentString componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] componentsJoinedByString:#" "];
Later on I use the nsstring method writeToFile to write the string to a file.
Now everything is fine but the file takes 2-3 seconds to open while before the it opened instantaneously. I do not know mush about working with txt files but I guess it has something to do with the this new file having one line... Both files are encoded in utf8. So I am wondering why does it take longer to load the txt file and is there another way to do this?
(The reason I want to get rid of the line breaks is that when I load the txt file contents into my nstextview I get awful line breaks in middle of my lines, because my nstextview has another width than that set for the project gutenberg file)

What is the easiest way to write from an Array to a CSV file?

What is the easiest way to export from an array to a CSV file? I was planning on using a for loop and inserting commas into the string, but realized I don't know how to change the txt file into a CSV file. Thanks for the help!
I always use
NSString *csvString = [/*name of array*/ componentsJoinedByString:#","];
//Create file manager, pick path, etc
[/*name of file manager*/ createFileAtPath:/*file path*/ contents:[csvString dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
I only ever use it to create a .csv file from an array of floats or strings. If you have formatting characters in your data you might have to call an escape-addition method before you generate the .csv to get everything to look like you want.
If you want multiple lines in a .csv then you need to add \n at each line break (if you don't already have it).
just save/open the file as .csv file. for example, if you now save/open as test.txt, just use test.csv
BTW, if the text value has ',' in it, you need use " " for that field. otherwise, it will not be a 100% compatible csv file.

rtf bullet character in objective c

Does anyone know if this line of code would work for a NSString from an rtf file on iOS?
NSString* cList = [[NSString alloc] initWithContentsOfFile:#"name of file" encoding:NSUTF8StringEncoding error:nil];
c = [cList componentsSeparatedByString:#"\n• "];
I'm just wondering since it includes a bullet point character which I pretty much copy pasted. I wasn't expecting it to be that easy. It just seems like it should be an escape sequence character or something.
Probably should've included some form of error checking in the first line, but that aside for the moment.
Update: After much compiling with no success with an rtf, I copied the text into a txt and used that instead. Works the first time. Seemed like the rtf reading was getting weird rtf data that wasn't really what I was after when I tried to NSLog it.
Thanks!
How about using unicode sequence?
like...
c = [cList componentsSeparatedByString:#"\n\u0000 "];
0000 <---- unicode.

Problem creating UTF8 text file with NSFileHandle

I want to use NSFileHandle to write large text files to avoid handling very large NSString's in memory. I'm having a problem where after creating the file and opening it in the Text Edit app (Mac), it is not displaying the unicode characters correctly. If I write the same text to a file using the NSString writeToFile:atomically:encoding:error: method, Text Edit display everything correctly.
I'm opening both the files in Text Edit with the "opening files encoding" option set to automatic, so I'm not sure why one works and the other method doesn't. Is there some form of header to declare the format is UTF8?
// Standard string
NSString *myString = #"This is a test with a star character \u272d";
// This works fine
// Displays: "This is a test with a star character ✭" in Text Edit
[myString writeToFile:path atomically:YES encoding:NSUTF8StringEncoding];
// This doesn't work
// Displays: "This is a test with a star character ‚ú≠" in Text Edit
[fileManager createFileAtPath:path contents:nil attributes:nil];
fileHandle = [NSFileHandle fileHandleForWritingAtPath:path];
[fileHandle writeData:[myString dataUsingEncoding:NSUTF8StringEncoding]];
The problem is not with your code, but with TextEdit: It doesn't try to decode the file as UTF-8 unless it has a UTF-8 BOM identifying it as such. Presumably, the first version of your code adds such a BOM. See this question for further discussion.
UTF-8 data generally should not include a BOM, so you probably shouldn't modify your code from the second version at all—it's working correctly. If opening the file in TextEdit has to work, you should be able to force the BOM by including it (\ufeff) explicitly at the start of the string, but, again, you should not do that unless you really need to.