LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-21-2008, 09:36 AM   #1
HarryBoy
Member
 
Registered: Apr 2008
Distribution: MontaVista Linux Version 4.0.1, Professional Edition
Posts: 215

Rep: Reputation: 16
how can I run the 'cat' command from my program?


Hi,

I have a platform which has several framebuffers like so:
fb0 = graphics
fb1 = video
fb2 = transparency

I need to change the transparency every now and again and I do this manually by:
cat FileContainingAlphavalues > /dev/fb/2

My question is how can I do this in my program?

i.e. how can I run the command:
cat FileContainingAlphavalues > /dev/fb/2
from within my c program?

Thanks
 
Old 10-21-2008, 09:48 AM   #2
CRC123
Member
 
Registered: Aug 2008
Distribution: opensuse, RHEL
Posts: 374
Blog Entries: 1

Rep: Reputation: 32
Use the system(const char *command) function.

Use this command for more info:
Code:
man 3 system
 
Old 10-21-2008, 10:02 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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.
 
Old 10-21-2008, 10:06 AM   #4
HarryBoy
Member
 
Registered: Apr 2008
Distribution: MontaVista Linux Version 4.0.1, Professional Edition
Posts: 215

Original Poster
Rep: Reputation: 16
OK thanks guys. I'll try the open(2), read(2) and write(2) combination, or use the stdio.h file handling functions and see how I get on.
 
Old 10-21-2008, 02:47 PM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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;
}

Last edited by Hko; 10-21-2008 at 02:58 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How To Run Program From Command Line? neuropulse Linux - Newbie 11 05-28-2020 09:39 AM
how to run cat command in silent mode user_linux Linux - Newbie 4 06-16-2005 10:21 AM
How to run a command through a Java program kernelvn Programming 3 12-02-2004 09:25 PM
command to run a program stupidloser Linux - Newbie 1 08-11-2004 02:55 AM
Viewing program messages when program isn't run from command line? Locura Linux - Software 1 09-27-2003 08:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration