I am very new to C and C+, and I am trying to teach myself from VERY outdated text books, and I can't seem to figure this out.
No matter what I do, the code below will either not compile with errors, or compile and then seg-fault when it hits the qsort function.
I have tried all changing the symbols around, but without good C programing skills, I'm grasping at straws.
I just want this to compile and not seg fault.
If someone could look it over, and maybe tell me where and why its having a problem. Maybe a breif explaination of how to copy a pointer to a 2-d array, and vice-versa.
Oh yea, I use GCC and Linux. This example may be off a * or & here and there. I think.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
char tstring[256];
char tgot[256];
char tname[256];
char *tdata[500][256];
int tfi=0;
int i=0;
DIR *pd;
FILE *pFile;
struct dirent *allp;
static int scomp(const void *, const void *);
int main(){
pd = opendir ("/mp3s"
;
if ( pd != NULL ) {
while ( (allp = readdir(pd)) ) {
sprintf(tstring,"%s",allp->d_name);
strcpy(tgot,tstring);
strcpy(tname,(&tgot[0]));
sprintf(*tdata[tfi],"%s",&tname);
tfi++;
}
}
tfi=tfi-1;
closedir(pd);
qsort(tdata, tfi, sizeof tdata[0], scomp);
pFile = fopen ("List.m3u","wt"
;
do {
fputs(tdata[ti],pFile);
puts(tdata);
i++;
}while (i<=tfi)
fclose (pFile);
return 0;
}
static int scomp(const void *p1, const void *p2){
char *a = * (char **) p1;
char *b = * (char **) p2;
return strcasecmp(a,b);
}
ANY help would be great.
No matter what I do, the code below will either not compile with errors, or compile and then seg-fault when it hits the qsort function.
I have tried all changing the symbols around, but without good C programing skills, I'm grasping at straws.
I just want this to compile and not seg fault.
If someone could look it over, and maybe tell me where and why its having a problem. Maybe a breif explaination of how to copy a pointer to a 2-d array, and vice-versa.
Oh yea, I use GCC and Linux. This example may be off a * or & here and there. I think.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
char tstring[256];
char tgot[256];
char tname[256];
char *tdata[500][256];
int tfi=0;
int i=0;
DIR *pd;
FILE *pFile;
struct dirent *allp;
static int scomp(const void *, const void *);
int main(){
pd = opendir ("/mp3s"
if ( pd != NULL ) {
while ( (allp = readdir(pd)) ) {
sprintf(tstring,"%s",allp->d_name);
strcpy(tgot,tstring);
strcpy(tname,(&tgot[0]));
sprintf(*tdata[tfi],"%s",&tname);
tfi++;
}
}
tfi=tfi-1;
closedir(pd);
qsort(tdata, tfi, sizeof tdata[0], scomp);
pFile = fopen ("List.m3u","wt"
do {
fputs(tdata[ti],pFile);
puts(tdata);
i++;
}while (i<=tfi)
fclose (pFile);
return 0;
}
static int scomp(const void *p1, const void *p2){
char *a = * (char **) p1;
char *b = * (char **) p2;
return strcasecmp(a,b);
}
ANY help would be great.