473,804 Members | 4,181 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C editing a file

6 New Member
Hi all.
I've to edit a txt file, deleting a given word.

My approach is to open the file and read it char by char, copying that to a second file.
I use a buffer to identify the given word, so if I recognize the pattern I don't pass it to the new file. Finally I copy the new file to the former.

Is there a better way to carry out my task, maybe editing "in loco" (without using a support file)?
Before opening the file I know in which row/rows the word is located.
For example: cat, row 3, row 5 twice, row 13. Is it someway useful to speed up the process?
Sep 17 '07 #1
10 2856
kreagan
153 New Member
Hi all.
I've to edit a txt file, deleting a given word.

My approach is to open the file and read it char by char, copying that to a second file.
I use a buffer to identify the given word, so if I recognize the pattern I don't pass it to the new file. Finally I copy the new file to the former.

Is there a better way to carry out my task, maybe editing "in loco" (without using a support file)?
Before opening the file I know in which row/rows the word is located.
For example: cat, row 3, row 5 twice, row 13. Is it someway useful to speed up the process?
If you have the row/column, you can target the word quickly.

if you know nothing about the word, (assuming you must maintain order or the file), I would search the file. If you are worried about efficiency, maybe you can compair letter by letter instead of whole string compairs.

If there is some order to the file, you can possibly make a cleaver algorithm. For example, if your file is sorted, you can perform different searches to enhance efficiency.
Sep 17 '07 #2
PhilSorum
6 New Member
If you have the row/column, you can target the word quickly.

if you know nothing about the word, (assuming you must maintain order or the file), I would search the file. If you are worried about efficiency, maybe you can compair letter by letter instead of whole string compairs.

If there is some order to the file, you can possibly make a cleaver algorithm. For example, if your file is sorted, you can perform different searches to enhance efficiency.
Hello, thanks for the reply.
My file is not sorted, it's a random txt file, for example a program readme.txt or a forum FAQ, so words can occur in any order.
I know the row/rows in which the word occurs, if any, but I haven't the column.
As in the example above I know:

word: cat, row 3, row 5 twice, row8.
word: dog, not present.

I'm in search for a good efficiency!
Sep 17 '07 #3
kreagan
153 New Member
Hello, thanks for the reply.
My file is not sorted, it's a random txt file, for example a program readme.txt or a forum FAQ, so words can occur in any order.
I know the row/rows in which the word occurs, if any, but I haven't the column.
As in the example above I know:

word: cat, row 3, row 5 twice, row8.
word: dog, not present.

I'm in search for a good efficiency!
I really don't know what you have done or your limitations, so my ideas are unbounded.

instead of parsing the file word for word, I would suggest using strstr function to point to every instance of that string.
strstr
Sep 17 '07 #4
PhilSorum
6 New Member
I really don't know what you have done or your limitations, so my ideas are unbounded.

instead of parsing the file word for word, I would suggest using strstr function to point to every instance of that string.
strstr
I have no limitations, my program can be simplified to this recipe:
take a random txt file
take a random word
a magician tells you how many occurrence of the given word are located in the file and in what rows they occur
you have to edit the file, removing them
is the magician hint useful?
is it possible to carry out the task without using a second file?
Sep 17 '07 #5
StarvingLinguist
2 New Member
the way I can think of to do the job with the fewest resources is to set an fseek write point at the beginning of the discovered word, and write the letters which follow the word into the space the word used to be in. continuing to the end of the file skipping (advancing the read point by one word size) each point where the word is again recognized. If those are disk access commands it might be a little slower than doing the same thing with a large array of all the characters and rewriting the file back to disk.

I hope your programming tasks eventually become more stimulating.
Sep 17 '07 #6
RRick
463 Recognized Expert Contributor
You're going to have to be careful and not delete words that have your search word contained in them. For example, if you need to delete cat in the following sentence, "Did you catch the cat?", you probably don't want "Did you ch the cat?"

Since your search data is centered on lines, then the simplest thing to do is read the complete file into an array of lines. Now you can get to the line you want very easily. Once there, parse by word instead of character and now the comparison is easy.

You'll have to watch out for delimiters because when you read the last word in the sentence, you'll get "cat?", not just cat.
Sep 18 '07 #7
PhilSorum
6 New Member
You're going to have to be careful and not delete words that have your search word contained in them. For example, if you need to delete cat in the following sentence, "Did you catch the cat?", you probably don't want "Did you ch the cat?"

Since your search data is centered on lines, then the simplest thing to do is read the complete file into an array of lines. Now you can get to the line you want very easily. Once there, parse by word instead of character and now the comparison is easy.
Ok, I like this approach.
Once I've built an array of lines with every line edited (removing the word), I imagine I've to copy again all the lines in the source file. If a line contains only the word I've to cancel, I can directly skip that line and go on with next line or some adjustment on the file size/allocation are required?
Sep 18 '07 #8
RRick
463 Recognized Expert Contributor
You are going to have to write out the file again with this approach. You've got two choices, write to a new file or overwrite the original file. I prefer option 1 because you can compare the two files.

Be careful with lines that contain only your search word. Does your app want you to delete the line or make it an empty line. These are two different things. An empty line only has a '\n' but that is still something.
Sep 18 '07 #9
PhilSorum
6 New Member
When I have only the given word in a row, I have to remove the whole line, so decreasing the total number of rows in the file.
I have a question: if I have to use a backup file where copying data to, is it better to create an array of lines from the original file, as just said, or I can simply scan the file and copy in the dest file char by char?
Sep 18 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1689
by: sam.collett | last post by:
Is there a basic guide on Xml document creation and editing (simpler than the MSDN docs). Say I want to create a file containing the following: <?xml version="1.0" encoding="utf-8" standalone="yes"?> <Files> <File> <Text>Test</Text> <Name>Test.html</Name> </File>
8
11362
by: Sam Collett | last post by:
Is there a basic guide on Xml document creation and editing (simpler than the MSDN docs). Say I want to create a file containing the following: <?xml version="1.0" encoding="utf-8" standalone="yes"?> <Files> <File> <Text>Test</Text> <Name>Test.html</Name> </File>
12
3104
by: Ron Weldy | last post by:
I have a test server runinng 2003/IIS 6 with a mixture of asp and asp.net files. On my workstation I have a share set up to the folder where the web files reside. I am just doing quick and dirty asp editing (like I used to be able to do with 2K/IIS5) where I use VS.NET, open an asp file, make changes, save and refresh my browser. Problem is that I get an Access is Denied error when I try to save the file and then the file gets wiped on...
0
312
by: Vanga Sasidhar | last post by:
Hi All, Please help me in the following problem. I am having some files with AVI extension. I want to make two programs in Visual Basic .NET which will work with these AVI Files. One program should work as encrypter and another program should work as decrypter. The frist program, encrypter should lock (the user should not be able to open) any AVI file by editing the file. The second program, decrypter should
12
2834
by: Thomas Bartkus | last post by:
Does anyone use emacs together with both WordStar key bindings and python mode? I'm afraid that Wordstar editing key commands are burned R/O into my knuckles! I would like to play with emacs for Python editing but I'm having (2) problems. 1) When I load a .py file, emacs automatically overrides my wordstar-mode with python-mode, forcing all the keybindings back to emacs native keys. Why?
8
2071
by: D | last post by:
Hi, I currently have a Python app with a Tkinter GUI frontend that I use for system administration. Everytime it launches, it reads a text file which contains info about each host I wish to monitor - each field (such as IP, hostname, etc.) is delimited by !!. Now, I want to be able to edit host information from within the GUI - what would be the best way to go about this? Basically I just need to either edit the original host line, or...
2
2085
by: ritesh | last post by:
Hi, I'm facing a problem in which I need to edit an already created file, and the editing needs to be done at the start of the file rather then appending to the file. OS - Linux,Solaris For e.g. I have a file test.txt created and I have the path to this file in a
0
2019
by: Frnak McKenney | last post by:
Can I use a bound ComboBox for both browsing and editing? I'm working on a small, standalone database application using Visual C#.NET 2003 and an Access data file. In order to keep the number of different screens down to a minimum, I'm trying to use the same Windows Forms for both browsing and for updating. This works fine for TextBoxes, but I'm running into problems with my DropDownLists (ComboBoxes).
5
2500
by: Zytan | last post by:
I cannot stand being unable to change the code while the debugger is running. Is there a way to do this? thanks, Zytan
0
9579
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10575
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
10330
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
10319
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,...
1
7616
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
6851
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
5520
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
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2990
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.