Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ linux system command

I have the following problem:

I use in my program this function:

  system("echo -n 60  > /file.txt"); 

it works fine.

But I don't want to have constant value. I do so:

   curr_val=60;
   char curr_val_str[4];
   sprintf(curr_val_str,"%d",curr_val);
   system("echo -n  curr_val_str > /file.txt");

I check my string:

   printf("\n%s\n",curr_val_str);

Yes,it is right. but system in this case doesn't work and doesn't return -1. I just print string!

How can I transfer variable like integer that will be printed in file like integer, but don't string?

So I want to have variable int a and I want to print value of a with system function in file. A real path to my file.txt is /proc/acpi/video/NVID/LCD/brightness. I can't write with fprintf. I don't know why.

like image 884
Tebe Avatar asked Jun 28 '11 13:06

Tebe


People also ask

What is system command in C?

System() Function in C/C++It is used to pass the commands that can be executed in the command processor or the terminal of the operating system, and finally returns the command after it has been completed. <stdlib. h> or <cstdlib> should be included to call this function.

What is Linux C command?

cc command is stands for C Compiler, usually an alias command to gcc or clang. As the name suggests, executing the cc command will usually call the gcc on Linux systems. It is used to compile the C language codes and create executables. The number of options available for the cc command is very high.

What does system in C return?

system() returns the exit code of the process you start.

Can you use Linux commands in C?

In the C programming standard library, there is a function named system () which is used to execute Linux as well as DOS commands in the C program.


1 Answers

you cannot concatenate strings like you are trying to do. Try this:

curr_val=60;
char command[256];
snprintf(command, 256, "echo -n %d > /file.txt", curr_val);
system(command);
like image 192
Constantinius Avatar answered Sep 22 '22 23:09

Constantinius