473,378 Members | 1,106 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

How to display the last n lines from a text file


I am asked to write a simple program to displays the last n lines from a
given text file. But I have no ideas how C defines a "line" in a text
file. How does it tell if it is the end of the line, is there such thing
call EOL like the EOF?
--
Posted via http://dbforums.com
Nov 13 '05 #1
14 7378
On Fri, 12 Sep 2003 00:23:19 -0400, nic977 <me*********@dbforums.com>
wrote:

I am asked to write a simple program to displays the last n lines from a
given text file. But I have no ideas how C defines a "line" in a text
file. How does it tell if it is the end of the line, is there such thing
call EOL like the EOF?


You open the file in text mode, e.g:

FILE *f = fopen( "example.txt", "r" );

In this mode all newlines are translated to the integer '\n'.

Nick.

Nov 13 '05 #2
On Fri, 12 Sep 2003 00:23:19 -0400, nic977 <me*********@dbforums.com>
wrote in comp.lang.c:

I am asked to write a simple program to displays the last n lines from a
given text file. But I have no ideas how C defines a "line" in a text
file. How does it tell if it is the end of the line, is there such thing
call EOL like the EOF?


Yes, it is named '\n'.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #3
nic977 wrote:
I am asked to write a simple program to displays the last n lines from a
given text file. But I have no ideas how C defines a "line" in a text
file. How does it tell if it is the end of the line, is there such thing
call EOL like the EOF?


No; you just look for newlines. The simplest way to do what you want
is to look at each character in the file, and count how many are newlines.

Nov 13 '05 #4
nic977 <me*********@dbforums.com> wrote:

I am asked to write a simple program to displays the last n lines from a
given text file. But I have no ideas how C defines a "line" in a text
file. How does it tell if it is the end of the line, is there such thing
call EOL like the EOF?


C does not define a line in a text file.

To find an end-of-line read the file one character at a time and watch
out for '\n', it's C's representation of a newline-character.

Or use the fgets function, it reads a whole line of input (when provided
with a properly sized buffer).

And, please, read the faq-list:

http://www.eskimo.com/~scs/C-faq/top.html
Irrwahn
--
do not write: void main(...)
do not cast the return value of malloc()
do not fflush( stdin )
read the c.l.c-faq: http://www.eskimo.com/~scs/C-faq/top.html
Nov 13 '05 #5
Never tried that, but a fast way might be to:
1.open file in binary mode
2.start reading backwards (and from its end) and scanning for '\n' (new
line)
3.when you locate n times the '\n' then you have found your last n lines
from the file.

--
Elias
http://lgwm.org/
"nic977" <me*********@dbforums.com> wrote in message
news:33****************@dbforums.com...

I am asked to write a simple program to displays the last n lines from a
given text file. But I have no ideas how C defines a "line" in a text
file. How does it tell if it is the end of the line, is there such thing
call EOL like the EOF?
--
Posted via http://dbforums.com

Nov 13 '05 #6

(snip about finding the last n lines in a text file)

"lallous" <la*****@lgwm.org> wrote in message
news:bj************@ID-161723.news.uni-berlin.de...
Never tried that, but a fast way might be to:
1.open file in binary mode
2.start reading backwards (and from its end) and scanning for '\n' (new
line)
3.when you locate n times the '\n' then you have found your last n lines
from the file.


I think you have to test for seekable source first. If it isn't seekable
then you need to read all the lines and save the lines in some kind of
buffer.

-- glen
Nov 13 '05 #7
lallous wrote:

"nic977" <me*********@dbforums.com> wrote in message
news:33****************@dbforums.com...

I am asked to write a simple program to displays the last n lines from a
given text file. But I have no ideas how C defines a "line" in a text
file. How does it tell if it is the end of the line, is there such thing
call EOL like the EOF?
--
Posted via http://dbforums.com
Never tried that, but a fast way might be to:
1.open file in binary mode
2.start reading backwards (and from its end) and scanning for '\n' (new
line)
3.when you locate n times the '\n' then you have found your last n lines
from the file.

You're kidding. Read backwards? How, exactly?
--
Joe Wright mailto:jo********@earthlink.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 13 '05 #8
Joe Wright wrote:
Never tried that, but a fast way might be to:
1.open file in binary mode
2.start reading backwards (and from its end) and scanning for '\n' (new
line)
3.when you locate n times the '\n' then you have found your last n lines
from the file.
You're kidding. Read backwards? How, exactly?


Well, I don't think it's a good approach in general - only works on
seekable files, for one thing - but it doesn't seem hard. fseek(),
fread, scan the buffer backwards, repeat as needed. Kind of messy
but workable.

--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo 'g******@cbobk.pbz' | rot13
Nov 13 '05 #9
Irrwahn Grausewitz <ir*****@freenet.de> writes:
nic977 <me*********@dbforums.com> wrote:

I am asked to write a simple program to displays the last n lines from a
given text file. But I have no ideas how C defines a "line" in a text
file. How does it tell if it is the end of the line, is there such thing
call EOL like the EOF?


C does not define a line in a text file.


Yes it does, see C99 7.19.2#2:

A text stream is an ordered sequence of characters
composed into lines, each line consisting of zero or more
characters plus a terminating new-line character.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Nov 13 '05 #10
Tom Zych wrote:

Joe Wright wrote:
Never tried that, but a fast way might be to:
1.open file in binary mode
2.start reading backwards (and from its end) and scanning for '\n' (new
line)
3.when you locate n times the '\n' then you have found your last n lines
from the file.

You're kidding. Read backwards? How, exactly?


Well, I don't think it's a good approach in general - only works on
seekable files, for one thing - but it doesn't seem hard. fseek(),
fread, scan the buffer backwards, repeat as needed. Kind of messy
but workable.

Bear with me Tom, fseek() what exactly? fread() a text file? In binary
mode? I think not. You can't read backward. Let's start over..

The term 'lines' suggests a 'text' file. A line is a series of zero or
more characters terminating with a '\n' character. There may be any
number of lines in a text file. The last line of the text file may or
may not be terminated with a '\n' character.
--
Joe Wright mailto:jo********@earthlink.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 13 '05 #11
Joe Wright wrote:
Tom Zych wrote:
Well, I don't think it's a good approach in general - only works on
seekable files, for one thing - but it doesn't seem hard. fseek(),
fread, scan the buffer backwards, repeat as needed. Kind of messy
but workable.

Bear with me Tom, fseek() what exactly? fread() a text file? In binary
mode? I think not. You can't read backward. Let's start over.. The term 'lines' suggests a 'text' file. A line is a series of zero or
more characters terminating with a '\n' character. There may be any
number of lines in a text file. The last line of the text file may or
may not be terminated with a '\n' character.


It's my understanding that C makes no fundamental distinction
between "text" files/streams and "binary" ones. The "r"/"rb"
business in fopen is just there for line-terminator translation.
So we can treat the input file as a bunch of bytes and read it any
way we want.

Here's an outline of the algorithm I was thinking of. It would
need a lot more work but I think it shows the approach is
workable, if messy.

--------------------------------------------------
/* assumes the input file is seekable */
/* just an outline, not compiled or tested, no error checking */

#include <stdio.h>

#define SIZE 1024
#define FILENAME "whatever"

int main(void)
{
FILE *in;
char buf[SIZE];
long file_size, pos;
size_t ct;

in = fopen(FILENAME, "rb");

fseek(in, 0L, SEEK_END);
file_size = ftell(in);

pos = file_size - (file_size % SIZE);
while (pos >= 0) {
fseek(in, pos, SEEK_SET);
ct = fread(buf, 1, SIZE, in);
// if last block, check for final \n
// scan first ct bytes of buf backwards, counting \n's
// if we hit 10, compute the offset and break
pos -= SIZE;
}

// other stuff here

return 0;
}
--------------------------------------------------

--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo 'g******@cbobk.pbz' | rot13
Nov 13 '05 #12
Tom Zych wrote:
Joe Wright wrote:
Tom Zych wrote:
> Well, I don't think it's a good approach in general - only works on
> seekable files, for one thing - but it doesn't seem hard. fseek(),
> fread, scan the buffer backwards, repeat as needed. Kind of messy
> but workable.

Bear with me Tom, fseek() what exactly? fread() a text file? In binary
mode? I think not. You can't read backward. Let's start over..

The term 'lines' suggests a 'text' file. A line is a series of zero or
more characters terminating with a '\n' character. There may be any
number of lines in a text file. The last line of the text file may or
may not be terminated with a '\n' character.


It's my understanding that C makes no fundamental distinction
between "text" files/streams and "binary" ones.


The world is stranger than you understand...
The "r"/"rb"
business in fopen is just there for line-terminator translation.


Exactly. If you read a text file as binary, then the the line
terminator will not be translated. It could appear as anything;
a single arbitrary character, a sequence of characters, or nothing
at all.

Conversely, treating a text stream as binary can lead to spurious
'\n's being injected into the stream or to characters being
removed. On some platforms this breaks many stdin -> stdout
binary filter program (stdin and stdout are supposed to be text
streams).

Twirlip

Nov 13 '05 #13
Twirlip wrote:
It's my understanding that C makes no fundamental distinction
between "text" files/streams and "binary" ones.
The world is stranger than you understand... The "r"/"rb"
business in fopen is just there for line-terminator translation.

Exactly. If you read a text file as binary, then the the line
terminator will not be translated. It could appear as anything;
a single arbitrary character, a sequence of characters, or nothing
at all. Conversely, treating a text stream as binary can lead to spurious
'\n's being injected into the stream or to characters being
removed. On some platforms this breaks many stdin -> stdout
binary filter program (stdin and stdout are supposed to be text
streams).


Good timing...I recently started reading K&R2 for the first time and
just now reached Section 1.5. I'm glad this was pointed out to me
here before I read that bit, or I might have missed its importance.
If I had ever thought about it I'd have realized that there are all
kinds of systems out there that use all kinds of coding, including
EBCDIC and fixed line lengths. But only being experienced with *nix
and MS-DOS (and maybe due to stuff I read in my old Borland C
manual), I'd assumed \n and \r\n covered all possibilities. :p

So, my new understanding: if you open a stream with "r" or "w", the
program will read and/or write lines terminated with '\n', but the
actual file or whatever on the system may have a completely
different representation, and the library translates between the
two. If you open a file with "rb" or "wb", you get straight binary,
no translation. Don't mix them. And stdin, stdout, and stderr are
text, not binary. All correct?

Thanks to all for straightening me out!

--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo 'g******@cbobk.pbz' | rot13
Nov 13 '05 #14
Tom Zych <tz******@pobox.com> wrote:

<SNIP>

So, my new understanding: if you open a stream with "r" or "w", the
program will read and/or write lines terminated with '\n', but the
actual file or whatever on the system may have a completely
different representation, and the library translates between the
two. If you open a file with "rb" or "wb", you get straight binary,
no translation. Don't mix them. And stdin, stdout, and stderr are
text, not binary. All correct?

Exactely. Just to add, I found the following footnote in n843:

213The primary use of the freopen function is to change the
file associated with a standard text stream (stderr,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
stdin, or stdout), as those identifiers need not be
^^^^^^^^^^^^^^^^^
modifiable lvalues to which the value returned by the
fopen function may be assigned.

Irrwahn
--
do not write: void main(...)
do not use gets()
do not cast the return value of malloc()
do not fflush( stdin )
read the c.l.c-faq: http://www.eskimo.com/~scs/C-faq/top.html
Nov 13 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: xEM | last post by:
how can i read for example 10 last lines from a text file beginning from last line? do you have some idea? in a different manner... how can i set file position indicator one line upper in text...
4
by: umberto | last post by:
Hello, I know that I can process txt files using ASP scrips. But there is a compressed text file (with Winzip) that I want to list on my web page. Is there possibility to decompress it on server...
2
by: CsharpGuy | last post by:
I have to read a text file and parse it out to load to a db and I'm having some issues in doing it. here is what the text file looks like Dealership number: 98665362236 Location: Maryland...
6
by: magix | last post by:
Hi, when I read entries in file i.e text file, how can I determine the first line and the last line ? I know the first line of entry can be filtered using counter, but how about the last line...
2
by: sandhya2006 | last post by:
Hii, I hav a doubt in perl,Assume there is many html files in a folder I have to Take all files and display it into text file without displaying the tags.Please reply atleast for displaying...
4
by: Stupid48 | last post by:
I'm trying to do a simple task but can't seem to find a solution. How do I read lines from a text file backwards. i.e. I want to select the last 20 lines of a text file and display them in...
17
by: swuwarrior | last post by:
Whats up guys. I just decided i would try porgramming as i am a physical education major. As apart of my major i deal with alot of paper work. So i wanted to write a program to mess with all the...
4
by: Keith G Hicks | last post by:
I'm trying to read a text file and alter the contents of specific lines in the file. I know how to use streamreader to read each line of a file. I'm doing that already to get the data into a...
0
by: amel86 | last post by:
Hello everyone, My problem is i want to display data in txtfile to the xml file. For example i write www.yahoo.com in the test.txt and i want to display the link (www.yahoo.com) in the xml file....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.