C读、写和操作文件

C读、写和操作文件,c,C,我有一份档案,里面有团队的详细情况。我需要代码来读取文件,并在第二个文件中写入获胜百分比。我还需要使用指定的搜索功能搜索团队的细节。代码未写入百分比文件。当菜单显示时,将打印第一个文件的内容,但代码甚至在输入搜索选项之前就退出了。我只是在学习C。我很想知道为什么它不适合我 #include <stdio.h> #define NUM_TEAMS 50 #define LEN_LINE 40 #define LEN_NAME 25 char Name[NUM_TEAMS][LEN_

我有一份档案,里面有团队的详细情况。我需要代码来读取文件,并在第二个文件中写入获胜百分比。我还需要使用指定的搜索功能搜索团队的细节。代码未写入百分比文件。当菜单显示时,将打印第一个文件的内容,但代码甚至在输入搜索选项之前就退出了。我只是在学习C。我很想知道为什么它不适合我

#include <stdio.h>

#define NUM_TEAMS 50
#define LEN_LINE 40
#define LEN_NAME 25

char Name[NUM_TEAMS][LEN_NAME] = { 0 },
    league[NUM_TEAMS][LEN_NAME],
    division[NUM_TEAMS][LEN_NAME], 
    line[LEN_LINE];

int wins[NUM_TEAMS] = { 0 }, 
    losses[NUM_TEAMS] = {0}, 
    ties[NUM_TEAMS] = {0}, 
    pWin[NUM_TEAMS] = {0};

int main (void) {

    //getting the file
    FILE *filePtr;
    filePtr = fopen ("NFLStandings_20171031.txt", "r");
    if (filePtr == NULL) {
        printf ("Error: File did not open");
    }

    //executing
    displayWelcome ();

    select (filePtr);

    fclose (filePtr);

    return 0;
}

void displayWelcome ()
{
    printf ("............WElCOME TO THE FOOTBALL ANALYSIS.............\n\n\n\n");
}

//read file
void readFile (FILE * filePtr)
{
    int index = 0, count;

    while (index < NUM_TEAMS && fgets (line, sizeof (line), filePtr) != NULL) {
        sscanf (line, "%s%s%s%i%i%i", Name[index], league[index],
                division[index], &wins[index], &losses[index], &ties[index]);
        printf ("%s team in %s league and  %s division has won [%i] "
                "losing [%i] and tied in [%i] times \n",
                Name[index], league[index], division[index], wins[index],
                losses[index], ties[index]);
        index++;
    }

}

//find by name
void searchName (char name[], FILE * filePtr)
{
    int index = 0;

    while (index < NUM_TEAMS && fgets (line, sizeof (line), filePtr) != NULL) {
        sscanf (line, "%s%s%s%i%i%i", Name[index], league[index],
                division[index], &wins[index], &losses[index], &ties[index]);
        if (Name[index] == name) {
            printf ("The following team has the name");
            printf ("%s team in %s league and  %s division has won [%i] "
                    "losing [%i] and tied in [%i] times \n",
                    Name[index], league[index], division[index], wins[index],
                    losses[index], ties[index]);
            index++;
        }
    }
    if (index == 0)
        printf ("Sorry! No teams Found with such name");
}

//find by league
void searchLeague (char league1[], FILE * filePtr)
{
    int index = 0;

    while (index < NUM_TEAMS && fgets (line, sizeof (line), filePtr) != NULL) {
        sscanf (line, "%s%s%s%i%i%i", Name[index], league[index],
                division[index], &wins[index], &losses[index], &ties[index]);
        if (league[index] == league1) {
            printf ("The League has the following teams");
            printf ("%s team in %s league and  %s division has won [%i] "
                    "losing [%i] and tied in [%i] times \n",
                    Name[index], league[index], division[index], wins[index],
                    losses[index], ties[index]);
            index++;
        }
    }
    if (index == 0)
        printf ("Sorry! No teams Found with such League  name");
}

//search by percentage win
void searchpWin (char pWin_[], FILE * filePtr)
{
    int index = 0;

    while (index < NUM_TEAMS && fgets (line, sizeof (line), filePtr) != NULL) {
        sscanf (line, "%s%s%s%i%i%i", Name[index], league[index],
                division[index], &wins[index], &losses[index], &ties[index],
                &pWin[index]);
        if (pWin[index] == pWin_) {
            printf ("The Teams with the Percentage Win are  the following ");
            printf ("%s team in %s league and  %s division has won [%i] "
                    "losing [%i] and tied in [%i] times \n",
                    Name[index], league[index], division[index], wins[index],
                    losses[index], ties[index]);
            index++;
        }
    }
    if (index == 0)
        printf ("Sorry! No teams Found with such Percentage Win");
}

void searchDivision (char division1[], FILE * filePtr)
{
    int index = 0;

    while (index < NUM_TEAMS && fgets (line, sizeof (line), filePtr) != NULL) {
        sscanf (line, "%s%s%s%i%i%i", Name[index], league[index],
                division[index], &wins[index], &losses[index], &ties[index]);
        if (division[index] == division1) {
            printf ("The Division has the following teams");
            printf ("%s team in %s league and  %s division has won [%i] "
                    "losing [%i] and tied in [%i] times \n",
                    Name[index], league[index], division[index], wins[index],
                    losses[index], ties[index]);
            index++;
        }
    }
    if (index == 0)
        printf ("Sorry! No teams Found with such Division name");
}

//selection user Menu
void select (FILE * filePtr)
{
    int selection1 = 0;
    int selection2 = 0;

    do {
        printf ("Select the options(by typing 1,2 or 3) below to proceed \n"
                " 1. Print The Teams \n 2. Search Team \n 3. Exit \n");
        selection1 = scanf ("%d", &selection1);

        //incorrect choice
        if (selection1 > 2) {
            printf ("Invalid option! Try again");

        }
        //reading file
        if (selection1 == 1) {
            readFile (filePtr);
        }
        //option 2 Search data
        if (selection1 == 2) {
            printf ("Search by :\n 1. Team Name \n 2. By league \n "
                    "3. By division \n 4. By percentage wins \n");
            selection2 = scanf ("%d", &selection2);
        }

        switch (selection2) {
            case 1:
                printf ("Team name : \n");
                char name[10];
                scanf ("%c", name);

                searchName (name, filePtr);
                break;

            case 2:
                printf ("League name : \n");
                char league[10];
                scanf ("%c", league);
                searchLeague (league, filePtr);
                break;
            case 3:
                printf ("Division name : \n");
                char division[10];
                scanf ("%c", division);
                searchDivision (division, filePtr);
                break;
            case 4:
                printf ("Percentage win : \n");
                char pWin[10];
                scanf ("%c", pWin);
                searchpWin (pWin, filePtr);

                break;
            default:
                printf ("Invalid option.Try again!\n\n");
                break;
        }
    } while (selection1 == 0 || selection1 > 3);
}
#包括
#定义NUM_团队50
#定义LEN_第40行
#定义LEN_名称25
字符名[NUM_TEAMS][LEN_Name]={0},
联赛[球队数量][球队名称],
部门[团队数量][列名],
线[蓝线];
int赢[NUM_团队]={0},
损失[NUM_TEAMS]={0},
ties[NUM_TEAMS]={0},
pWin[NUM_TEAMS]={0};
内部主(空){
//获取文件
文件*filePtr;
filePtr=fopen(“NFLStandings_20171031.txt”,“r”);
if(filePtr==NULL){
printf(“错误:文件未打开”);
}
//执行
欢迎你();
选择(文件管理器);
fclose(文件管理器);
返回0;
}
void displayWelcome()
{
printf(“……欢迎来到足球分析………\n\n\n”);
}
//读取文件
void readFile(文件*filePtr)
{
int index=0,计数;
while(index2){
printf(“无效选项!重试”);
}
//读取文件
如果(选择1==1){
读取文件(文件管理器);
}
//选项2搜索数据
如果(选择1==2){
printf(“搜索者:\n 1.团队名称\n 2.按联盟\n”
“3.按除法\n 4.按百分比获胜\n”);
selection2=scanf(“%d”和&selection2);
}
开关(选择2){
案例1:
printf(“团队名称:\n”);
字符名[10];
scanf(“%c”,名称);
searchNa
    selection1 = scanf ("%d", &selection1);
    scanf ("%d", &selection1);
scanf ("%c", name);
scanf ("%s", name);
if (Name[index] == name)