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

Home Posts Topics Members FAQ

To delete few lines and add few lines at the end of a text file using c program

Hello Everyone,
I am breaking my head with this problem. Please help
me out. Let me first explain my problem :

Here it is:

I am working in realtime environment where i will be creating
logfiles internally while my system is running.
I am having a logfile.txt in which i have to store maximum of 150
lines.Suppose for every 60 seconds 50 lines are going to add to my
logfile If it exceeds more than 150 my program should remove first 50
lines and add coming 50 lines at the end.

In brief to say how to delete few lines at the top of a text file and
append it 50 lines at bottom.
It will be very helpful for me if you send me any system call to solve
my problem or the code.

Thank you,
Murali

Mar 9 '06 #1
2 3023
Murali wrote:

In brief to say how to delete few lines at the top of a text file and
append it 50 lines at bottom.


You'll have to rewrite the entire file.

The general form is:
Read in old file, modify, write temporary file
Delete old file
Rename temporary file to old file name.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Mar 9 '06 #2
In article <1141912411.265 479.258040
@p10g2000cwp.go oglegroups.com> , mm****@gmail.co m says...
Hello Everyone,
I am breaking my head with this problem. Please help
me out. Let me first explain my problem :

Here it is:

I am working in realtime environment where i will be creating
logfiles internally while my system is running.
I am having a logfile.txt in which i have to store maximum of 150
lines.Suppose for every 60 seconds 50 lines are going to add to my
logfile If it exceeds more than 150 my program should remove first 50
lines and add coming 50 lines at the end.


As stated, this simply isn't possible (at least not
portably).

Your choices are 1) create something more or less along
the lines of a database, or 2) create the file anew each
time it's needed.

Given the size of file, the latter is probably the better
option in this case unless your lines are _really_ long.
With typical log file entries, you're looking at 150x40
(average) => around 6K. That just doesn't justify the
overhead of a database style of format (which wouldn't be
readable as normal text anyway).

If you have some memory available, I'd consider reading
the data in at startup, holding it in a circular buffer
in memory, and writing it out as needed. This way you're
not reading the old file back into memory on a regular
basis, cutting your I/O roughly in half.

If the log file is really crucial, instead of overwriting
the old log file, you're probably better off writing the
data to a new file, then deleting the old file and
renaming the new file to the old name.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Mar 9 '06 #3

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

Similar topics

4
2666
by: deko | last post by:
I'm not sure how to do this in php - need to calculate and delete an unspecified number of lines from the *top* of a file. <?php //append $visitor to the bottom of $visdata $fp = fopen($visdata,"a"); fwrite($fp, "\n".$visitor;) //count the lines in $visdata $lines = count($visdata); if $lines > 10
16
17017
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums must be UPDATED, if not, they must be INSERTED. Logically then, I would like to SELECT * FROM <TABLE> WHERE ....<Values entered here>, and then IF FOUND UPDATE <TABLE> SET .... <Values entered here> ELSE INSERT INTO <TABLE> VALUES <Values...
5
3292
by: Magix | last post by:
Hi, This is about File I/O Operation. I want to delete a record from a file, let say FileA. Any one has any good and efficient example/approach? void DeleteRecord(int LineNum) { // open FileA // open tmpFile
3
3281
by: Arpan | last post by:
A Form has a FileUpload, 2 Buttons & a TextBox web server controls. Using the FileUpload control, I want to give users the provision to move & delete files that DO NOT exist in C:\Inetpub\wwwroot (i.e. the root directory). This is the code: <script runat="server"> Sub MoveFile(ByVal obj As Object, ByVal ea As EventArgs) File.Move(fudFileSource.FileName, txtFileDest.Text) 'File.Move("F:\4.jpg", "C:\4.jpg") End Sub
2
3539
by: cdun2 | last post by:
Hello, I have some code that reads each line of a text file, and if a line is found where the length of the string in the line is 384, it writes the line to a text file. The other step that I need to take is to delete the line from the source file. The code is as follows; -***************************************** Public Sub Main()
24
7558
by: biganthony via AccessMonster.com | last post by:
Hi, I have the following code to select a folder and then delete it. I keep getting a Path/File error on the line that deletes the actual folder. The line before that line deletes the files in the folder but I get the error message when the procedure attempts to delete the folder selected in the BrowseforFolderbyPath function. How can I get it to delete the folder that the user has selected in the Browse for Folder dialog?
4
3002
by: koutoo | last post by:
If I have a text file that is delimited by spaces, how do I import it and get to comma delimited? Here is a row of data from the text file: 1 1 10:55:14 2 65 8.5 1.4+1.1 2.5 Class-2 0 I tried a few examples from the group and it didn't work, since the file also has a header row and a row of seperators ( -------). The lengths of each row is something like 130, so there are extra spaces after...
3
5610
by: Barkingmadscot | last post by:
I am stuck, i can workout how to remove lines from an array I have loading a text file (a Log), I know which lines a need, but the logs can be upto 30K sometimes bigger. I found trying to delete the lines from the log file before loading in the array took ages, i thought i would be alot quicker to put in an array and remove the unwanted lines. Dim text As Array Dim lines As New List(Of String)
6
5866
by: Annalyzer | last post by:
My database must produces a .csv file with a header line that is different from the detail lines. This .csv file contains a monthly report to an outside agency and is uploaded online so it must follow specific guidelines. As usual, I think I have made the processing way more convoluted than necessary. Here is what I have currently: 1) Macro #1 runs query #1 to create (or overwrite) a table with the current month's header information....
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
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...
0
9249
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
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
4607
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4877
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2787
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
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.