不打印的字符串(保留)

时间:2021-10-29 18:23:22

I am trying to build a code that uses a text file to calculate and group specific data that is provided.

我正在尝试构建一个代码,它使用一个文本文件来计算和分组提供的特定数据。

The current data set that I am trying to group is a list of country names with a single letter (char) that is next to it ("Australia B" or "China D") there are 32 sets of these, i currently have the country list in alphabetical order but the single letter or grouping letter is not printing connected to the country name and is staying in the original order. how do i connect this sting and character together, while being able to refer back to the single letter in another question.

当前数据集,我试图集团是国家名称的列表一个字母(char)旁边(B“澳大利亚”或“中国D”)有32组,我目前有国家列表按字母顺序排列,但单个字母或分组信不是印刷连接到国家名称和留在原来的顺序。我如何将这个sting和character连接在一起,同时又可以在另一个问题中引用单个字母。

This is what i have:

这就是我所拥有的:

#include "stdafx.h"
#include "stdlib.h"
#include <math.h>
#include "string.h"

void matchRead(FILE *match);
void teamRead(FILE *team);
void BubbleSort(int A[], const int arraySize);
void swap(int *a, int *b);

struct team_value {
    char name[17], group;
    int points = 0,
        played = 0,
        win = 0,
        loss = 0,
        draw = 0,
        goalsFor = 0,
        goalsAgainst = 0,
        goalDiff = 0;
}tV[32];

int main()
{
    int i = 0;
    errno_t err;
    FILE *team, *match;

    err = fopen_s(&team, "team.dat", "r");
    if (err == 0) {
        printf_s("team file opened\n\n");
        err = fopen_s(&match, "match.dat", "r");
        teamRead(team);
        if (err == 0) {
            printf_s("\nmatch file opened\n\n");
            matchRead(match);
        }
        else
            return(-1);
    }
    else
        return(-1);
    return 0;
}
void teamRead(FILE *team) {
    int j, i;
    char temp[17];
    for (i = 0; i < 32; i++) {
        fscanf_s(team, "%s %c\n", &tV[i].name, 17, &tV[i].group,17);
    }
    for (i = 0; i < 32; i++) {
        for (j = i + 1; j < 32; j++) {
            if (strcmp(tV[i].name, tV[j].name) > 0) {
                strcpy_s(temp, tV[i].name);
                strcpy_s(tV[i].name, tV[j].name);
                strcpy_s(tV[j].name, temp);
            }
        }
    }
    for (i = 0; i < 32; i++) {
        puts(tV[i].name);
        printf_s("%16s %1c\n", tV[i].name, tV[i].group);
    }
}

void matchRead(FILE *match) {
    char tempName1[17], tempName2[17], finalName1 = 0, finalName2 = 0;
    int tempScore1, tempScore2, finalScore1 = 0, finalScore2 = 0;
    while (!feof(match)) {
        fscanf_s(match, "%s%d%s%d", &tempName1, sizeof(tempName1), &tempScore1, &tempName2, sizeof(tempName2), &tempScore2);
        if (tempScore1 < 0 || tempScore2 < 0) {
            printf_s("%16s %1d %16s %1d - results invalid!\n", tempName1, tempScore1, tempName2, tempScore2);
        }
        else {
            tempScore1 = finalScore1;
            tempScore2 = finalScore2;
            //tempName1[17] = finalName1;
            //tempName2[17] = finalName2;
            printf_s("%16s %1d %16s %1d\n", tempName1, finalScore1, tempName2, finalScore2);
        }
    }
}

1 个解决方案

#1


0  

The answer to your question is that in your sorting loop, you only copy the name field:

您的问题的答案是,在您的排序循环中,您只复制name字段:

        if (strcmp(tV[i].name, tV[j].name) > 0) {
            strcpy_s(temp, tV[i].name);
            strcpy_s(tV[i].name, tV[j].name);
            strcpy_s(tV[j].name, temp);
        }

Instead, copy the entire entry:

相反,复制整个条目:

        if (strcmp(tV[i].name, tV[j].name) > 0) {
            struct team_value tmp;
            memcpy(&temp, &tV[i], sizeof(struct  team_value) );
            memcpy(&tV[i], &tV[j], sizeof(struct  team_value) );
            memcpy(&tV[j], &temp, sizeof(struct  team_value) );
        }

Having said that, it is a really bad idea to sort stuff by keep copying all your data around and around - better use something like a linked list and just shuffle pointers...

说了这句话,把所有的数据都复制过来,这样做是一个很糟糕的主意——最好使用类似链表的东西,然后拖拽指针……

#1


0  

The answer to your question is that in your sorting loop, you only copy the name field:

您的问题的答案是,在您的排序循环中,您只复制name字段:

        if (strcmp(tV[i].name, tV[j].name) > 0) {
            strcpy_s(temp, tV[i].name);
            strcpy_s(tV[i].name, tV[j].name);
            strcpy_s(tV[j].name, temp);
        }

Instead, copy the entire entry:

相反,复制整个条目:

        if (strcmp(tV[i].name, tV[j].name) > 0) {
            struct team_value tmp;
            memcpy(&temp, &tV[i], sizeof(struct  team_value) );
            memcpy(&tV[i], &tV[j], sizeof(struct  team_value) );
            memcpy(&tV[j], &temp, sizeof(struct  team_value) );
        }

Having said that, it is a really bad idea to sort stuff by keep copying all your data around and around - better use something like a linked list and just shuffle pointers...

说了这句话,把所有的数据都复制过来,这样做是一个很糟糕的主意——最好使用类似链表的东西,然后拖拽指针……