473,326 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

C editing a file

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 2816
kreagan
153 100+
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
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 100+
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
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
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 Expert 256MB
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
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 Expert 256MB
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
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
RRick
463 Expert 256MB
That's really up to you and your application. If your changes are known and ordered, then you can just parse from one file to another.

If you have to go backwards and forwards in the file to find the various lines, then storing the file in an array of lines works best.

My view of this program is based on 3 stages. Read file to memory; change stuff internally; and then write it out. Since these chunks are separate and don't affect each other, they are easier to code. The choice is yours.
Sep 19 '07 #11

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

Similar topics

1
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"...
8
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"...
12
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...
0
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...
12
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...
8
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...
2
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 ...
0
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...
5
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
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.