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

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)

Related

Line terminator getting added to plain text file

I'm a little confused by some behavior I'm seeing with text files on my Mac. When I open a new file in vim and type in a single character (let's say the letter "t") into the file with no carriage return and hit save and then do a hex dump on the file (using vim's :r !xxd command), I see the following:
00000000: 740a t.
There is still a line feed oa in the file. And when I look at the file properties on the file, there are two bytes, not one. How did it get in there if I didn't type it?
Ok, so it turns out vim automatically adds a newline character at the last line to comply with Posix standard that all lines must end with a new line. You can turn this off with :set noeol in vim.

Removing blank line at end of string before writing to text file?

Been searching around for this for a couple hours, can't find anything which will do this correctly. When writing a string to a text file, a blank line is outputted at the end.
writeString = New StreamWriter(path, False)
writeString.WriteLine("Hello World")
writeString.Flush()
writeString.Close()
This will write the following to file:
Hello World
(Blank Line)
I've tried removing last character of string (both as regular string with varString.Substring(0, varString.Length - 1) and also as a list of string with varList.RemoveAt(varList.Count - 1)) but it just removes the literal last character.
I've also tried using Replace(vbCrLf, "") and many variations of it but again, they only remove literal new lines created in the string, not the new line at the end that is magically created.
Preferably, I'm seeking a method which will be able to remove that magical newline before the string is ever written to the file. I found methods which read from the file and then write back to it which would require Write > Read > Write, but in all cases the magical new line still appeared. :(
If it's important to note: The file will contain a string which may contain actual new lines (it's 'Song Artist - Song Title', though can contain other information and new lines can be added if the user wishes). That text file is then read by other applications (such as mIRC etc) of which output the contents by various means depending on application.
Eg. If an application were to read it and output it into a textbox.. the new line will additionally output to that textbox.. which is a problem! I have no control of the applications which will read the file as input considering it's the client which decides the application, so the removal of the new line needs to be done when outputted.
Help is appreciated~!
Use the Write method instead of WriteLine. The WriteLine method is the one adding a blank 0 length line to the file because it is terminating the "Hello World" string with a newline.
writeString.Write("Hello World")

Copying a text file line by line vb.net half the size?

I'm trying to modify specific lines in a 6 gig text file (SQL script). So I read it in with IO.StreamReader.ReadLine and write to a new file with IO.StreamWriter.WriteLine. If the line matches a certain condition, I'm modifiying it before I write it.
The problem is, the resulting file is exactly half (1.999582...) the size of the original file...
I'm trying to make sure the encoding is the same using:
sw = New IO.StreamWriter(NewFilepath, False, sr.CurrentEncoding)
But it doesn't make a difference, the new file is half the size of the old...
Where are you setting the encoding for your StreamReader, sr? If you are not doing this explicitly, and if you are setting the encoding of the StreamWriter before you perform any reads of your file(my best guess), then the CurrentEncoding of the StreamReader may change (it autodetects from the source file).
From MSDN on StreamReader.CurrentEncoding
The current character encoding used by the current reader. The value
can be different after the first call to any Read method of
StreamReader, since encoding autodetection is not done until the first
call to a Read method.
To determine the encoding you can read off the first line of the file with the StreamReader and then do :
sw = New IO.StreamWriter(NewFilepath, False, sr.CurrentEncoding)

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.

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

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.