I've learned to write bytes from a file by reading the entire file into a character array and then writing them out to another file using fwrite. Of course, my teacher told me this is both wasteful and a file might not fit into memory, so I should use another method. What I want to do is write the exact copy of a file into my own file. How can I do this the better way? Thanks
RexxX 0 Light Poster
Recommended Answers
Jump to Postuse fgets to read the source file one line at a time. this will take a small character array of some arbitrary length. say 80 or 120 characters. you could do it a lot smaller, but i don't see any reason why you would want to..
use fprintf to …
Jump to PostTry looking at the macro BUFSIZ.
I found this about BUFSIZ
Macro: int BUFSIZ
The value of this macro is an integer constant expression that is good to use for the size argument to setvbuf. This value is guaranteed to be at least 256.The value of BUFSIZ is …
Jump to Postno. not at all.
it doesnt get any more convenient than the example i handed you.
Jump to PostI didn't try it because by the looks of it, the code continually prints out a fixed stream of 80 bytes until the end of the file, so what's to prevent it from printing past the end of a file? Maybe you should stop ignoring what other people say, ie …
Jump to PostJust set up a buffer of x chars. Open the file for binary read.
Loop, reading x chars from the file. Write the characters.
Exit the loop when the read does not actually get x chars -- you've hit EOF.
All 17 Replies
jephthah 1,888 Posting Maven
gerard4143 371 Nearly a Posting Maven
RexxX 0 Light Poster
jephthah 1,888 Posting Maven
RexxX 0 Light Poster
jephthah 1,888 Posting Maven
jephthah 1,888 Posting Maven
RexxX 0 Light Poster
gerard4143 371 Nearly a Posting Maven
jephthah commented: we don't do people's homework. -1
jephthah 1,888 Posting Maven
jephthah 1,888 Posting Maven
RexxX 0 Light Poster
jephthah 1,888 Posting Maven
RexxX 0 Light Poster
jephthah 1,888 Posting Maven
gerard4143 371 Nearly a Posting Maven
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.