Tried to read a text file line by line. But line is split into two and getting stored on next line - c#-4.0

I have a text file with special characters as well as normal characters. I am trying to read this file line by line. I have used
string[] lines = System.IO.File.ReadAllLines("Trial.txt");
To read it.
I used a break point and tried to find out the values stored in those lines. It broke some of the lines in between without finishing reading it and stored the rest in a new next line. When I checked the records, I found that the breaking occurs only at the point where there are special characters even though it doesn't happen with a particular special character. If the file has a total of 10 lines and if there is 1 line which has this problem, it reads a total of 11 lines. Can any of you guys pleas help me out with this? The text file is in UTF-8 format.

The File.ReadAllLines method splits the file on carriage return ('\r'), new line ('\n'), or a carriage return followed by a new line (taken from here: http://msdn.microsoft.com/en-us/library/s2tte0y1.aspx).
Check if the line that is not supposed to be split has either of those characters (judging from your reply to Luke Wyatt you probably have a new line ('\n') on that line at the point where it splits).

Related

Writing to a text file in seperate lines

I am creating a program, which at the end of all the inputs, I write to a text file and it comes up as one big line. Other than going into my text file and manually changing it to multiple lines, how do you write to a python text file in separate lines?
Eta:
Line one that holds one piece of inputted information
Line two that holds another piece of inputted information
Line three that holds a final piece of inputted information
I've tried writing twice to a file before closing it, but it returns an error saying it expected 1 argument and received more.
You should post at least a failed attempt that we can fix; but, due to the simplistic nature of the problem, I'll just give a quick answer anyway.
Steps:
Open the file in write ('w') mode (note that this blanks the file)
Write a line
Write a new-line ('\n'). Note that this step can be combined with the previous
Repeat steps 2 and 3 for all your lines
Close the file.
So, here's an implementation of the above. Note how we can use a with statement to do the first and last steps in one (and other benefits).
lines = ['Line one', 'Line two', 'Line three']
with open('your_file.txt', 'w') as f:
for l in lines:
f.write(l + '\n')

remove all empty lines from text files while keeping format

I have multiple notepad text files which contains one empty line (the last line of each file). I want to delete the empty line form all files. I tried different grep and awk lines but they didn't work plus they messed up the file format; all text are shown on one line instead of separate line. i also tried with notepad++ regex to find ^\s*$ and replace it with nothing, but it also didn't work.
Current text file looks like this:
apples
oranges
peaches
[empty line]
The output should be
apples
oranges
peaches
Ctrl+H
Find what: \R^$
Replace with: LEAVE EMPTY
check Wrap around
check Regular expression
Replace all
Explanation:
\R : any kind of linebreak
^ : begining of line
$ : end of line
Result for given example:
apples
oranges
peaches
If you want to delete the last line of the file, use sed '$d'. If you want to do that only when the last line is empty, use sed '${/^$/d;}' (This treats a line with some whitespace as a non-blank line, so you might prefer sed '${/^ *$/d;}' or some variant.
The "empty last line" may be a matter of interpretation. From the wikipedia "Newline" article:
Two ways to view newlines, both of which are self-consistent, are that newlines either separate lines or that they terminate lines. If a newline is considered a separator, there will be no newline after the last line of a file. Some programs have problems processing the last line of a file if it is not terminated by a newline. On the other hand, programs that expect newline to be used as a separator will interpret a final newline as starting a new (empty) line. Conversely, if a newline is considered a terminator, all text lines including the last are expected to be terminated by a newline. If the final character sequence in a text file is not a newline, the final line of the file may be considered to be an improper or incomplete text line, or the file may be considered to be improperly truncated.
In my little world, the Visual Studio Code editor takes the former view; vim the latter.

Get the last line of a file in groovy

Google doesn't give any great results for getting the last line of a file in groovy so I feel this question is necessary.
How does one get the last line of a file in groovy?
Get all lines, then get the last one:
def lines=new File("/home/user/somefile.txt").readLines()
def lastline=lines.get(lines.size()-1)
Or, if the file is dangerously large:
def last=new File('/home/user/somefile.txt').withReader { r-> r.eachLine {it} }
which is almost identical to asker's answer.
Here's what I ended up with:
new File("/home/user/somefile.txt").eachLine {
lastLine = it
}
.eachLine iterates through each line of the text file and lastLine is set to the current iteration until eachLine finishes going through the file. Pretty straightforward.
How does one get the last line of a file in groovy?
It depends on what you know about the file. If the lines in the file are fixed length then you could look at the file size, divide it by the length of each line and then use random file access to jump to the last line in the file and read it. That would be more efficient than reading the entire file. If you don't know the length of the lines then you are probably going to have to read the entire file and discard everything before the last line you read.

What is the difference between a "line feed" and a "carriage return"?

If there are two keywords then they must have their own meanings. So I want to know what makes them different and what their code is.
A line feed means moving one line forward. The code is \n.A carriage return means moving the cursor to the beginning of the line. The code is \r.
Windows editors often still use the combination of both as \r\n in text files. Unix uses mostly only the \n.
The separation comes from typewriter times, when you turned the wheel to move the paper to change the line and moved the carriage to restart typing on the beginning of a line. This was two steps.
In very layman language Enter key press is combination of carriage return and line feed.
Carriage return points the cursor to the beginning of the line horizontally and Line feed shifts the cursor to the next line vertically. Combination of both gives you the new line (\n) effect.
Reference: https://en.wikipedia.org/wiki/Carriage_return#Computers
Both of these are primary from the old printing days.
Carriage return is from the days of the teletype printers/old typewriters, where literally the carriage would return to the next line, and push the paper up. This is what we now call \r.
Line feed LF signals the end of the line, it signals that the line has ended - but doesn't move the cursor to the next line. In other words, it doesn't "return" the cursor/printer head to the next line.
For more sundry details, the mighty Wikipedia to the rescue.
Both "line feed' (0x0A or 10) and 'carriage return' (0x0D or 13) are single-byte values. These values are the accepted standard for LF/CR. Most languages will type these as 'characters.' You can find these values on any standard ASCII table.
For example, in C# a string such as:
String str = "\n\r";
is two characters long (ignoring the hidden end null character '0x00' required in string types). However, you could make an equivalent array of type character such as:
char[] c = new char[](){0x0A,0x0D}; // LF, CR

Can you force Vim to show a blank line at the end of a file?

When I open a text file in Notepad, it shows a blank line if there is a carriage return at the end of the last line containing text. However, in Vim it does not show this blank line. Another thing I've noticed is that the Vim editor adds a carriage return to the last line by default (even though it doesn't show it). I can tell, because if I open a file in Notepad that was created in Vim, it shows a blank line at the end of the file.
Anyway, I can live with these two differences, but I'm wondering if there is an option in Vim that allows you to toggle this behaviour.
Thanks
PS - GVim 7.2
[Update]
Would this make sense to be on Server Fault instead?
[Update 2]
I'll rephrase this... I need to know when there is a carriage return at the end of single line file (Notepad shows an extra line with no text, with Vim I cannot tell). This is due to a Progress program that reads a text file (expects a single line, but with a carriage return) and parses the text for some purpose. If there is no carriage return, Progress treats the line as if it is null.
[Workaround Solution]
One way I've found to ensure there is a carriage return (but make sure I don't add a second one) is to make sure I have the end of line write option turned on (:set eol) and then just do a write/save. This will put an end of line in the file if it's not already there. Otherwise, it doesn't add a new one.
:help endofline
explains how you could stop vim from adding an extra newline.
It seems that vim treats newline as a line terminator, while notepad treats it as a line separator: from http://en.wikipedia.org/wiki/Newline
There is also some confusion whether
newlines terminate or separate lines.
If a newline is considered a
separator, there will be no newline
after the last line of a file. The
general convention on most systems is
to add a newline even after the last
line, i.e., to treat newline as a line
terminator. Some programs have
problems processing the last line of a
file if it isn't newline terminated.
Conversely, programs that expect
newline to be used as a separator will
interpret a final newline as starting
a new (empty) line. This can result in
a different line count being reported
for the file, but is otherwise
generally harmless.
If I recall correctly, on unix-y systems a text file must be terminated with a newline.
One useful Vim option is
set list
It will help you see all end of lines characters (and possibly other generally invisible chars). So you will be able to view this last endofline directly in Vim and not only in Notepad.
When you open the file in VIM the status line should say [noeol] after the filename. So that's one indication. As Manni said, you can change this by setting both the endofline option off and the binary option on. You can set this as your default settings in a .vimrc file.

Resources