分段错误处理strcmp

分段错误处理strcmp,c,segmentation-fault,strcmp,strcpy,strncmp,C,Segmentation Fault,Strcmp,Strcpy,Strncmp,我有以下代码: #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define RCVBUFSIZE 1024 void insertarOperacion(char *op){ char operacion[RCVBUFSIZE], *retrString = "RETR "; //< bool operacionError

我有以下代码:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define RCVBUFSIZE 1024

void insertarOperacion(char *op){
        char operacion[RCVBUFSIZE], *retrString = "RETR "; //<
        bool operacionError = false;

        printf("Para solicitar un fichero escriba get <nombreFechero>, para salir escriba quit\n");
        do{
            if(operacionError)
                printf("Opcion invalida\n");
            printf("Operacion: ");
            fgets(operacion, sizeof(operacion), stdin);
            operacion[strlen(operacion) - 1] = '\0';
            operacionError = true; printf("000000000\n");
        } while( ((strcmp(operacion, "quit") == 0) + (strncmp(operacion, "get", 3) == 0)) != 1);
        printf("111111111\n");
        if (strcmp(operacion, "quit") == 0){
            strcpy(operacion, "QUIT\r\n");
            operacion[strlen(operacion) - 1] = '\0';
            strcpy(op, operacion);
        }
        else if(strncmp(operacion, "get",3) == 0){printf("DALEEEKLAJDSFK");
            strcat(retrString, operacion + 4);printf("33333333333\n");
            strcat(retrString, "\r\n"); //>
            retrString[strlen(retrString) - 1] = '\0';
            printf("333333333334\n");
            strcpy(op, retrString);
            printf("333333333335\n");
        } printf("PORFI?");
    //  send(clientSock, retrString, RCVBUFSIZE, 0);
}
int main(){

    //int i = 10, j=20;
    char jiji[RCVBUFSIZE] = "lkajsdfkjdsf";
    insertarOperacion(jiji);
    printf("%s\n\n", jiji);
    insertarOperacion(jiji);
    printf("%s", jiji);



return 0;
}
#包括
#包括
#包括
#定义RCVBUFSIZE 1024
无效插入属性(字符*op){
字符操作[RCVBUFSIZE],*retrString=“RETR”//<
bool operacionError=false;
printf(“获得律师资格,退出律师资格”);
做{
如果(操作员错误)
printf(“残疾医生”);
printf(“操作:”);
fgets(操作、尺寸(操作)、标准DIN;
操作[strlen(操作)-1]='\0';
operacionError=true;printf(“000000000\n”);
}而(((strcmp(操作,“退出”)==0)+(strncmp(操作,“获取”,3)==0))!=1);
printf(“111111111\n”);
如果(strcmp(操作,“退出”)==0){
strcpy(操作,“退出”\r\n);
操作[strlen(操作)-1]='\0';
strcpy(op,operacion);
}
else if(strncmp(操作,“get”,3)==0{printf(“daleeeeklajdsfk”);
strcat(返回字符串,操作+4);printf(“33333\n”);
strcat(retrString,“\r\n”);/>
retrString[strlen(retrString)-1]='\0';
printf(“33333 4\n”);
strcpy(op,retrString);
printf(“33333 5\n”);
}printf(“PORFI?”);
//发送(clientSock、retrString、RCVBUFSIZE、0);
}
int main(){
//int i=10,j=20;
char jiji[RCVBUFSIZE]=“lkajsdfkjdsf”;
插入属性(jiji);
printf(“%s\n\n”,jiji);
插入属性(jiji);
printf(“%s”,jiji);
返回0;
}
问题是“退出”部分工作得很好。“获取”部分不起作用。如果我输入例如:“get rekt.txt”。我得到了以下输出:

000000000

111111111

分段故障(堆芯转储)

这:

字符串文本,这意味着此指针指向的内存是只读的,因此无法修改/写入

您的“get”部分在试图修改该字符串文字时完全违反了上述规定:

strcat(retrString, operacion + 4);
strcat(retrString, "\r\n"); //>
retrString[strlen(retrString) - 1] = '\0';
这会导致分割错误

为了解决这个问题,只需声明char指针,然后为其分配空间,填充它,然后随意修改它

例如:

char* retrString = NULL;
retString = malloc(sizeof("RETR ") * sizeof(char) + 1); // +1 for the null terminator
strcpy(retrString, "RETR ");
// do your get thing then
strcat(retrString, operacion + 4);
strcat(retrString, "\r\n"); //>
retrString[strlen(retrString) - 1] = '\0';

您是否尝试过为retrString分配空间,而不是只分配一个文本?
retrsing
是一个初始化为指向静态字符串的字符指针。get部分正在尝试写入该静态区域。您需要为其分配空间。当调用任何堆分配函数:malloc calloc realloc时,始终检查(!=NULL)返回值以确保操作成功。如果未成功,请使用错误消息调用peror()。此函数将输出错误消息和系统认为stderr发生错误的原因的文本,以便于理解和可读:1)if-else的单独代码块while do..while`调用C库函数(如fgets())时,通过单个空行切换大小写默认值,始终检查返回值以确保操作成功–发布的代码缺少以下语句:
\include
函数:
strlen()
strcpy()
strcat()
,等等。表达式:
sizeof(char)
在C标准中定义为
。任何东西乘以1都没有效果。在对
malloc()`的调用中,该表达式只会使代码变得混乱,使其更难理解、调试等
char* retrString = NULL;
retString = malloc(sizeof("RETR ") * sizeof(char) + 1); // +1 for the null terminator
strcpy(retrString, "RETR ");
// do your get thing then
strcat(retrString, operacion + 4);
strcat(retrString, "\r\n"); //>
retrString[strlen(retrString) - 1] = '\0';