473,748 Members | 2,214 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Counting the lines of text, but upwards...

Hello,

there is another problem i am facing. i have a text file which is
about 15000 lines big. i have to cut the last 27 lines from that file
and create a new text file that contans those 27 lines. and after that
save both of those files... since that is a big block of text (15000
lines) i thint that it is a big job to look for a keyword... so my
question is this exactly:

how do i do this:

- open a existing text file
- go to the end of that file
- count the last 27 lines of text
- cut that 27 lines and paste it into a new text file (so CUT, not
copy from the original file)
- save both files

counting from the beginning is not an option because he original file
is always different... the number 15000 is an approximation.. .

Thanks in advance...

Mar 1 '07 #1
7 2115
pe*****@gmail.c om wrote:
Hello,

there is another problem i am facing. i have a text file which is
about 15000 lines big. i have to cut the last 27 lines from that file
and create a new text file that contans those 27 lines. and after that
save both of those files... since that is a big block of text (15000
lines) i thint that it is a big job to look for a keyword... so my
question is this exactly:

how do i do this:

- open a existing text file
- go to the end of that file
- count the last 27 lines of text
- cut that 27 lines and paste it into a new text file (so CUT, not
copy from the original file)
- save both files

counting from the beginning is not an option because he original file
is always different... the number 15000 is an approximation.. .

Thanks in advance...
You can't jump around in a text stream, so you would have to open it as
a binary stream. You can read a block of data into a buffer, but you
have to locate the line breaks yourself so that you can decode the bytes
between them into strings.

--
Göran Andersson
_____
http://www.guffa.com
Mar 1 '07 #2
Or just read in the whole file, take off the last 27 lines, then write it
back out. Here's an untested version.

Dim lines() as String = File.ReadAllTex t("c:\data.txt" ).Split(crlfs,
StringSplitOpti ons.None)
Dim numOfLines = lines.Length

Then use WriteAllText to output the data.

Dim newText as String = String.Join(Con trolChars.CrLf, lines, 0,
lines.length - 27)
File.WriteAllTe xt("c:\output.t xt",newText,Fal se)

Robin S.
Ts'i mahnu uterna ot twan ot geifur hingts uto.
---------------------------------------------------------
"Göran Andersson" <gu***@guffa.co mwrote in message
news:e1******** ******@TK2MSFTN GP04.phx.gbl...
pe*****@gmail.c om wrote:
>Hello,

there is another problem i am facing. i have a text file which is
about 15000 lines big. i have to cut the last 27 lines from that file
and create a new text file that contans those 27 lines. and after that
save both of those files... since that is a big block of text (15000
lines) i thint that it is a big job to look for a keyword... so my
question is this exactly:

how do i do this:

- open a existing text file
- go to the end of that file
- count the last 27 lines of text
- cut that 27 lines and paste it into a new text file (so CUT, not
copy from the original file)
- save both files

counting from the beginning is not an option because he original file
is always different... the number 15000 is an approximation.. .

Thanks in advance...

You can't jump around in a text stream, so you would have to open it as a
binary stream. You can read a block of data into a buffer, but you have
to locate the line breaks yourself so that you can decode the bytes
between them into strings.

--
Göran Andersson
_____
http://www.guffa.com

Mar 1 '07 #3
Read each line of the file into a FIFO queue that is 27 elements deep.
When you get to the end iterate through he stack and write each line out to
the new file.

<pe*****@gmail. comwrote in message
news:11******** **************@ h3g2000cwc.goog legroups.com...
Hello,

there is another problem i am facing. i have a text file which is
about 15000 lines big. i have to cut the last 27 lines from that file
and create a new text file that contans those 27 lines. and after that
save both of those files... since that is a big block of text (15000
lines) i thint that it is a big job to look for a keyword... so my
question is this exactly:

how do i do this:

- open a existing text file
- go to the end of that file
- count the last 27 lines of text
- cut that 27 lines and paste it into a new text file (so CUT, not
copy from the original file)
- save both files

counting from the beginning is not an option because he original file
is always different... the number 15000 is an approximation.. .

Thanks in advance...

Mar 2 '07 #4
If you do that, don't you just get the 27 lines he wanted removed from the
original file?
Robin S.
--------------------------------
"Ray Cassick" <rc************ *@enterprocity. comwrote in message
news:u9******** ******@TK2MSFTN GP02.phx.gbl...
Read each line of the file into a FIFO queue that is 27 elements deep.
When you get to the end iterate through he stack and write each line out
to the new file.

<pe*****@gmail. comwrote in message
news:11******** **************@ h3g2000cwc.goog legroups.com...
>Hello,

there is another problem i am facing. i have a text file which is
about 15000 lines big. i have to cut the last 27 lines from that file
and create a new text file that contans those 27 lines. and after that
save both of those files... since that is a big block of text (15000
lines) i thint that it is a big job to look for a keyword... so my
question is this exactly:

how do i do this:

- open a existing text file
- go to the end of that file
- count the last 27 lines of text
- cut that 27 lines and paste it into a new text file (so CUT, not
copy from the original file)
- save both files

counting from the beginning is not an option because he original file
is always different... the number 15000 is an approximation.. .

Thanks in advance...


Mar 2 '07 #5
If the original author is willing to do a little labor I'd suggest starting
by reading this thread:

http://groups.google.com/group/micro...7beee5cee8da15

At the end of it I supplied a class named Copier to produce the results
another guy wanted (never heard from him.) But clearly it is a simple
matter to use this class as a starting point adding a method (named Splitter
perhaps) which takes three parameters. Two strings representing the two
parts of the file and a numeric representing the number of lines desired in
the first file.

The Copy method I supplied will copy a fixed number of lines to a file so
the only required adjustment is to open a second file (the parameter
contains the name) and to copy the remaining lines to it. At the end you
have the original file intact along with two new files split at the line
number specified. If it is necessary to specify the number of lines to be
placed in the second file (rather than the first) then simply add another
method that returns the number of lines in the original file and do the math
to calculate how many lines should be written to the first file.

And it appears that I failed to call Dispose() on the FileStreams in that
code so one might want to add that not that it wouldn't work without it.
"RobinS" <Ro****@NoSpam. yah.nonewrote in message
news:PY******** *************** *******@comcast .com...
If you do that, don't you just get the 27 lines he wanted removed from the
original file?
Robin S.
--------------------------------
"Ray Cassick" <rc************ *@enterprocity. comwrote in message
news:u9******** ******@TK2MSFTN GP02.phx.gbl...
>Read each line of the file into a FIFO queue that is 27 elements deep.
When you get to the end iterate through he stack and write each line out
to the new file.

<pe*****@gmail .comwrote in message
news:11******* *************** @h3g2000cwc.goo glegroups.com.. .
>>Hello,

there is another problem i am facing. i have a text file which is
about 15000 lines big. i have to cut the last 27 lines from that file
and create a new text file that contans those 27 lines. and after that
save both of those files... since that is a big block of text (15000
lines) i thint that it is a big job to look for a keyword... so my
question is this exactly:

how do i do this:

- open a existing text file
- go to the end of that file
- count the last 27 lines of text
- cut that 27 lines and paste it into a new text file (so CUT, not
copy from the original file)
- save both files

counting from the beginning is not an option because he original file
is always different... the number 15000 is an approximation.. .

Thanks in advance...



Mar 2 '07 #6
Yeah, I did not read that he also wants to save the lines that are not the
last 27 to another file. In this case I would write a custom FIFO stack that
writes the lines that get pushed off the bottom to a file (so you get the
ones - the 27 that remain in the stack) and then write the ones left in the
stack to another file :)
"RobinS" <Ro****@NoSpam. yah.nonewrote in message
news:PY******** *************** *******@comcast .com...
If you do that, don't you just get the 27 lines he wanted removed from the
original file?
Robin S.
--------------------------------
"Ray Cassick" <rc************ *@enterprocity. comwrote in message
news:u9******** ******@TK2MSFTN GP02.phx.gbl...
>Read each line of the file into a FIFO queue that is 27 elements deep.
When you get to the end iterate through he stack and write each line out
to the new file.

<pe*****@gmail .comwrote in message
news:11******* *************** @h3g2000cwc.goo glegroups.com.. .
>>Hello,

there is another problem i am facing. i have a text file which is
about 15000 lines big. i have to cut the last 27 lines from that file
and create a new text file that contans those 27 lines. and after that
save both of those files... since that is a big block of text (15000
lines) i thint that it is a big job to look for a keyword... so my
question is this exactly:

how do i do this:

- open a existing text file
- go to the end of that file
- count the last 27 lines of text
- cut that 27 lines and paste it into a new text file (so CUT, not
copy from the original file)
- save both files

counting from the beginning is not an option because he original file
is always different... the number 15000 is an approximation.. .

Thanks in advance...



Mar 3 '07 #7
pe*****@gmail.c om wrote:
Hello,

there is another problem i am facing. i have a text file which is
about 15000 lines big. i have to cut the last 27 lines from that file
and create a new text file that contans those 27 lines. and after that
save both of those files... since that is a big block of text (15000
lines) i thint that it is a big job to look for a keyword... so my
question is this exactly:

how do i do this:
I provided a reply in March 2006 to another person with a similar
request, and while I didn't intend to write your entire solution, I have
done just that below (watch for line-wrapping) -

Dim blPossibleEOL As Boolean = False
Dim byTemp As Byte = 0
Dim iFileNum As UInt16 = FreeFile()
Dim sInputFileName As String = "YOUR INPUT FILENAME HERE!!"
Dim sTextLines As String = ""
Dim iCounter As UInt16 = 0

Dim iFileEnd As Integer = FileLen(sInputF ileName) - 2
Dim iFileSeekPositi on As Integer = iFileEnd
FileOpen(iFileN um, sInputFileName, OpenMode.Binary , OpenAccess.Read )

Do
FileGet(iFileNu m, byTemp, iFileSeekPositi on)
If blPossibleEOL Then
If byTemp = &HD Then
iCounter += 1
If iCounter = 27 Then 'Change this for whatever number of lines
you are wanting.
Exit Do
Else
blPossibleEOL = False
End If
Else
blPossibleEOL = False
End If
ElseIf byTemp = &HA Then
blPossibleEOL = True
Else
blPossibleEOL = False
End If
iFileSeekPositi on -= 1
Loop

sTextLines = StrDup(iFileEnd - (iFileSeekPosit ion + 1), " ")
FileGet(iFileNu m, sTextLines, iFileSeekPositi on + 2)
FileClose(iFile Num)

My.Computer.Fil eSystem.WriteAl lText("CUT-LINES OUTPUT FILENAME HERE!!",
sTextLines, False)

Dim srInput As New IO.StreamReader (sInputFileName )
Dim srOutput As New IO.StreamWriter ("TRIMMED-LINES OUTPUT FILENAME HERE!!")

iFileEnd = iFileSeekPositi on
iFileSeekPositi on = 0
Do
sTextLines = srInput.ReadLin e
srOutput.WriteL ine(sTextLines)
iFileSeekPositi on += (Len(sTextLines ) + 2)
Loop Until iFileSeekPositi on >= iFileEnd

srInput.Close()
srOutput.Close( )

'Kill(sInputFil eName)
I have tested this with a 200,000 line Text File and the files were
created almost instantly!

Basically, it works like this -

The first loop opens your Input File (Binary Read Mode), and starting
from the end of the file, counts the number of lines you are wanting to
cut. (iCounter = 27 in this case). It does this by counting the CrLf
characters in your text file. This approach has a tremendous speed
advantage as it doesn't have to count ALL of your Input File lines from
the beginning.

Once it has reverse-counted the number of lines you are wanting, it
notes the current file (byte) position, reads the lines from that
position until the end of the file into a string and writes-out those
lines to your new file. You now have a file with just the number of
lines that you required to be cut.

It then opens your Input File in a StreamReader and starts reading line
by line in the second loop. As each line is read, it is written to
another file, which will eventually contain everything except the last
few lines you want cut. It knows when to stop writing by matching the
number of characters read so far (allowing extra 2 bytes for CrLf on
each line) to the original Seek Position obtained earlier.

As already mentioned, this routine is VERY fast, not necessarily pretty,
but fast!

I would encourage you to add error-handling etc., but I trust this gives
you a strong foundation to work from.
ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
Mar 3 '07 #8

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

Similar topics

11
7313
by: Derek Basch | last post by:
Is there a better way to count iterations that this?: pets = 0 for i in pets: pets += 1 print "pet" + "#" + pets Thanks, Derek Basch
5
2847
by: Anders K. Jacobsen [DK] | last post by:
Hi We have a rather large asp.net project with serveral utility projects (written in C#). Is there at tool out there which can give an estimate of the total amount of code lines all projects results in? thanks in regads Anders
1
6927
by: j | last post by:
Hi, I've been trying to do line/character counts on documents that are being uploaded. As well as the "counting" I also have to remove certain sections from the file. So, firstly I was working with uploaded MS WORD .doc files. Using code like that below: strLine = sr.ReadLine While Not IsNothing(strLine) 'Not eof If Trim(strLine) <> "" Then 'Not blank
3
1403
by: Brent | last post by:
Say that I have a text box that holds 5 lines of text. If the user enters ten text lines, I want the first four lines of text displayed, followed by a "More" hyperlink. The link will pop up another page to display the rest of the text. The problem is that I don't know how to count lines. I can count characters, but this solution fails spectacularly when it encounters the characters "<br/>" (which should be counted as a full line, of...
5
7704
by: andy.lee23 | last post by:
hi im having trouble counting lines in a text file, i have the following code int node1, node2, i; char name; float value; ifstream fin; fin.open(OpenDialog1->FileName.c_str()); i=1;
4
2664
by: anoehre | last post by:
Hi! I have a multi-line text box in a compact framework c# application. I need a method to calculate the excact number of lines of the textbox and a method to calculate the number of used lines after the text box was filled with a string. Any ideas? Greetings,
10
1514
by: cj | last post by:
I'm writing a TCP/IP server app that will have many simultaneous connections. The main thread listens for new connections and starts a thread to handle each requested connection. These are short lived threads that get a request and return a reply and end. Can the main thread tell me how many connection threads are currently running at any given time? I'd like to have a label on the main form to show how many connections the server is...
7
13505
by: Mark..... | last post by:
Hi, Can someone tell me the easiest way to count the number of lines in a text file? I can write a loop to do this but it seems cumbersome.... there must be an easier way?? Thanks in advance
6
16859
by: =?Utf-8?B?SGFucyAtIERpYUdyYXBoSVQgLQ==?= | last post by:
Hi! sorry for a dumb question. I have a textbox where multiline=true and I want to set the scrolbar from none to vertical when the textbox have more then f.ex. 5 lines, or reverse. How can I count the amount of lines in a textbox? Best regards
0
8991
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9552
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9326
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8245
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6076
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4877
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2215
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.