-1

I am working on a small program that is simply supposed to take a text file and copy it. My assignment states that I am supposed to be using a character buffer during this, but I'm not really sure why.

Is it simply because we will not always be able to allocate enough space for a file of arbitrary size? The text file I'm copying is tiny, but for this to work in general, it should have a buffer? If so, is it intended that I simply allocate a certain amount of the file into the buffer, copy it over to the new file, then go back and grab the next bit of the original file, and repeat?

I think this sounds right. If so, how does one determine an appropriate buffer size?

pretzlstyle
  • 2,774
  • 5
  • 23
  • 40
  • *how does one determine an appropriate buffer size?* You can use any size. You can start with [`BUFSIZ`](http://en.cppreference.com/w/c/io). – R Sahu Jan 14 '16 at 04:12
  • @John3136 Seriously? What's the difference between me asking here and me asking my tutor? Either way someone is just giving me information. It's 10:11pm, let me go ring my tutors doorbell and have a slumber party. – pretzlstyle Jan 14 '16 at 04:12
  • Try different sizes, and see which seems to best for your use-case. – Some programmer dude Jan 14 '16 at 04:13
  • @RSahu hm. Is BUFSIZ some kind of "standard" value? I'm not sure what a Macro is. – pretzlstyle Jan 14 '16 at 04:14
  • @jphollowed, follow the link, scroll down to where BUFSIZ is documented. – R Sahu Jan 14 '16 at 04:16
  • Maybe you need to check [The Definitive C Book Guide and List](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) first, to find a good beginners book or tutorial? – Some programmer dude Jan 14 '16 at 04:16
  • @JoachimPileborg How would I determine what is "best"? I would think by the performance time. But I'm copying tiny text files, I'm not sure how to judge the size needed. Regardless, I want this to be a general solution for any file, so it should be a decent size, right? My tiny text file will likely fit in its entirety in the buffer? – pretzlstyle Jan 14 '16 at 04:16

1 Answers1

0

Is it simply because we will not always be able to allocate enough space for a file of arbitrary size? The text file I'm copying is tiny, but for this to work in general, it should have a buffer? If so, is it intended that I simply allocate a certain amount of the file into the buffer, copy it over to the new file, then go back and grab the next bit of the original file, and repeat?

Yes. Small file or large file, you have to have a place to put what you read somewhere. You then have to write what you read to the next file. The buffer acts as storage for what you've read and what you're going to write, so your spot on. How big your buffer should be depends on you. Do you want to do a lot of little reads and writes and save RAM? Or should you eat up all of your RAM for bulk reading/writing? That's your choice.

Blerg
  • 163
  • 9