I want to open terminal and cat a file (show its content) using c;
i got error from code below:
char * com = "xterm cat /home/user/Desktop/file.c";
system(com);
or even:
char * com = "/usr/bin/xterm /bin/cat /home/user/Desktop/file.c";
system(com);
thanks for any help;
You could change your command to:
xterm -e less file.c
Related
I am leaning c program . And i have a program shown below.
in testpipe.c
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<fcntl.h>
const char *fifo ="/tmp/test";
int main(){
int fd = open(fifo, o_wronly);
char *text ="hello this is the text";
while(1){
write(fd,text,strlen(text));
}
return 0;
}
i created a pipe in bash using below command
mkfifo /tmp/test
then execute
./testpipe
the program will be running and writing to the pipe /tmp/test
now in another terminal
if i type
tail -f /tmp/test
I don't find any result
But i tried to do
cat /tmp/test
Able to get the result result continuously
What is the difference between these two.
Apart from it if i terminate cat /tmp/test , it kill the ./testpipe why?
I tried to experiment the above case using a bash. But the result was different
First terminal i tried like
mkfifo /tmp/test
while true; do
echo $(date) >/tmp/test
done
Second terminal
When i type
cat /tmp/test
I get output not continuously.
But when i try
tail -f /tmp/test
Getting continuously the output.
If i try to terminate tail or cat , It is not killing the while loop.
What is the difference between these two approach, what i am missing?
I've been following the tutorial on Linux Kernel programming over here: http://www.tldp.org/LDP/lkmpg/2.6/html/index.html
I've gotten to the section that is dedicated to "character device drivers" and while I've gotten it to compile, it will not function on the described case:
"Called when a process writes to dev file: echo "hi" > /dev/chardev"
I've tried several Linux console commands such as:
echo "hi" > sudo /dev/chardev/
and
sudo sh -c 'printf "hi" > sudo /dev/chardev/'
I'm running my code on a Raspberry Pi 3 B+
When I run the first command I will get nothing in return, and nothing is added to /var/logs/messages
When I run the second command I get:
sh:printf: I/O error
Full code over at: http://www.tldp.org/LDP/lkmpg/2.6/html/x569.html
I've modified the code with my snippet below.
/*
* Called when a process writes to dev file: echo "hi" > /dev/chardev
*/
static ssize_t
device_write(struct file *filp, const char *buff, size_t len, loff_t * off)
{
printk(KERN_INFO "%s\n", buff);
return -EINVAL;
}
What I'm expecting to happen is when I use echo "hi" > sudo /dev/chardev that in my /var/logs/messages a line will appear that simply says "hi".
echo "hi" > /dev/chardev
This is ok.
echo "hi" > sudo /dev/chardev/
This is invalid. This will echo hi /dev/chardev/ and write that to file named sudo. And don't /dev/chardev/, it't not a directory, it's a file, it's /dev/chardev (without the / on the end).
sudo sh -c 'printf "hi" > sudo /dev/chardev/'
Same error as above.
If you want to append to a file using sudo, use tee, as in echo hi | sudo tee /dev/chardev. Or if you have to sudo sh -c 'echo "hi" > /dev/chardev'.
I am trying to implement command line interface command for file transfer and that will call internally
sshpass -p "password" scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -r user#remote-machine:/home/QA.txt /home/faadmin/
Here error handling is not happening properly , when I am running this command using system().if in case route not there for file transfer ,or file not found errors some time they blocking the execution of CLI command.so I have check return values after executing the above Linux command not showing other than 0 and 1.how can I get other return values?
You should use popen() because system() return value tell you if the command has been executed or not. E.G.:
#include <stdio.h>
void main(void)
{
FILE *output = NULL;
char text[2048];
char cmd[256];
sprintf(cmd, "%s", "sshpass -p \"password\" scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -r user#remote-machine:/home/QA.txt /home/faadmin/");
output = popen(cmd, "r");
while(fgets(text, 1024, output) != NULL)
printf("%s", text);
pclose(output);
}
I´m trying to execute the next linux command
cat file_a file_b file_c | wc –l > result.txt
in a C program, but I´m not able to do it properly. I have very low level of C programming, and I would like to see how to make that command works in a C program.
This is the code I developed without success:
void main() {
execlp("/bin/sh", "/bin/sh", "-c", "cat file1 file2 fileN | wc –l > lines.txt", 0);
}
I follow your example.
Its results:
implicit declaration of function 'execlp' is invalid in C99
And some other warning errors.
But I think it would be better if you use the system() C-function, here is what I've done:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
system("cat filea fileb filec | wc -l > result.txt");
return EXIT_SUCCESS;
}
After compilation that works!
If you want to run just a command without needing to read the resulting output, you could use the C-function system(), but if you want to run it getting its results, you should use popen().
system()
popen()
Say I have the following program that simply outputs "Hello World":
//DEMO.c
#include<stdio.h>
int main()
{
printf("HELLO World");
}
Now I want to display it both to the screen and to a file output.txt.So I enter the following command in the command prompt(I use CodeBlocks on Windows XP and have configured it to work on command prompt as well):
demo.exe>>output.txt>>stdout
It doesn't work!!! Please tell me how to do it,ie how to output the same thing that I see on my screen(When i run the program),simultaneously to a text file?
You will need to download a tee command for Windows. tee is a UNIX/Linux command that copies the standard input to standard output and also outputs to a file. Then, you can do this:
demo.exe | tee output.txt
Here is one port of tee for Windows.
#include <stdio.h>
#define my_fprintf(fp,...) do{fprintf(fp, __VA_ARGS__);fprintf(stdout, __VA_ARGS__);}while(0)
int main(int argc, char **argv){
FILE *fp;
fp=fopen("output.txt","w");//or filename from argv[1]
my_fprintf(fp, "hello world by %s\n", argv[0]);
fclose(fp);
return 0;
}