LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 04-24-2007, 10:36 AM   #1
nocturna_gr
Member
 
Registered: Feb 2004
Location: Athens, Greece
Distribution: Gentoo,FreeBSD, Debian
Posts: 704

Rep: Reputation: 30
is EOF equal to CR LF


Hi,
what's the difference between EOF and CR followed by LF? Do texts end with EOF or with CR and LF?
 
Old 04-24-2007, 11:06 AM   #2
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-12/ fc-11-live-usb/ aix
Posts: 1,932

Rep: Reputation: 157Reputation: 157
my understandding is that eof is the same thing as null (ascii-0).

a cr (ascii-13) followed by lf (ascii-10) is how windows delineates lines. i think posix does it with just a line feed.

please someone correct me if i got it confused.
 
Old 04-24-2007, 11:53 AM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 9,898

Rep: Reputation: 336Reputation: 336Reputation: 336Reputation: 336
Typically the end of file marker for ASCII text files is ctrl-z. CR and LF are just end of line markers depending on operating system.

Last edited by michaelk; 04-24-2007 at 11:54 AM.
 
Old 04-24-2007, 12:50 PM   #4
HowDoIProgramIt
Member
 
Registered: Nov 2006
Location: East Coast, USA (in "the great northeast")
Distribution: Custom / from source; Fedora, Debian, CentOS, Scientific; LFS.
Posts: 94

Rep: Reputation: 15
Quote:
Originally Posted by nocturna_gr
Hi,
what's the difference between EOF and CR followed by LF? Do texts end with EOF or with CR and LF?
The main difference is that <CR> and <LF> are characters, while EOF is more of a concept - though the end of a file can be denoted by a special marker character, too.

Generally speaking, under Windows and DOS, lines of text in a file end with the two byte character sequence <CR><LF>; these characters have the ASCII values 13 and 10, respectively. Under *nix, a line of text ends with just ASCII character # 10.

Under DOS and Windows, a file might also contain an end of file marker character; the ASCII value of this character is 0x1A (26 decimal). This character was used more to speed things up when working with certain kinds of files (text files, as opposed to binary files) than anything.

Exactly how you open a file, read from it, or write to it, will vary with the programming language or tool that you're using; there are a lot of commonalities between the majority of them, though. Hopefully, a few examples in C will help clear this up. A few important points first, though: a carriage return (CR) - ASCII character code 13 - is also represented by the escape sequence "\r" (not including the quote marks), and a line feed (LF) - ASCII character code 10 - is also represented by the escape sequence "\n" (again, not including the quote marks).

Under DOS or Windows - assuming that we're using the C run-time library (RTL) and not the Windows application programming interface (API), or DOS function calls - you have to tell the RTL how you would like it to treat the file. Your option are "as a text file" and "as a binary file". If you tell the RTL to treat a file as a text file, the following things will happen:

When the sequence <CR><LF> is encountered in a file, it's replaced, "behind the scenes", by just <LF>, which is ASCII character 10. Which matches the constant '\n'.

If the end-of-file character is encountered, the file's end-of-file flag is set: fgetc() returns the value EOF (defined in stdio.h); feof() will return true (non-zero), and so on.

However, if you tell the RTL to treat the file as a binary file, the data is returned to you "raw" - no end-of-line marker translation is done, you get both the <cr> and the <lf>; and, the end-of-file marker - if present - is treated just like any other byte in the file; it's value is returned to you.

With most compilers, under Windows or DOS, in C, a file is opened as a binary file by using fopen() and specifying the letter "b" after the mode you wish to open the file in (r, w, a, etc.). For example, to open a file named "test.dat" in read-only mode, in binary mode (without any translation of the file's contents), one could write:

Code:
FILE *f;

if (!(f = fopen(f,"rb")))
{
   /* an error occurred */
}
A note regarding porting code between DOS/Windows and *nix systems: if you were to attempt to compile the above code on a *nix system, the compiler might or might not give you an indication that there was a problem: generally speaking, *nix systems always open files in "binary" mode, and "rb" (or "wb", "ab", etc.) is not a valid parameter. Exactly what would happen can be expected to vary from compiler to compiler; on some, the presence of the "b" might cause the fopen() call to fail; others might print a warning message.

One last note regarding end-of-file and translation of characters in "text" mode on DOS and Windows systems: while, in theory, the rules are simple, in practice, they're a lot more complicated. If you were to take a program that used text files in "text" mode, and compile it, on a DOS / Windows system, with different compilers from different vendors, you can expect that, to some extent, you'll get different results. One of the ways they differ in is in how the final <cr><lf> pair in a file is treated. In "text" mode, sometimes, you'll get a final "\n", and other times you won't. In "binary" mode, you'll always get the entire contents of the file.

Good luck!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Equal Bandwidth Distribution roopunix Linux - Networking 1 03-22-2007 03:30 AM
is this equal? shanenin Linux - Software 1 03-30-2004 01:08 PM
Proxomitron equal for linux. Likosin Linux - Software 1 09-06-2003 06:31 PM
What is linux equal to DirectInput? HelloKitty Linux - Software 1 08-07-2003 06:08 PM
DirectX equal for Linux X11 Linux - General 3 01-28-2002 05:50 PM


All times are GMT -5. The time now is 09:36 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration