2016-09-10 4 views
0

프로그래밍에 익숙하지 않습니다. 내가C : 텍스트 파일을 읽고 데이터 조작

한 2,009 제임스 스미스 2 18

이 2010 밥 데이비스 (5) 다음 형식의 텍스트 파일의 작업입니다 18

3 2010 앨런 톰슨 (15) (26)

4 2010 브래드 Haye에 (15) 26

(200)에 데뷔 한 내가 각 라인을 읽고

제임스 스미스처럼 의미있는 방식으로 콘솔에 인쇄되어하고 싶은 9와 일년에 2m를 벌어 들인다. 그는 xyzM의 평균 연 다음

최고 흑장미

와 함께 자신의 수명 기간 동안 18m를 얻었습니다 :
평균 적립 :?
총 플레이어 :

이것은 내가 지금까지 무엇을 가지고

: 당신이 파일 (EOF)의 끝을 만날 때까지

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

typedef char *string; 

int main() { 

    int i = 0, line = 5; 
    char ch[100]; 
    string array[4]; 

    FILE *myfile; 
    myfile = fopen("C:/Data/Players.txt","r"); 
    if (myfile== NULL) 
    { 
     printf("File Does Not Exist \n"); 
     return 1; 
    } 

    while(line--) 
    { 
     fscanf(myfile,"%s",&ch[i]); 
     printf("\n%s", &ch[i]); 
     array[0] = array[i]; 
     i++; 

     if(i=5) 
     { 
      printf("Yes"); 
     } 


    } 

    fclose(myfile); 

    return 0; 
} 
+1

거의 모든 초보자를위한 책이나 튜토리얼은 ['scanf' ] (http://en.cppreference.com/w/c/io/fscanf). –

답변

0

당신은 문자열을 구문 분석, 라인하여 파일 라인을 읽어야합니다. 이미 읽은 후에 만난 것을 알고 있습니다.

내 솔루션입니다 :

#include <stdio.h> 

int main() 
{ 
    FILE *file; 
    int line_number; 
    int year; 
    char fname[10]; 
    char lname[10]; 
    int m_a_year; 
    int m_over_lifetime; 

file = fopen("textfile.txt", "r"); 

while(!feof(file)) 
{ 
    fscanf(file, "%d %d %s %s %d %d", &line_number, &year, fname, lname, &m_a_year, &m_over_lifetime); 
    printf("%s %s made his debut in %d and earns %dm a year. He has earned %dm over his lifetime with an average of %dM a year\n\n", fname, lname, year, m_a_year, m_over_lifetime, (m_over_lifetime/(2016-year))); 
} 

fclose(file); 

    return 0; 
} 

출력 :

james smith made his debut in 2009 and earns 2m a year. He has earned 18m over h 
is lifetime with an average of 2M a year 

bob davies made his debut in 2010 and earns 5m a year. He has earned 18m over hi 
s lifetime with an average of 3M a year 

Allan Thomson made his debut in 2010 and earns 15m a year. He has earned 26m ove 
r his lifetime with an average of 4M a year 

Brad Haye made his debut in 2010 and earns 15m a year. He has earned 26m over hi 
s lifetime with an average of 4M a year 
+2

[왜 while (! feof (file))가 항상 잘못 되었습니까?] (http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – BLUEPIXY

+0

feof (file) 파일의 끝 부분에있는 EOF 바이트를 읽은 후에 만 ​​참입니다. @BLUEPIXY –

+2

아이디어는 'while (fscanf (...) == 6) {printf (...);}로 루프를 다시 작성하는 것입니다. }'. 그렇게하면 EOF와 모든 필드를 읽었는지 확인하는 것입니다. –

0

당신이 필요로하는 거의 모든 수행이 코드를 시도하십시오.

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

int main(void) { 
    int status; 
    int number; 
    FILE *fp; 
    if ((fp = fopen("C:/Data/Players.txt", "r+")) == NULL) { 
     printf("No such file\n"); 
     exit(1); 
    } 
    int x, y; 
    char firstname[10]; 
    char lastname[20]; 
    while (fscanf(fp, "%d %d %s %s %d %d", &number, &x, firstname, lastname, &y, &status) == 6) { 
     printf("%s %s made his debut in %d and earns %dm a year. He has earned %dm over his lifetime with an average of %dM a year\n\n", firstname, lastname, x, y, status, (status/(2016-x))); 
    } 
    return 0; 
} 

출력

james smith made his debut in 2009 and earns 2m a year. He has earned 18m over his lifetime with an average of 2M a year 

bob davies made his debut in 2010 and earns 5m a year. He has earned 18m over his lifetime with an average of 3M a year 

Allan Thomson made his debut in 2010 and earns 15m a year. He has earned 26m over his lifetime with an average of 4M a year 

Brad Haye made his debut in 2010 and earns 15m a year. He has earned 26m over his lifetime with an average of 4M a year 
+0

사람들을 위해 숙제를하는 것은 아무에게도 도움이되지 않습니다. – doron

0

당신은 방법이 더 간단하게 만들 수 있습니다. 먼저 변수 "Line"이 필요하지 않습니다. c가 파일의 끝에 도달하면 알 수있는 파일을 읽을 때, c에서 문자열 유형을 사용하지 않고 문자 배열을 사용하지만 코드에서는 문자열 배열을 쓸모 없게 만들었습니다. 단지 위의 주석이 정확하고 파일을 읽는 방법에 대한 아이디어를 줄 수있는 공간이 있습니다.

+0

welcome to stackOverflow. 여기에 조언이 정확할 수도 있지만, 효과가 있다고 생각되는 코드를 추가하는 것을 고려해보십시오. 이렇게하면 미래의 독자를 도울 수 있습니다. 또한 왜 OP 코드를 변경했는지에 대한 동기를 포함 시키십시오. – lmo

관련 문제