I have this code:
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,0,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");
}*/
What I get when running it is a seg fault when trying to print.
If I comment out qsort
and the printing for
, and comment in the print data array part, I get, as expected, all my entries in the order I inserted them in.
If I leave qsort
out, but keep the for loop as my printing method, I still get a seg fault.
1.The strings in the data array come from a file so they re probably not null terminated. However, I am hesitant to add a null byte cause it must not be there when I write the sorted array back in the file.
Please tell me to further explain anything I haven't done a good job explaining. Thanks.
Edit: Entire code cause it turns out I can't locate the problem
#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],O_RDWR|O_CREAT|O_APPEND,S_IRWXU);
write(fd,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],O_RDWR|O_CREAT,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],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,0,start);
read(fd,data[i],(end-start));
data[i][end - start] = 0;
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");
}*/
}
}while(1);
}
I'm going to guess that MAX_SIZE
is the maximum size of a string.
But it should be sizeof(char*)
.
The function qsort
expects a parameter that indicates the size of each element.
The size of each element in a data type char**
is the size of char*
.
Here's an example, similar to your program, that's useful for quick testing.
#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;
}
Regarding your updated code, after this line:
read(fd,data[i],(end-start));
You should null-terminate your string.
data[i][end - start] = 0;
I say this with the assumption that end - start
does not account for a null-terminating character and that you're not storing null-terminated characters in the file you're reading from.
Regarding your additional update, if you don't want to write the null-terminator to the file, then just find the length of the string using strlen
before writing it to the file. Your other option would be to write a wrapper for qsort
that looks something like this.
int cmp_string_block(const void* a, const void* b) {
return memcmp(a, b, MAX_SIZE);
}
And then pass that function to qsort
instead of strcmp
.