简体   繁体   English

对 C 中的 txt 文件进行冒泡排序的程序

[英]Program to bubble sort a txt file in C

I'm writing a C program to be given a txt file containing names of player and how many wins they have (its for a bigger game project), and sort them from highest to lowest amount of wins.我正在编写一个 C 程序来获得一个包含玩家姓名和他们有多少胜利的 txt 文件(对于更大的游戏项目来说),并将它们从最高到最低的胜利数量排序。 While the code compiles and successfully orders the wins im experiencing a problem with the names where they seem to be stacking on top each other.虽然代码编译并成功订购了胜利,但我遇到了名称问题,它们似乎相互堆叠。


#include<stdio.h>
 #include<stdlib.h>

    char name[20];
    int wins[20];


void main()
{

    FILE *fp;
    int i=0,size,j,swap;
    char ch; 

    fp=fopen("record.txt","r");
    if(fp==NULL)
    { 
    printf("\n Cannot open the file \n");
    exit(0);
    }
    while(ch!=EOF)
    {
    fscanf(fp,"%s %d",&name[i],&wins[i]);  
    ch=fgetc(fp); 
    i++;  
    } 
    size=i-1;
    for(i=1;i<size;++i)
    for(j=0;j<size-i;j++)
    if(wins[j+1]>wins[j])

    {

        swap    = wins[j];
        wins[j]   = wins[j+1];
        wins[j+1] = swap;

        swap      = name[j];
        name[j]   = name[j+1];
        name[j+1] = swap;
    }
    fp=fopen("sortedRecord.txt","w");
    for(i=0;i<size;i++){
    fprintf(fp,"%s %d \n",&name[i],wins[i]);
    printf ("%s %d \n", &name[i],wins[i]); 
    }


    fclose(fp); 

}

Here is the input file "record.txt"这是输入文件“record.txt”

Andrew 5
Billboy 10
Hill 7
Mill 1

And here is what i get when i run it.这是我运行它时得到的。

BHAMill 10 
HAMill 7 
AMill 5 
Mill 1 

I'm new to code so i know mine sucks but for the life of me i cant see exactly where its going wrong.我是编码新手,所以我知道我的很糟糕,但对于我的生活,我无法确切地看到它哪里出了问题。 Any help or advice is appreciated.任何帮助或建议表示赞赏。

As jwdonahue said the issue is with your definition of name .正如 jwdonahue 所说,问题在于您对name的定义。 char name[20] creates an array of 20 char s, not 20 strings. char name[20]创建一个由 20 个char组成的数组,而不是 20 个字符串。

When you run the while(ch!=EOF) loop what's happening?当你运行while(ch!=EOF)循环时发生了什么? First time through you find the address of the 0th element in name and write Andrew there so name has ['A', 'n', 'd', 'r', 'e', 'w', '\0', '\0', '\0', '\0'] (the \0 is the end of string character).第一次通过你找到name中第 0 个元素的地址并在那里写Andrew所以name['A', 'n', 'd', 'r', 'e', 'w', '\0', '\0', '\0', '\0']\0是字符串字符的结尾)。 Second time through you find the address of the 1st element in name and write Billboy , but the 0th element is still there and hasn't changed so you end up with the contents being ['A', 'B', 'i', 'l', 'l', 'b', 'o', 'y', '\0', '\0'] .第二次通过你在name中找到第一个元素的地址并写Billboy ,但是第 0 个元素仍然存在并且没有改变所以你最终的内容是['A', 'B', 'i', 'l', 'l', 'b', 'o', 'y', '\0', '\0'] Adding Hill to the 2nd position results in ['A', 'B', 'H', 'i', 'l', 'l', '\0', 'y', '\0', '\0'] .Hill添加到第二个 position 会导致['A', 'B', 'H', 'i', 'l', 'l', '\0', 'y', '\0', '\0'] Then finally adding Mill gives the array ['A', 'B', 'H', 'M', 'i', 'l', 'l', '\0', '\0', '\0'] .然后最后添加Mill给出数组['A', 'B', 'H', 'M', 'i', 'l', 'l', '\0', '\0', '\0']

When you then go to sort scores you are sorting the characters in this array which end up as ['B', 'H', 'A', 'M', 'i', 'l', 'l', '\0', '\0', '\0'] (only the first four characters will be affected by your sort).当您然后 go 对分数进行排序时,您正在对该数组中的字符进行排序,这些字符最终为['B', 'H', 'A', 'M', 'i', 'l', 'l', '\0', '\0', '\0'] (只有前四个字符会受您的排序影响)。 In your print statement you then print the character array starting at the 0th, 1st, 2nd and 3rd positions respectively so you get BHAMill , HAMill , AMill , and Mill .然后,在您的打印语句中,您分别从第 0、第 1、第 2 和第 3 个位置开始打印字符数组,以便获得BHAMillHAMillAMillMill

Hopefully that should help you enough to get you unstuck.希望这对你有足够的帮助,让你摆脱困境。 :) :)

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

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