簡體   English   中英

程序不會打印txt文件中的所有行

[英]Program wont print all lines from txt file

編寫了該程序,該程序應該能夠從txt文件中打印出所有行,但是它只能打印出一個,現在已經看了1個小時,我找不到錯誤,可以尋求任何幫助! :)

1 16.07.2011 kl。 17.00 OB-FCN 2-0 6.965
1 17.07.2011 kl。 14.00 FCM-SIF 1-2 5.370
1 17.07.2011 kl。 16.00 ACH-HBK 3-0 2.227
1 17.07.2011 kl。 16.00 SDR-FCK 0-2 4.992
前4行。

#include <stdio.h>
#include <stdlib.h>
#define MAX_LINE_LGT 200
#define NAME_MAX 200
#define TEAM_MAX 200

struct team{
char name[NAME_MAX];
int five_or_more_goals;
};
typedef struct team team;

void read_data_1(const char *file_name, team teams[]){
FILE *ifp;
char team1[NAME_MAX];
char team2[NAME_MAX];
int goal1, goal2;
int dag, month, year;
double clock;
int attendance;
int round;
team local_match;

ifp = fopen(file_name, "r");

while (fscanf(ifp, "%d %d.%d.%d kl. %lf %4s - %4s %d - %d %d\n", &round, &dag, &month, &year, &clock, team1, team2, &goal1, &goal2, &attendance) == 10){
    printf("runde %d den %d %d %d klokken %.2lf, mellem %s og %s endte %d - %d %d så kampen\n", round, dag, month, year, clock, team1, team2, goal1, goal2, attendance);
    }

fclose(ifp);

   }

  int main(void) {
  team all_matches_teams[TEAM_MAX];
  read_data_1("superliga-2011-2012", all_matches_teams);

 return 0;
 }

每行輸入末尾的出勤值導致您出現問題。 您需要將其解析為浮點數或用句點分隔的兩個小數,而不只是一個小數。 假定出席人數從未達到一百萬,它應該與以下代碼更改一起使用:

int valuesRead;
int attendance;
int attend1, attend2;
[...]
while ((valuesRead = fscanf(ifp, "%d %d.%d.%d kl. %lf %4s - %4s %d - %d %d.%d\n", &round, &dag, &month, &year, &clock, team1, team2, &goal1, &goal2, &attend1, &attend2)) >= 10){
    if (valuesRead == 11)
        attendance = attend1 * 1000 + attend2;
    else
        attendance = attend1;
    printf("runde %d den %d %d %d klokken %.2lf, mellem %s og %s endte %d - %d %d så kampen\n", round, dag, month, year, clock, team1, team2, goal1, goal2, attendance);
}

謝謝您的投入,我現在就可以通過將出勤率變量更改為double來使其正常工作,並且可以正常工作,再次在此編寫代碼,如果有人有任何技巧可以更聰明地編寫它,請告訴我! :)

#include <stdio.h>
#include <stdlib.h>
#define MAX_LINE_LGT 200
#define NAME_MAX 200
#define TEAM_MAX 200

struct team{
char name[NAME_MAX];
int five_or_more_goals;
};
typedef struct team team;

void read_data_1(const char *file_name, team teams[]){
FILE *ifp;
char team1[NAME_MAX];
char team2[NAME_MAX];
int goal1, goal2;
int dag, month, year;
double clock;
double attendance;
int round;
team local_match;

ifp = fopen(file_name, "r");

while (fscanf(ifp, "%d %d.%d.%d kl. %lf %4s - %4s %d - %d %lf\n", &round, &dag, &month, &year, &clock, team1, team2, &goal1, &goal2, &attendance) == 10){
    printf("runde %d den %d %d %d klokken %.2lf, mellem %s og %s endte %d - %d %.3lf så kampen\n", round, dag, month, year, clock, team1, team2, goal1, goal2, attendance);
    }

fclose(ifp);

    }

  int main(void) {
  team all_matches_teams[TEAM_MAX];
  read_data_1("superliga-2011-2012", all_matches_teams);

  return 0;
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM