c - 使用字符串 C 程序/Popen 时出现段错误

标签 c linux string segmentation-fault malloc

我已经为我的问题寻找了将近两天的答案,并尝试了所有建议的解决方案,但都无济于事。 我正在尝试使用我的 C 程序通过 linux 终端访问文件。 我想运行 popen() 来执行此操作。

我想在 popen() 中运行的命令是:grep -o %s/usr/share/dict/words

其中 %s 是一个可变词,每次迭代都会改变。我尝试过使用指针、数组和替代函数,例如 asprintf()/snprintf()

这是我现在的代码:

char *message = (char *)malloc(500);
strcpy(message, "grep -n");
printf("%s", message);
strcat(message, "hello");
printf("%s", message);
strcat(message, " /usr/share/dict/words"); // SEG FAULT OCCURS HERE
printf("%s", message);

然后我会将其传递给 popen。 我还尝试初始化为:char message[500] 并且这会在同一位置返回相同的错误。

这是我的完整代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "caeserheader.h"

int main( int argc, char *argv[]){

  char *inputfile;
  int n = 0;
  int shiftamount = 0;

  //Determine amount of arguments
  if(argc == 2){
   inputfile = argv[1];
   }
  else if(argc == 3){
    inputfile = argv[1];
    n = atoi(argv[2]);
    shiftamount =  n * (-1) ;
  }
  else{
    printf("Please enter a proper number of arguments.");
    return -1;
  }

  //OPENS INPUT FILE
  FILE *input = fopen(inputfile, "r");

  if(input == NULL){
    printf("\n FILE NOT FOUND.");
    perror("fopen");
    return -1;
  }

  //RESERVES MEMORY AND GRABS STRING
  fseek(input, 0L, SEEK_END);
  long Tsize = ftell(input);
  rewind(input);

  char *inputtext;
  inputtext = calloc( 1, Tsize+1);



//ERROR CHECKING
  if(!inputtext){
    fclose(input), printf("MEMORY FAILED.");
  }
  if(1!=fread( inputtext, Tsize, 1, input)){
    fclose(input), free(inputtext), printf("READ FAIL.");
  }

  //CREATES DECRYPTED STRING

  char newletter;
  char *newstring;
  int i;

  //WITH GIVEN NUMBER OF SHIFTS

  if(argc == 3){
  newstring = malloc(Tsize + 1);

  for(i=0; i<Tsize; i++){
    newletter = shift(inputtext[i], shiftamount);
    newstring[i] = newletter;
  }
  }

  //WITHOUT GIVEN NUMBER OF SHIFTS
  if(argc == 2){

 char *message = (char *)malloc(500); //SEG FAULT SOMEWHERE HERE?
    // strcpy(message, "grep -n");
    //    printf("%s", message);
    //strcat(message, "hello");
    //    printf("%s", message);
    //    strcat(message, "/usr/share/dict/words");
    //printf("%s", message);

    //    word = strtok(inputtext," ,.-!?\n");

    // int i;


       //for(i=0; i<10; i++){
        //word = strtok(NULL," ,.-!?\n");
        //printf("\n%s", word);
//}
   //  if(( fp = popen(message, "r")) == NULL){
    //perror("No file stream found.");
    //return -1;
    // }

      // else {
      // pclose(fp);

      // printf("FOUND.");
      //        }

  }

  // PUTS DECRYPTED STRING IN NEW FILE

  char copiedname[100];
  strcpy(copiedname, inputfile);
  strcat(copiedname, ".dec");
  FILE *newfile = fopen(copiedname, "w");
  fputs(newstring, newfile);

  //  free(newstring);
  fclose(input);
  fclose(newfile);

  return 0;

}

最佳答案

您已将 inputfile 设置为 argv[1],稍后您已使用 strcat 附加到它。不要这样做。你不拥有 argv。

strcat 函数将源字符串的副本附加到目标字符串,然后返回指向目标字符串的指针。它确实“添加两个字符串并返回结果”,这就是您使用它的方式。

关于c - 使用字符串 C 程序/Popen 时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38060476/

相关文章:

c++ - 是什么阻止了 __attribute__((packed)) 进入 ISO C/C++?

c - 以破折号为前缀的命令行参数有什么特别之处吗?

python - 从旧数据库复制到 Couchdb 中的新数据库

linux - 如何从 POS 打印机获取状态

android - 从 res/drawable 中检索字符串

c - 四舍五入以避免在随后的求和中四舍五入

c - 这个结构的大小是如何变成 4 字节的

python - 如何杀死超过 Linux 中 CPU 使用率和运行时限制的特定进程?

c - 基于规则的语法检查器

javascript - 根据数组值打乱字符串 - Javascript