473,803 Members | 3,416 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

easy way to modify textfile

Hello.
If I wish to open a huge textfile and only change the 300th line in the
file, how do I do that?
Jul 23 '05 #1
5 1225
On Wed, 06 Jul 2005 11:49:13 +0400, Gunnar G <de****@comhem. se> wrote:
If I wish to open a huge textfile and only change the 300th line in the
file, how do I do that?


Use a stream editor, such as sed.

--
Maxim Yegorushkin
<fi************ ****@gmail.com>
Jul 23 '05 #2
> Use a stream editor, such as sed.
And if that is not an option?
I solved it by storing all the lines in a vector, then reopen the file in
non-appending mode and write all the lines.

There must be a better way...

Jul 23 '05 #3
On Wed, 06 Jul 2005 12:23:18 +0400, Gunnar G <de****@comhem. se> wrote:
Use a stream editor, such as sed.

And if that is not an option?
I solved it by storing all the lines in a vector, then reopen the file in
non-appending mode and write all the lines.

There must be a better way...


You could read the file by chunks in a buffer rather then reading the
whole in the vector. This way you avoid all the problems related to memory
allocation.

--
Maxim Yegorushkin
<fi************ ****@gmail.com>
Jul 23 '05 #4
Gunnar G wrote:

Hello.
If I wish to open a huge textfile and only change the 300th line in the
file, how do I do that?


open input file
open a new file for output

counter = 0;

while( line can be read from input ) {
increment counter
if counter equals 300
write replacement line to output
else
write original line to output
}

close input file
close output file
delete input file
rename output file to the input file name

done

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 23 '05 #5
Gunnar G wrote:
Use a stream editor, such as sed.

And if that is not an option?
I solved it by storing all the lines in a vector, then reopen the file in
non-appending mode and write all the lines.

There must be a better way...


If the line isn't longer or shorter afterwards you can use random
access.

Jul 23 '05 #6

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

Similar topics

7
5032
by: Hans A | last post by:
I have a textfile "textfile.txt" containing a list of words. There is one word on each line. I want to pick two random lines from this textfile, and I have tried to do something like: //Loading the file into an array: $textarray = file("textfile.txt); //Using array_rand to pick two random words $rand_numbers = array_rand($textarray, 2);
1
2547
by: Larry Rekow | last post by:
I have a report that's created each day as a flat textfile. Because I came from the Access world, I created a macro that imports it with a schema that gives meaningful names to the various columns, and then uses a query to massage some of the data for me (deletes the first blank row and does a couple of calculations) Then I use DTS to import the Access query as a table. the textfile has a column called "File_num", and among several...
1
3135
by: Michael Schindler | last post by:
Ich habe es nun erreicht, ein textfile einzulesen das nun zeile für zeile im datagrid angezeigt wird. Nun wäre es toll wenn ich das textfile zum beispiel nach jedem blank in eine spalte zerstückeln könnte und meine frage hier wie geht sowas? Ich habe nun das ganze mit folgendem code im datagrid, nur zu welchem zeitpunkt zerstückle ich das und meine eigentliche frage wie kann ich das dann in die jeweiligen spalten stellen?
4
1543
by: Kevin Johnson | last post by:
Hi Using C# and a textfile I need to do two things: 1. Read the contents of the textfile and return the number of words contained in it. 2. Output a list of unqiue words (i.e. not duplicated) to a new textfile. These words should appear on a new line. eg. hello
15
1137
by: RBCC | last post by:
How do I modify a record in a textfile in vb? Do I need 2 files? JOhn --- Posted using Wimdows.net NntpNews Component - Posted from .NET's Largest Community Website: http://www.dotnetjunkies.com/newsgroups/
6
6096
by: Dave Slinn | last post by:
I have a VB app hosting the Webbrowser control. I would like to add "something" to the requests that app is submitted to our web application to indicate that its from this webbrowser and not a separate instance of IE. Is this possible, keeping in mind that I cannot add anything to the registry, since a user may still use IE to visit the web app.
1
1811
by: kachokaratz | last post by:
well i have a problem in my project..since the contents of my textfile is too long, i was wondering how to output the textfile in such a way that you can divide the contents to be able to output everything in an organized manner.. my problem is that since my textfile is too long, when i run the program, the output omits the first parts of the textfile and i end up only seeing the latter parts.. how can i solve this in c language?
14
3362
by: blumen | last post by:
Hi all, I'm a newbie in VB.Net Programming.. Hope that some of you can help me to solve this.. I'm working out to read,parse and save textfile into SQL Server. The textfile contains thousands of rows with about 50 coloums every row.. Everythings goes well until I found one textfile with some strange character...seems to be Japanese character(because it's a Japanese company who owns this textfile)
1
2822
by: asedt | last post by:
With my Excel macro and two text files I want to create a new textfile containing the first textfile then text from the sheet and then the second textfile. My problem is that i don't know how to append the second textfile. Sub mysub() Dim TempString As String 'Open the first file Dim fileA As Integer
0
9703
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
10550
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
10317
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
10295
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
10069
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...
0
6844
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
5501
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...
2
3799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2972
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.