473,583 Members | 3,155 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

determine last line of file entries

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 of entry in EOF while loop ?

while (! file.eof() )
{
....
}

Sep 29 '06 #1
6 5774
magix wrote:
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 of entry in EOF while loop ?

while (! file.eof() )
This is not standard C.
Do you mean feof(file)?
Then this is also wrong.
{
....
}
char *firstLine, *secondToLastLi ne, *lastLine;
Read the first line. Keep this one via firstLine. Set lastLine =
firstLine.
Try reading the second line, return on failure with firstLine and
lastLine, on success set lastLine to this second line.
Try reading the third line, on failure: as above, on success set
secondToLastLin e = lastLine and afterward lastLine to the third line.
From then on:
Read line; on failure: discard secondToLastLin e, return with firstLine
and lastLine; on success: discard secondToLastLin e, set secondToLastLin e
= lastLine and lastLine to current line.

Reading lines: I recommend fggets() from
http://cbfalconer.home.att.net/download/
Discarding lines: Means in this case free().
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Sep 29 '06 #2
magix wrote:
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 of entry in EOF while loop ?

while (! file.eof() )
{
....
}
i don't know exactly what you mean by filtered. but it sounds like you
want to know which line is the first and which is the last? if so,
whats wrong with using a loop to count number of '\n'. when n = last
line then you do whatever you need to do on that line.

Sep 29 '06 #3

"magix" <ma***@asia.com wrote in message
news:45******** **@news.tm.net. my...
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 of entry in EOF while loop ?

while (! file.eof() )
{
....
}
This sounds a bit like someone's homework to me, so I didn't post complete
code for you... Why? Because, it's rare that one ever needs to know if it
is the last line in the file... If it is, you'll still have some work to
do. If it isn't, good luck.

This is slightly more complete and should get you started. You'll need to
add checks to make sure fopen, etc., actually worked. Also, note that feof
is being used to detect eof.

FILE *file;

strcpy(file,arg v[1]);
fopen(file,"rb" );
while (!feof(file))
{ /* ... */}

I almost never open a file in text mode, always as binary such as "rb" or
"wb". So, I'm slightly unsure of the following quidance work properly for
text mode...

You can use fseek/ftell to return the size of the file in bytes:

long size;

fseek(file 0L, SEEK_END);
size=ftell(file );
fseek(file 0L, SEEK_SET);

Some C functions to read data from a file tell you how many bytes were read
(i.e., fread, fscanf). If you continually add up the bytes returned by such
a function, the total bytes read in will be equal to the file size upon
reading in the last line. This will occur in your while loop prior to the
check for eof.

Also, those same functions will usually return a specific value when there
is no more input. Depending on which function you choose, the value could
be EOF, NULL or zero. I would suggest reworking the loop into a do{}while
and then using an if()break at the top of the loop to terminate the loop.
The while loop condition could be used to check that your entire read/write
buffer was filled, and the if condition would check for the eof value. If
you don't need to check if your buffer is full, you could still use the
while but change the feof check to instead check for the reading function's
returned value for EOF, NULL, or zero.

Rod Pemberton
Sep 30 '06 #4
"gabs" <ca********@yah oo.comwrites:
magix wrote:
>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 of entry in EOF while loop ?

while (! file.eof() )
{
....
}

i don't know exactly what you mean by filtered. but it sounds like you
want to know which line is the first and which is the last? if so,
whats wrong with using a loop to count number of '\n'. when n = last
line then you do whatever you need to do on that line.
And how are you supposed to know when "n = last line"? You don't know
how many lines are in the file until you've gone past the last line
and hit end-of-file.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 2 '06 #5
Keith Thompson wrote:
"gabs" <ca********@yah oo.comwrites:
>magix wrote:
>>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 of entry in EOF while loop ?

while (! file.eof() )
{
....
}
i don't know exactly what you mean by filtered. but it sounds like you
want to know which line is the first and which is the last? if so,
whats wrong with using a loop to count number of '\n'. when n = last
line then you do whatever you need to do on that line.

And how are you supposed to know when "n = last line"? You don't know
how many lines are in the file until you've gone past the last line
and hit end-of-file.
Maybe like this..

Let's make a couple of assumptions just to keep it simple. We have a
text file of lines, none of which are longer than say 80 characters. We
will read the file and report the number of lines and the offset to the
last line. We will use fgets() to read lines, ftell() to keep track of
where we are and fseek() to go where we want to go.

long first = 0, last;

The first line of a file will always start at offset 0.

char line[100];
int n = 0;

More than enough for an 80 character line, right?

while (fgets(line, sizeof line, fp)) {
last = ftell(fp);
++n;
}

When the loop reaches EOF, last is the offset of the last line. We can
now read and print the first line..

fseek(fp, first, SEEK_SET);
fgets(line, sizeof line, fp);
fputs(line, stdout);

And then..

fseek(fp, last, SEEK_SET);
fgets(line, sizeof line, fp);
fputs(line, stdout);

The above is not really a program. But you can make it one if you want
to. You might also use the n variable to display the line count.

I haven't tested this. Will that bite me?

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Oct 3 '06 #6
Joe Wright <jo********@com cast.netwrites:
Keith Thompson wrote:
>"gabs" <ca********@yah oo.comwrites:
>>magix wrote:
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
of entry in EOF while loop ?

while (! file.eof() )
{
....
}
i don't know exactly what you mean by filtered. but it sounds like you
want to know which line is the first and which is the last? if so,
whats wrong with using a loop to count number of '\n'. when n = last
line then you do whatever you need to do on that line.

And how are you supposed to know when "n = last line"? You don't know
how many lines are in the file until you've gone past the last line
and hit end-of-file.
Maybe like this..
[solution using ftell() and fseek() snipped]
The above is not really a program. But you can make it one if you want
to. You might also use the n variable to display the line count.

I haven't tested this. Will that bite me?
I don't see any problem with it. Of course, it won't work on a
non-seekable input stream, and if you can assume that the input lines
are no longer than N characters (you assumed 80), there's another
solution that does work with non-seekable files.

My point was that the previous poster's suggestion:

| i don't know exactly what you mean by filtered. but it sounds like you
| want to know which line is the first and which is the last? if so,
| whats wrong with using a loop to count number of '\n'. when n = last
| line then you do whatever you need to do on that line.

is at best incomplete.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 3 '06 #7

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

Similar topics

1
2187
by: Nick | last post by:
I wrote a news script to post news posts in reverse chronological order. I'd like a page to post the last 6 posts from a page, from newest to oldest. But as it is, I don't know how to find the number of the last row in the table. So I have to count down from 100, skipping blank entries along the way.
2
4334
by: OvErboRed | last post by:
Hi, I'm trying to determine whether a given URL exists. I'm new to Python but I think that urllib is the tool for the job. However, if I give it a non-existent file, it simply returns the 404 page. Aside from grepping this for '404', is there a better way to do this? (Preferrably, there is a solution that can be applied to both HTTP and FTP.)...
5
2201
by: bart plessers | last post by:
Hello, Currently I am developping a internet "directory browser" My page 'default.asp' has following entries: CurrentPATH = Request("MyLink") Set oFSO = CreateObject("Scripting.FileSystemObject") Set oFolder = oFSO.GetFolder(CurrentPATH) Set oFolderContents = oFolder.Files
12
2382
by: levent | last post by:
What is an elegant way (using std::stream's) to extract number of white-space separated entries in a given line of a formatted text file? e.g.: Take this section of a file, 1 2.78 4 5 -0.003 <tab> 7.1d5 9 40 <tab> 2.e5 -10 ....
2
3812
by: Eric Lilja | last post by:
Hello, I'm writing a simple program that upon start-up needs to open a text file and read a value on the last line. Then all other accesses to the file will be writes (at the end of it). I'm having two problems with this...I tried opening the file for both writing and reading with: std::fstream file("budget.txt", std::ios_base::in |...
40
2945
by: Jeff | last post by:
I have a system on a network and want to determine if anyone is currently connected to the back-end files. An interesting twist is that I have noticed that some users can be connected (have the front end open at the first form) and even though this links to the back-end files, there are no ldb files created. This is so I know when it is...
8
7510
by: Frost | last post by:
Hi All, I am a newbie i have written a c program on unix for line by line comparison for two files now could some one help on how i could do word by word comparison in case both lines have the same words but in jumbled order they should match and print only the dissimilar lines.The program also checks for multiple entries of the same line....
19
2632
by: Chad | last post by:
Okay, let's say I have an exotic os that limits how much goes to stdout. When I go like.. #include <stdio.h> int main(void) { int i=0; for(i=0; i< 10; i++) { printf("a \n");
0
1557
by: Henning | last post by:
This time I agree with Bill replying to a .Net related Q, even if it hurts :)) No need to kick on Bill everytime he is posting in this group. Fair is fair. /Henning "Kevin Provance" <kevin@remove_tpasoft_remove.comskrev i meddelandet news:%23qLKOL4SJHA.5568@TK2MSFTNGP05.phx.gbl...
0
7896
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7827
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8184
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7936
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8195
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6581
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3845
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2334
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1158
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.