泡泡排序搞乱字符串值?

时间:2015-04-19 05:32:54

标签: c arrays struct

我有一个数据行的文件,格式为:

NAME,SUBJECT

我将所有这些行放入一个定义为:

的结构数组中
struct node{
char name[10];
char subject[15];
};

使用以下代码:

    i = 0;
    fp = fopen("data.csv", "r");
    while(fgets(buffer, sizeof(buffer), fp) != NULL) /* let's populate our structures with the data files */
    {
            token = strtok(buffer, del);
            strcpy(data[i].name, token);
            token = strtok(NULL, del);
            strcpy(data[i].subject, token);
            i++;
    }
    cols = i;

其中data是由以下内容定义的结构数组:

struct node data[25];

和del is:

char* del = ',\n';

我调用了sortData(data,cols);其定义如下:

void sortData(struct node data[], int cols)
{
    int i, unsorted;
    struct node temp;

do {
    unsorted = 0;

    for(i = 0; i < cols-1; i++)
        if( strcmp(data[i].name, data[i+1].name) > 0 )
        {
            temp = data[i];
            data[i] = data[i+1];
            data[i+1] = temp;
            unsorted = 1;
        }

} while(unsorted);
}

然而,在调用之后,似乎搞砸了主题的字符串值?这是数组之前的排序版本:

Name & Subject at index 0 is Brian and CSE 2315
Name & Subject at index 1 is Tanbir and CSE 2315
Name & Subject at index 2 is Summit and IE 3301
Name & Subject at index 3 is Adam and CSE 1310
Name & Subject at index 4 is Dominic and CSE 1320
Name & Subject at index 5 is Zackery and IE 3301
Name & Subject at index 6 is Nicholas and CSE 2315
Name & Subject at index 7 is Julianne and CSE 1310
Name & Subject at index 8 is Ana and CSE 1310
Name & Subject at index 9 is Daniel and CSE 2315
Name & Subject at index 10 is Amarachi and CSE 1310
Name & Subject at index 11 is Thamid and MATH 1426
Name & Subject at index 12 is Khoa and CSE 1320
Name & Subject at index 13 is Danny and CSE 1310
Name & Subject at index 14 is Alejandro and CSE 1310
Name & Subject at index 15 is Damian and MATH 1426
Name & Subject at index 16 is Dwij and MATH 1426
Name & Subject at index 17 is Michelle and CSE 2315
Name & Subject at index 18 is Grace and CSE 1320
Name & Subject at index 19 is Paul and CSE 2315
Name & Subject at index 20 is Hiba and IE 3301
Name & Subject at index 21 is Hari and CSE 1310
Name & Subject at index 22 is Elizabeth and CSE 1320
Name & Subject at index 23 is Keenan and IE 3301

并且在调用sortData函数之后:

Name & Subject at index 0 is Adam and CSE 1310
Name & Subject at index 1 is Alejandro and CSE 1310
Name & Subject at index 2 is Amarachi and CSE 1310
Name & Subject at index 3 is Ana and CSE 1310
Name & Subject at index 4 is Brian and CSE 2315
Name & Subject at index 5 is Damian and MATH 1426Daniel
Name & Subject at index 6 is Daniel and CSE 2315
Name & Subject at index 7 is Danny and CSE 1310
Name & Subject at index 8 is Dominic and CSE 1320
Name & Subject at index 9 is Dwij and MATH 1426Elizabeth
Name & Subject at index 10 is Elizabeth and CSE 1320
Name & Subject at index 11 is Grace and CSE 1320
Name & Subject at index 12 is Hari and CSE 1310
Name & Subject at index 13 is Hiba and IE 3301
Name & Subject at index 14 is Julianne and CSE 1310
Name & Subject at index 15 is Keenan and IE 3301
Name & Subject at index 16 is Khoa and CSE 1320
Name & Subject at index 17 is Michelle and CSE 2315
Name & Subject at index 18 is Nicholas and CSE 2315
Name & Subject at index 19 is Paul and CSE 2315
Name & Subject at index 20 is Summit and IE 3301
Name & Subject at index 21 is Tanbir and CSE 2315
Name & Subject at index 22 is Thamid and MATH 1426Zackery
Name & Subject at index 23 is Zackery and IE 3301

为什么有些名字会搞砸并坚持主题?

0 个答案:

没有答案