데이터베이스에서 첫 줄을 스캔하고 인쇄 한 후이 코드가 충돌합니다. 나는 정말로 이것에 대한 해결책을 찾지 못했습니다.첫 번째 줄 스캔/인쇄 후 코드가 충돌 함
충돌의 샷 : 데이터베이스의
내용 :
Matthew Summers 53901523 256325 135500
Jacob Sutherland 52392302 723232.2 1200000
Michael Phelps 58238211 971000.52 653350
Aaron Gordon 59923325 325700.92 623320
Vasil Maglaperidze 59952323 189900.32 330000
Avtandil Shoshiashvili 95234322 432000.72 723023
Michael Jordan 35252372 120899.75 50000
Daniel Whiteman 85238202 178500.53 349800
James Oneal 98773235 90750.23 197050
Haytheim Russels 19326233 178250.22 221580
내 코드 :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CHAR_BUF 128
#define DATA_FILE "database.txt"
typedef struct client
{
char fname[CHAR_BUF];
char lname[CHAR_BUF];
int pnumber;
float wins;
float loses;
float ratio;
}client;
int ReadData(FILE *fp);
int main()
{
//int lines=0;
//client client[i];
FILE *fp = fopen(DATA_FILE, "r"); // opens file
if(fp==NULL) // checks if .txt file is empty
{
printf("Database is empty.");
exit(1);
}
ReadData(fp); // Calls function to read db
//lines = ReadData(fp);
//printf("Line amount: %d", lines);
}
/* This function reads data from database
* and assigns values to their variables
*/
int ReadData(FILE *fp)
{
int i=0;
client client[i];
while(!feof(fp))
{
fscanf(fp, "%s %s %d %f %f", client[i].fname, client[i].lname,
&client[i].pnumber, &client[i].wins, &client[i].loses);
printf("%s %s %d %.2f %.2f\n", client[i].fname, client[i].lname,
client[i].pnumber, client[i].wins, client[i].loses);
i++;
}
return i;
}
오른쪽 도구는 디버거이다. 스택 오버플로를 묻기 전에 코드를 단계별로 실행해야합니다. 자세한 도움말은 [작은 프로그램 디버깅 방법 (Eric Lippert 작성)] (https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)을 참조하십시오. 문제를 재현하는 [최소, 완료 및 확인 가능] (http://stackoverflow.com/help/mcve) 예제와 함께 해당 질문을 \ [편집]해야합니다. 디버거. –
왜이 태그가 C++입니까? – Biffen
'클라이언트 클라이언트 [i];'는 클라이언트 클라이언트 [0]입니다. – BLUEPIXY