[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 552
  • Last Modified:

please help:using system call (cat) with fread and fwrite

hi,
is there any difference using cat vs fread and fwrite. fread and fwrite require a buffer. Doesn't the cat use a buffer as well. which is better . thanks
0
zizi21
Asked:
zizi21
  • 7
  • 5
  • 3
  • +1
2 Solutions
 
99starCommented:

cat is a ready made command in linux/unix where as fread and fwrite are functions in C

cat is command to display a file where as fread and fwrite can be used in program to read and display a file

both are very different
0
 
zizi21Author Commented:
But I can use the system call to use cat in the C programming.
0
 
evilrixSenior Software Engineer (Avast)Commented:
Yes you can but it is not a robust way to do this. First of all it relies on cat actually being installed (unlikely not to be but not 100% certainty). Also, you have far less control over how it works. For example, if it fails you will have no idea why and failure may not even be detectable depending on how it fails.

The effort in doing this using fread and fwrite is minimal so why not just do it (for want of a better expression) properly?

>> cat is command to display a file
Actually, strictly speaking cat just concatenates files and/or standard input to standard output by default but that output can be re-directed to any file or device.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
zizi21Author Commented:

Does this mean that if i need to concat few files, the best way is to use fread, fwrite ? Is there any other way to concat the files. Thanks a lot.
0
 
eagerCommented:
You can concatenate files with cat:

  $  cat file1.txt file2.txt file3.txt > allfiles.txt

Unless there are unusual requirements, I would not waste time writing and debugging a program to concatenate files.  
0
 
zizi21Author Commented:
I am coding in C. So, fread and fwrite is necessary. But, I read that one could call the cat from the C programmed. I assumed the "cat" is more efficient that the normal fread and fwrite ? Is this right ?
0
 
zizi21Author Commented:
"The effort in doing this using fread and fwrite is minimal so why not just do it (for want of a better expression) properly?"
Yes, but to use fread and fwrite, I need to use a buffer which uses some RAM. Does the "cat" use the RAM ? I am assuming the cat is more efficient because I don't see any memory allocation for the buffer. Thanks.
0
 
evilrixSenior Software Engineer (Avast)Commented:
Well cat will use ram but as you are running it in a separate process using "system" you won't see it allocated in your process. The size of the buffer doesn't need to be big. You just keep reading and writing the buffer until the file is copied. Once you're done the buffer can be destroyed. The code to do this really is no more complicated than this...


// assuming you've opened a file and fptr is your file stream
char buf[1024];
while(fread(buf, 1, 1024, fptr) && fwrite(buf, 1, 1024, fptr));
// check fptr to make sure the loop ended due to eof not error

Open in new window

0
 
eagerCommented:
Cat does more or less the same as the short program that evilrix suggested, with the addition of processing command line and handling multiple input files.  Any program which copies files will need to create a buffer in ram.  Is there a reason you are concerned?

BTW, if performance is an issue, larger buffers are better.  Use malloc to allocate it the buffer.
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> larger buffers are better.  Use malloc to allocate it the buffer.
Actually, that's not strictly true. Any buffer larger than the IO buffer will probably be a waste.
0
 
evilrixSenior Software Engineer (Avast)Commented:
^^^you are also better off keeping your buffer rounded to block sizes for optimum through-put if performance is really that much of an issue.
0
 
eagerCommented:
evilrix -- Each read results in a system call, which has considerable overhead.  Larger buffers mean fewer syscalls, and better performance.  
0
 
zizi21Author Commented:
thanks a lot. sorry, for taking time.
0
 
zizi21Author Commented:
sorry taking time as something happen and i couldn't get back to it. but, now, i can. thanks a lot.
0
 
zizi21Author Commented:
also, thanks a lot for the explaination of cat.
evilrix, i didn't divide the points between your answers but all of them help me. so, i gave the first answer the points but in reality every single answer helped me.
0
 
evilrixSenior Software Engineer (Avast)Commented:
No worries, ziz21. Thanks for the update. As always, do post back here if you have any further questions about this.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 7
  • 5
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now