2016-08-13 6 views
-1

오늘 저는 Linux mint에서 C로 텍스트 파일을 사용하려고했지만 텍스트가 표시되지 않습니다. 해결하도록 도와주세요. 텍스트 파일을 사용 중입니다. 작동하지 않습니다.

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

int main() 
{ 
    int account; 
    char name[30]; 
    float balance; 
    FILE *fp; 
    if((fp = fopen("tin", "w")) == NULL) { 
     printf("File could not be opened\n"); 
     exit(1); 
    } 
    else { 
     printf("Enter the account, name, and balance.\n"); 
     printf("Enter EOF to end input.\n"); 
     printf("?"); 
     scanf("%d%s%f", &account, name, &balance); 
     while(!feof(stdin)) { 
      fprintf(fp, "%d %s %2.f\n", account, name, balance); 
      printf("?"); 
      scanf("%d%s%f", &account, name, &balance); 
     } 
     fclose(fp); 
    } 
    return 0; 
} 

내 터미널에서이 코드를 실행

click here to see the picture

나는

this가 대단히 감사합니다 얻을.

+0

입력 사항을 표시하십시오. – BLUEPIXY

+0

실수로 죄송합니다. 이제 이미 보여 드렸습니다. –

+0

당신이 직면 한 정확한 오류를 공유 할 수 있다면 좋을 것입니다. 컴파일 또는 런타임 오류 –

답변

0

입력을 캡처하려면 fgets를 사용하고 입력을 구문 분석하려면 sscanf를 사용해보십시오. sscanf가 성공했는지 확인하십시오. 이렇게하면 빈 행을 입력하여 프로그램을 종료 할 수 있습니다. EOF보다 조금 더 편리합니다.

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

#define SIZE 256 

int main() 
{ 
    int account; 
    char name[30]; 
    char input[SIZE]; 
    float balance; 
    FILE *fp; 
    if((fp = fopen("tin", "w")) == NULL) { 
     printf("File could not be opened\n"); 
     exit(1); 
    } 
    else { 
     do { 
      printf("Enter the account, name, and balance.\n"); 
      printf("Enter at ? to end input.\n"); 
      printf("?"); 
      if (fgets (input, SIZE, stdin)) { 
       if ((sscanf (input, "%d%29s%f", &account, name, &balance)) == 3) { 
        printf ("adding input to file\n"); 
        fprintf(fp, "%d %s %2.f\n", account, name, balance); 
       } 
       else { 
        if (input[0] != '\n') { 
         printf ("problem parsing input\nTry again\n"); 
        } 
       } 
      } 
      else { 
       printf ("problem getting input\n"); 
       exit (2); 
      } 
     } while(input[0] != '\n'); 
     fclose(fp); 
    } 
    return 0; 
} 
+0

와우! 그것은 매우 유용한 코드입니다. 조언 해 주셔서 감사합니다. –