Problem: When i tried to compile my file and run, it has segmentation problem. When I passed the file to my friend (he is using the same version of ubuntu), the server will be able to run. I am wondering why?
Below will be my code for the whole page. Personally I feel that there is no problem with it but I will just paste it for reference in case anyone asks for it.
void readNStoreData () { char words[MAX]; char *wholeLine; char* delimiter = ","; int cflag = 0; int x, count = 0; char input; FILE *countryFile; countryFile = fopen("Countries.txt","r"); if (!countryFile) { exit(EXIT_FAILURE); } while (fgets (words, MAX - 1, countryFile) != NULL) { //atof to convert string to double //split a single line into individual tokens indicating , as the delimeter //afterwards store them into array wholeLine = strtok (words, delimiter); strcpy (records [count].TDL, wholeLine); wholeLine = strtok (NULL, ","); strcpy (records [count].cName, wholeLine); wholeLine = strtok (NULL, delimiter); strcpy (records [count].FIPS104, wholeLine); wholeLine = strtok (NULL, delimiter); strcpy (records [count].ISO2, wholeLine); wholeLine = strtok (NULL, delimiter); strcpy (records [count].ISO3, wholeLine); wholeLine = strtok (NULL, delimiter); records [count].ISO = atof(wholeLine); wholeLine = strtok (NULL, delimiter); strcpy (records [count].cCapital, wholeLine); wholeLine = strtok (NULL, delimiter); strcpy (records [count].cRegion, wholeLine); wholeLine = strtok (NULL, delimiter); strcpy (records [count].cCurrencyName, wholeLine); wholeLine = strtok (NULL, delimiter); strcpy (records [count].cCurrencyCode, wholeLine); wholeLine = strtok (NULL, delimiter); records [count].cPopulation = atof(wholeLine); count++; } fclose(countryFile); //close file } I hope someone will be able to spot the mistake somewhere. Thanks to advance to those who helped!
run the gdb and the error is actually this line. it is situated in this function.
l(gdb) frame 1 l#1 0x08048936 in readNStoreData () at testserver.c:61 61 strcpy (records [count].cName, wholeLine);
valgrind,gdb, perhapsstrace? Do you know on what line the error occurs? I'm not going to debug this for you, this is horrible code, why did you usestrcatlike that if you know aboutsprintf?