【发布时间】:2012-02-09 13:13:36
【问题描述】:
问题:当我尝试编译我的文件并运行时,它有分段问题。当我将文件传递给我的朋友(他使用相同版本的 ubuntu)时,服务器将能够运行。我想知道为什么?
下面是我整个页面的代码。 个人觉得没啥问题,不过贴出来供大家参考。
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
}
我希望有人能够在某个地方发现错误。 感谢帮助的人!
运行gdb,错误实际上是这一行。 它位于这个函数中。
l(gdb) frame 1
l#1 0x08048936 in readNStoreData () at testserver.c:61
61 strcpy (records [count].cName, wholeLine);
【问题讨论】:
-
您是否尝试使用
valgrind、gdb或strace进行调试?你知道错误发生在哪一行吗?我不会为你调试这个,这是可怕的代码,如果你知道sprintf,你为什么要这样使用strcat? -
@cha0site 我已调试,这是错误。
标签: linux ubuntu client segmentation-fault