File manipulation

C

Chad

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.
 
D

Default User

Chad said:
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
 
T

Thomas Matthews

Chad said:
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
 
R

Rolf Magnus

Chad said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top