ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I wouldn't run the 'cat' program from inside your own program since the only thing 'cat' does in you your example commmands is to copy bytes from one file to another.
just doe the same thing in C itself, using either the low-level open(2), read(2) and write(2) combination, or use the stdio.h file handling functions if you're more confromatable with them.
Here's a bare-bones file copy program I wrote. It shows how you can use open(2), read(2) and write(2) to 'cat' a file from within a C program.
(it not as complicated as it may first look: most of the lines are just comments and error checking/reporting code)
Code:
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define BLOCKSIZE 4096
int main(int argc, char *argv[])
{
int fdsrc, fddst;
int nbytes;
char blockbuffer[BLOCKSIZE];
if (argc != 3) {
fprintf(stderr, "Usage:\n\t%s <src> <dest>\n", argv[0]);
return 1;
}
/* Open source file for reading */
fdsrc = open(argv[1], O_RDONLY);
if (fdsrc < 0) {
fprintf(stderr, "Error opening %s for reading: %s\n", argv[1], strerror(errno));
return 2;
}
/* Open destination file for writing
* O_EXCL makes sure the file doesn't exist yet, so we
* won't be overwriting anything...
* (mode_t)0600 means the newcreated file will have its
* permissions set to 0600 (rw-------).
*/
fddst = open(argv[2], O_WRONLY | O_CREAT | O_EXCL, (mode_t)0600);
if (fddst < 0) {
fprintf(stderr, "Error creating %s for writing: %s\n", argv[2], strerror(errno));
return 2;
}
/* Copy the file block by block in a loop
* Endless loop ( for(;;) ) used here, but we get
* out with 'break' when done.
*/
for (;;) {
nbytes = read(fdsrc, blockbuffer, BLOCKSIZE);
if (nbytes == 0) { /* EOF, we are done. */
break;
}
if (nbytes < 0) { /* Error occurred */
fprintf(stderr, "Error reading file: %s\n", strerror(errno));
return 2;
}
/* Once here we have read a block of bytes succesfully.
* The block may not be complete if it is the last
* block. If that is the case, we will get out of
* the loop next round, since then read() will return 0.
* Note we are using 'nbytes' to specify number of
* bytes to write, so if the block is not completely
* full, we will also be writing the correct number
* of bytes.
*/
if (write(fddst, blockbuffer, nbytes) < 0) {
fprintf(stderr, "Error writing file: %s\n", strerror(errno));
return 2;
}
}
return 0;
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.