简体   繁体   English

C strcpy 使代码崩溃

[英]C strcpy crashes code

I have the following code, the code crashes whenever the strcpy function is called.我有以下代码,只要strcpy函数,代码就会崩溃。 When these lines are commented out the code does not crash.当这些行被注释掉时,代码不会崩溃。 What is wrong?怎么了?

char cities[80][17];
char char_distances[40][2];
int distances[40];
char cities_sorted[20][17];

while (!feof(Text)) {
    fscanf(Text, "%[^\t]\t%[^\t]\t%[^\n]\n",
           cities[i], cities[i + 1], char_distances[i]);
    distances[i] = atoi(char_distances[i]);
    printf("City_start: %s  City_end: %s  Distance: %d \n",
           cities[i], cities[i + 1], distances[i]);
    static char uniqueCities[21][17];
    int uniqueCitiesCount;
    for (int j = 0; j < 21; j++) {
        printf("%s\n", uniqueCities[i]);
        bool start_unique = !areEqual(cities[i], cities[j]);
        bool end_unique = !areEqual(cities[i], cities[j + 1]);
        if (start_unique) {
            strcpy(uniqueCities[uniqueCitiesCount], cities[i]);
            uniqueCitiesCount++;
        }
        if (end_unique) {
            strcpy(uniqueCities[uniqueCitiesCount], cities[i + 1]);
            strcpy(uniqueCities[uniqueCitiesCount], cities[i + 1]);
            uniqueCitiesCount++;
        }
    }
    i++;
}

Thanks谢谢

What's wrong?怎么了?

Well many things:很多事情:

  • you posted a code fragment: this is not enough information to get help diagnosing your problem.您发布了一个代码片段:这不足以帮助您诊断问题。 The code posted cannot be compiled and tested, does not even have definitions for all the symbols in the fragment: how is Text defined?发布的代码无法编译和测试,甚至没有片段中所有符号的定义: Text是如何定义的? how was it opened?它是如何打开的? is it guaranteed to be different from NULL ?是否保证与NULL不同?

  • i is not defined in the fragment, how is it defined?片段中没有定义i ,它是如何定义的? is it initialized?它被初始化了吗?

  • while (!feof(Text)) is not a good way to test for end of input, you should instead compare the result of fscanf() with the expected number of conversions. while (!feof(Text))不是测试输入结束的好方法,但您应该将fscanf()的结果与预期的转换次数进行比较。

  • fscanf(Text, "%[^\\t]\\t%[^\\t]\\t%[^\\n]\\n", does not have enough information to avoid buffer overflows. The maximum number of characters to store into the arrays should be specified this way: fscanf(Text, "%16[^\\t]\\t%16[^\\t]\\t%1[^\\n]\\n", . Note however that if the input is not consistent with these limitations, conversions will fail and the rest of the input file will be read out of sync. You shoud read the lines into a line buffer and use sscanf() to parse the lines. fscanf(Text, "%[^\\t]\\t%[^\\t]\\t%[^\\n]\\n",没有足够的信息来避免缓冲区溢出。存储到数组应该这样指定: fscanf(Text, "%16[^\\t]\\t%16[^\\t]\\t%1[^\\n]\\n", 。但是请注意,如果输入不是与这些限制一致,转换将失败并且输入文件的其余部分将被不同步读取。您应该将行读入行缓冲区并使用sscanf()来解析行。

  • char char_distances[40][2]; defines an array of array that can only contain 1 character and a null terminator.定义只能包含 1 个字符和空终止符的数组数组。 The distances must all be expressed as a single digit in the input file.在输入文件中,距离必须全部表示为一位数。 You should probably define the arrays with a larger size or convert the distance directly into the distances array with a %d conversion specifier.您可能应该定义更大尺寸的数组,或者使用%d转换说明符将距离直接转换为distances数组。

  • int uniqueCitiesCount; defines a local variable that is not initialized.定义一个未初始化的局部变量。 Using it in your loop invokes undefined behavior as you probably try to access beyond the end of the 2D array.在循环中使用它会调用未定义的行为,因为您可能会尝试访问二维数组的末尾。

  • printf("%s\\n", uniqueCities[i]); will print an empty string as no city has yet been copied to this array.将打印一个空字符串,因为尚未将任何城市复制到此数组中。

  • bool start_unique = !areEqual(cities[i], cities[j]); how are bool and areEqual() defined? boolareEqual()是如何定义的?

  • strcpy(uniqueCities[uniqueCitiesCount], cities[i + 1]); is duplicated.是重复的。 The index i + 1 is incorrect.索引i + 1不正确。

  • The logic in the loop is contorted and probably flawed.循环中的逻辑被扭曲并且可能存在缺陷。 You should just compare the city name with all entries in the uniqueCities and only add it the this array after the loop if it has not been found.您应该只将城市名称与uniqueCities所有条目进行比较,如果尚未找到,则仅将其添加到循环后的 this 数组中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM