You are not logged in.

#1 2009-10-25 00:16:01

nmccrina
Member
From: Atlanta, GA
Registered: 2009-10-24
Posts: 2

Vim and new lines

I just switched to Arch, so hi guys! cool

I'm using Vim to write a c program for school. The program is supposed to simply output a single rot13'd word, followed by a newline. So I did:

fprintf(output, "%s\n", result);

When I opened up the output file though, Vim didn't show a newline. I had to use two '\n' characters to get one line to show up. Exploring around, I found out that when the 'eol' option is set (and it is), vim automatically adds a new line to the file. I'm thinking that this means that Vim will ignore an ending '\n' that I output (replacing it with it's autogenerated one, or maybe it doesn't autogenerate a newline if there already is one?), but it will in fact show up in Visual Studio or whatever my professor uses to run our programs. Is this a safe assumption?


You have to know these things when you're king, you know.

Offline

#2 2009-10-25 00:31:31

Peasantoid
Member
Registered: 2009-04-26
Posts: 928
Website

Re: Vim and new lines

Yes, it will show up in your professor's VS/editor/what-have-you, assuming he does not also have a similar option enabled.

You can test this with cat:

$ cat some-file
blahblahblah$

would mean there is no newline at the end, whereas

$ cat some-file
blahblahblah
$

would mean there is.

## Actually, I'm not sure. Take what I posted with a grain of salt.

Last edited by Peasantoid (2009-10-25 00:34:34)

Offline

#3 2009-10-25 01:10:18

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: Vim and new lines

What do you mean by "Vim didn't show a newline"?  If you mean there was no blank line at the end of the file, that's expected, given the code.  If you open a file without a newline at the end, Vim will explicitly warn you with [noeol] at the bottom of the screen (you can check this by removing the \n from your program and opening the resulting file in Vim).

Every line in a conventional text document ends in a newline.  This does not mean that there is a blank line at the end, and Vim would be wrong in displaying one.  What would happen if you had a file consisting of nothing but a newline character?

Edit: Peasantoid: If I'm correctly understanding the OP's question, yes, that's right.

Last edited by Trent (2009-10-25 01:11:00)

Offline

#4 2009-10-25 01:44:01

nmccrina
Member
From: Atlanta, GA
Registered: 2009-10-24
Posts: 2

Re: Vim and new lines

Trent wrote:

What do you mean by "Vim didn't show a newline"?  If you mean there was no blank line at the end of the file, that's expected, given the code.

Well, I guess I'm used to editors (the text editor in Visual Studio, for example) where the output of my command would look like

somestring
<blank line>

in the editor. Which, as I've been reading on google, is horribly wrong big_smile. The whole issue seems rather needlessly complicated. Anyhow, thanks for the cat tip peasantoid, that will help my peace of mind!

Last edited by nmccrina (2009-10-25 01:45:33)


You have to know these things when you're king, you know.

Offline

#5 2009-10-25 02:07:14

tlvb
Member
From: Sweden
Registered: 2008-10-06
Posts: 297
Website

Re: Vim and new lines

Seems that the problem is solved, but a tip that might be relevant: When in doubt, use hexdump to see what's really in the file.


I need a sorted list of all random numbers, so that I can retrieve a suitable one later with a binary search instead of having to iterate through the generation process every time.

Offline

Board footer

Powered by FluxBB