By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,188 Members | 986 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,188 IT Pros & Developers. It's quick & easy.

File manipulation

P: n/a
I was wondering what the best method of updating a file was.

heres my prob, ive got a file with say 1million urls and I want to
take the first 10,000 and store them in memory. I want to 10k of urls
to be taken out an the file to be updated. is there anyway i could
move the beggining of the file?? What would b the most efficient way
of updating the file??

thanks for any help in advanced.
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Chad wrote:
I was wondering what the best method of updating a file was.

heres my prob, ive got a file with say 1million urls and I want to
take the first 10,000 and store them in memory. I want to 10k of urls
to be taken out an the file to be updated. is there anyway i could
move the beggining of the file?? What would b the most efficient way
of updating the file??

thanks for any help in advanced.


You can always append to a file to update it.
If the file consists of fixed sized records then
you can position to a record and write a new value
to the file (assuming the file has random access).

Read about:
seekp
seekg
fseek
tellp
tellg
ftell

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #2

P: n/a
Chad wrote:
I was wondering what the best method of updating a file was.

heres my prob, ive got a file with say 1million urls and I want to
take the first 10,000 and store them in memory. I want to 10k of urls
to be taken out an the file to be updated. is there anyway i could
move the beggining of the file??
No. You have to copy all the following data back to the beginning. Or
you could somehow mark your first 10,000 urls as removed so you can
just ignore them the next time you read.
What would b the most efficient way of updating the file??


Open a new file and copy all the data you want to stay to that new file.
Then rename the new file so it overwrites the old one. The latter is
usually an atomic operation, which means that if something happens (a
crash e.g.) during your update operation, the user will not get corrupt
data.

Jul 22 '05 #3

P: n/a
Chad wrote:

I was wondering what the best method of updating a file was.

heres my prob, ive got a file with say 1million urls and I want to
take the first 10,000 and store them in memory. I want to 10k of urls
to be taken out an the file to be updated. is there anyway i could
move the beggining of the file?? What would b the most efficient way
of updating the file??

There's not way in standard C++ (nor in most implementations) to remove
anything from the start or middle of a file. Some systems provide
extensions that let you truncate files, but that's OT here. Your only
way to do this is to copy the file. Load your lines into memory, update
them, write them out to a new file, deal with the next batch or copy the
remaining portion of the original file to the new one.

A performance improvement would be if you had a way to categorized the
URLs, so you could break things up into smaller files.

Brian Rodenborn
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.