打印字符串数组行时出现段故障,按字符打印

我有此代码:

char **data;
int start = 0;

data = malloc(all_names * sizeof(char*));

        fd=open(argv[1],O_RDWR|O_CREAT,S_IRWXU);
        for(i=0; i<all_names; i++){
            data[i] = malloc((MAX_SIZE+1)*sizeof(char));

            int end = atoi(positions[i]);
            lseek(fd,start);
            read(fd,data[i],(end-start));
            data[i][end - start] = 0; //line edited in after answer
            start = end;

        }

        qsort(data,all_names,sizeof(char*),strcmp);

        for(int i=0; i<all_names; ++i)
        {
            printf("%s\n",data[i]);
        }

        /*//print data array
        start = 0;
        for(i=0; i<all_names; i++){
            int end = atoi(positions[i]);
            for(j=0;j<(end-start) ;j++){
                printf("%c",data[i][j]);
            }
            printf("\n");
        }*/

我在运行时得到的是尝试打印时出现的段错误。

如果我注释掉qsort和打印for,并注释 print数据数组部分,则可以按预期获得所有输入的顺序将它们插入。

如果我将qsort留在外面,但是将for循环作为我的打印方法,我仍然会遇到段错误。

1。数据数组中的字符串来自文件,因此它们可能不会以null结尾。但是,我不愿意添加一个空字节,因为当我将排序后的数组写回到文件中时,它必须不存在

  1. ** positions数组包含除第一个条目以外的每个条目的起点。例如。如果我插入“ alpha 1”,然后插入“ beta 2”,则positions [0] =9。

请告诉我进一步解释所有我做得不好的事情。谢谢。

编辑:整个代码导致我找不到问题

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>

#define MAX_SIZE 50

int updateCounter(int pcounter,char *str){
int m,charcount = 0;
for(m=0; str[m]; m++) {
        charcount ++;
}
//charcount--;
printf("chars: %d \n",charcount);

pcounter = pcounter + charcount;
printf("pcounter = %d \n",pcounter);

return pcounter;
}

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

int option,i,j;
FILE *fptr;
char *name;
int dcounter,pcounter = 0;
int fd;
char **positions,**data;
int all_names=0; //keeps track of how many names are currently stored
int start = 0; //first byte of word to read in data.bin
char *filename = argv[2];

name=(char*)malloc((MAX_SIZE+1)*sizeof(char));

do{
    printf("MENU: \n 1.Insert \n 2.Delete \n 3.Search \n 4.Display \n");


    printf("Please choose 1-4\n");
    scanf("%d",&option);
    while(getchar() != '\n');

    //Insert
    if(option==1){

        printf("Insert name: ");
        fgets(name,MAX_SIZE,stdin);
        name[strcspn(name,"\n")]=0;

        fd=open(argv[1],O_RDWR|O_CREAT|O_APPEND,S_IRWXU);
        write(fd,name,strlen(name));

        pcounter = updateCounter(pcounter,name);

        char passpos[5];
        sprintf(passpos,"%d",pcounter); //int to string
        fd=open(argv[2],passpos,3);
        write(fd," ",1);

        all_names++;
        printf("all names: %d\n",all_names);

        positions = malloc(all_names * sizeof(char*));

        //create pos array
        fd=open(argv[2],S_IRWXU);
        for(i=0; i<all_names; i++){
            positions[i] = malloc((MAX_SIZE+1)*sizeof(char));
            for(j=0; ;j++){
                read(fd,&positions[i][j],1);
                if (positions[i][j] == ' ') {
                    break;
                }
            }
        }
        //print pos array
        for(i=0; i<all_names; i++){
            printf("%s\n",positions[i]);
        }

        //create data array
        data = malloc(all_names * sizeof(char*));

        fd=open(argv[1],(end-start));
            data[i][end - start] = 0;
            start = end;

        }

        qsort(data,data[i][j]);
            }
            printf("\n");
        }*/

    }

}while(1);

}

as516344224 回答:打印字符串数组行时出现段故障,按字符打印

我猜想MAX_SIZE是字符串的最大大小。

但是应该是sizeof(char*)

函数qsort需要一个参数,该参数指示每个元素的大小。 数据类型char**中每个元素的大小为char*的大小。

这是一个类似于您的程序的示例,对快速测试很有用。

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

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

    int all_names = argc;

    char** data = argv;

    qsort(data,all_names,sizeof(char*),strcmp);

    for(int i=0; i<all_names; ++i)
    {
        printf("%s\n",data[i]);
    }

    return 0;
}

关于您更新的代码,在此行之后:

read(fd,data[i],(end-start));

您应该对字符串进行空终止。

data[i][end - start] = 0;

我这样说是假设end - start不考虑以零结尾的字符,并且您没有在读取的文件中存储以零结尾的字符。

关于其他更新,如果您不想将空终止符写入文件,则只需在使用strlen查找字符串之前将其写入文件即可。您的另一种选择是为qsort写一个看起来像这样的包装器。

int cmp_string_block(const void* a,const void* b) {
    return memcmp(a,b,MAX_SIZE);
}

然后将该函数传递给qsort而不是strcmp

本文链接:https://www.f2er.com/3020768.html

大家都在问