【问题标题】:c++ linux system commandc++ linux系统命令
【发布时间】:2011-09-24 07:33:08
【问题描述】:

我有以下问题:

我在我的程序中使用了这个函数:

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

效果很好。

但我不想保持不变的价值。我这样做:

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

我检查我的字符串:

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

是的,没错。 但是system 在这种情况下不起作用并且不返回-1。我只是打印字符串!

如何传输像整数这样的变量,这些变量将像整数一样打印在文件中,但不要字符串?

所以我想要变量 int a 并且我想在文件中使用系统函数打印 a 的值。我的 file.txt 的真实路径是 /proc/acpi/video/NVID/LCD/brightness。我不能用 fprintf 写。我不知道为什么。

【问题讨论】:

  • 尝试编写多语言源文件时会发现很多问题。我建议你只坚持 C 或 C++ 中的一种。

标签: c++ c linux function system


【解决方案1】:

你不能像你试图做的那样连接字符串。试试这个:

curr_val=60;
char command[256];
snprintf(command, 256, "echo -n %d > /file.txt", curr_val);
system(command);

【讨论】:

  • 这值得 +1 只是因为使用 snprintf 而不是 sprintf
【解决方案2】:

system 函数接受一个字符串。在您的情况下,它使用文本 *curr_val_str* 而不是该变量的内容。与其使用sprintf 来生成数字,不如使用它来生成您需要的整个系统命令,即

sprintf(command, "echo -n %d > /file.txt", curr_val);

首先确保命令足够大。

【讨论】:

    【解决方案3】:

    在您的情况下实际(错误)执行的命令是:

     "echo -n curr_val_str  > /file.txt"
    

    相反,您应该这样做:

    char full_command[256];
    sprintf(full_command,"echo -n  %d  > /file.txt",curr_val);
    system(full_command);
    

    【讨论】:

      【解决方案4】:
      #define MAX_CALL_SIZE 256
      char system_call[MAX_CALL_SIZE];
      snprintf( system_call, MAX_CALL_SIZE, "echo -n %d > /file.txt", curr_val );
      system( system_call );
      

      man snprintf

      【讨论】:

        【解决方案5】:

        正确的方法应该是这样的:

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

        【讨论】:

          【解决方案6】:

          只是不要。 :)

          这么简单的操作为什么要求助system()

          #include <sys/types.h>
          #include <sys/stat.h>
          #include <fcntl.h>
          #include <string.h>
          
          int write_n(int n, char * fname) {
          
              char n_str[16];
              sprintf(n_str, "%d", n);
          
              int fd;
              fd = open(fname, O_RDWR | O_CREAT);
          
              if (-1 == fd)
                  return -1; //perror(), etc etc
          
              write(fd, n_str, strlen(n_str)); // pls check return value and do err checking
              close(fd);
          
          }
          

          【讨论】:

            【解决方案7】:

            您是否考虑过使用 C++ 的 iostreams 工具而不是使用 echo?例如(未编译):

            std::ostream str("/file.txt");
            str << curr_val << std::flush;
            

            或者,您传递给system 的命令必须完全格式化。像这样的:

            curr_val=60;
            std::ostringstream curr_val_str;
            curr_val_str << "echo -n " << curr_val << " /file.txt";
            system(curr_val_str.str().c_str());
            

            【讨论】:

              【解决方案8】:

              使用snprintf 来避免安全问题。

              【讨论】:

                【解决方案9】:

                如何使用std::string & std::to_string...

                std::string cmd("echo -n " + std::to_string(curr_val) + " > /file.txt");
                std::system(cmd.data());
                

                【讨论】:

                  猜你喜欢
                  • 2013-02-22
                  • 2013-05-02
                  • 2011-06-05
                  • 1970-01-01
                  • 2011-07-30
                  • 2014-10-06
                  • 1970-01-01
                  • 2018-05-02
                  相关资源
                  最近更新 更多