2017-01-01 6 views
-5

ファイル、スペースで区切っfollowingformatでコースの生徒の成績が含まれています(例えば、ファイルは以下の)C、文字列、「grades.txt」という名前のファイル、

First name Last name Midterm Final 
Ali Caliskan 60 40 
Veli Dalgaci 80 10 
Turkan Sevimli 90 50 
Ali Yilmaz 30 70 
Ahmet Koc 50 50 

は計算するプログラムを書きます生徒の全体的な成績を「passed.txt」と「failed.txt」という2つの別々のファイルに書き込みます。中期は学年の40%、最終は60%です。

First name Last name Midterm Final Overall 
passed.txt: 
Turkan Sevimli 90 50 66 
Ali Yilmaz 30 70 54 
Ahmet Koc 50 50 50 
failed.txt: 
Ali Caliskan 60 40 48 
Veli Dalgaci 80 10 38 

私は「grades.txt」を読むことができますが、私はそれらを格納することはできません。合格点は50例の出力ファイルが続くです。これは私のコードです。

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

int main(void) { 
char* string[100]; 
char line[100]; 
char junk[100]; 

FILE *file = fopen("grades.txt", "r"); 
if(!file) { 
    printf("Could not open file. Exiting application. Bye"); 
    return 1; 
} 
while(!feof(file)) { 
    fscanf(file,"%[^ \n\t\r]s",line); //Get text 
    printf("%s\n", line); 
    fscanf(file,"%[ \n\t\r]s",junk); //Remove any 'white space' characters 
    } 
fclose(file); 
} 

#include "stdio.h" 
#include <string.h> 

int main() { 

    FILE *pToFile = fopen("grades.txt","r"); 

    int line = 0; 
    char * pch; 
    char input[512]; 

    while(fgets(input, 512, pToFile)) 
     { 
     line++; 
     printf("%s",input); 
     } 

    printf("\n\nEnd Of Program\n"); 

    fclose(pToFile); 

    return 0; 

} 

私もそれを書いたが、これはTXTラインを分離していないと私はこのコードを続行するには難しいだろうと思いました。

+2

ファイル読み込みループを 'feof()'で制御しないでください。 –

+2

"プログラムがエラーを返す"。何のエラー?具体的にしてください。 – kaylum

+0

どこにファイルに書き込もうとしていて、エラーが出るのですか?あなたはどんなエラーを出していますか? –

答えて

1

私はあなたがStudentためstructを使用することをお勧めします。

typedef struct { 
    char first[25], last[25]; 
    int midterm, final; 
} Student; 

次に、ファイルから読み込んでstructsを入力します。このような機能を持つ行から、structを構築することができます。

int scan_student(Student *e, const char *line); 

ファイルに書き込む関数を使用します。

void write_student(FILE *fp, const char *tag, const Student *e, double score); 

以下の全プログラム解決しよう

#include <stdio.h> 

typedef struct { 
    char first[25], last[25]; 
    int midterm, final; 
} Student; 

void write_student(FILE *fp, const char *tag, const Student *e, double score); 

int scan_student(Student *e, const char *line); 

enum { 
    MAXSTUD = 10 
}; 

int main(void) { 
    char line[4096]; 
    Student stu[MAXSTUD]; 
    FILE *f = fopen("grades.txt", "r"); 
    FILE *pa = fopen("passed.txt", "w"); 
    FILE *fa = fopen("failed.txt", "w"); 
    if (f == NULL || pa == NULL || fa == NULL) { 
     perror("Error"); 
     return 1; 
    } 
    fprintf(fa, "First name Last name Midterm Final Overall\n"); 
    fprintf(pa, "First name Last name Midterm Final Overall\n"); 
    if (fgets(line, sizeof(line), f) == 0) 
     return 1; 
    for (int i = 0; i < MAXSTUD && fgets(line, sizeof(line), f) != 0; i++) { 
     if (scan_student(&stu[i], line) == 0) { 
      if (stu[i].midterm * 0.4 + stu[i].final * 0.6 >= 50) { 
       write_student(pa, "Student", &stu[i], stu[i].midterm * 0.4 + stu[i].final * 0.6); 
      } else { 
       write_student(fa, "Student", &stu[i], stu[i].midterm * 0.4 + stu[i].final * 0.6); 
      } 
     } 
    } 
    fclose(pa); 
    fclose(fa); 
    fclose(f); 
    return 0; 
} 

int scan_student(Student *e, const char *line) { 
    if (sscanf(line, "%24s %24s %d %d", 
       e->first, e->last, &e->midterm, &e->final) != 4) 
     return -1; 

    return 0; 
} 

void write_student(FILE *fp, const char *tag, const Student *e, double score) { 
    fprintf(fp, "%s %s %2d %2d %2d\n", 
      e->first, e->last, e->midterm, e->final, (int) score); 
} 

テスト

$ cat grades.txt 
    First name Last name Midterm Final 
    Ali Caliskan 60 40 
    Veli Dalgaci 80 10 
    Turkan Sevimli 90 50 
    Ali Yilmaz 30 70 
    Ahmet Koc 50 50⏎                 
    $./a.out 
    $ cat passed.txt ;cat failed.txt 
    First name Last name Midterm Final Overall 
    Turkan Sevimli 90 50 66 
    Ali Yilmaz 30 70 54 
    Ahmet Koc 50 50 50 
    First name Last name Midterm Final Overall 
    Ali Caliskan 60 40 48 
    Veli Dalgaci 80 10 38 
+0

ありがとう^^、このコードでは "Veli Dalgaci 80 10 38"を取得できません。どうして? :S – Enigma

-1

#include <stdio.h> 
 

 
typedef struct { 
 
    char first[25], last[25]; 
 
    int midterm; 
 
    int finall; 
 
} Student; 
 

 
void write_student(FILE *fp, const char *tag, const Student *e, double score); 
 

 
int scan_student(Student *e, const char *line); 
 

 
int main(void) { 
 
    char line[4096]; 
 
    FILE *f = fopen("grades.txt", "r"); 
 
    FILE *pa = fopen("passed.txt", "w"); 
 
    FILE *fa = fopen("failed.txt", "w"); 
 
    fprintf(fa, "First name Last name Midterm Final Overall\n"); 
 
    fprintf(pa, "First name Last name Midterm Final Overall\n"); 
 
    
 
    while (fgets(line, sizeof(line), f) != 0) { 
 
    Student stu; 
 
    if (scan_student(&stu, line) != 0) continue; 
 
    
 
    if (stu.midterm * 0.4 + stu.finall * 0.6 >= 50) { 
 
     write_student(pa, "Student", &stu, stu.midterm * 0.4 + stu.finall * 0.6); 
 
    } else { 
 
     write_student(fa, "Student", &stu, stu.midterm * 0.4 + stu.finall * 0.6); 
 
    } 
 
    } 
 
    
 
    fclose(pa); 
 
    fclose(fa); 
 
    fclose(f); 
 

 
    return 0; 
 
} 
 

 
int scan_student(Student *e, const char *line) { 
 
    if (sscanf(line, "%s %s %d %d", 
 
      e->first, e->last, &e->midterm, &e->finall) != 4) 
 
    return -1; 
 

 
    return 0; 
 
} 
 

 
void write_student(FILE *fp, const char *tag, const Student *e, double score) { 
 
    fprintf(fp, "%s %s %d %d %d\n", 
 
      e->first, e->last, e->midterm, e->finall, (int) score); 
 
}

+0

フォーマットが間違っていて、UBでの回答は良い答えではありません。特にコードだけがある場合。たとえば、 'fopen()'の返り値を確認しません。 – Stargateur

関連する問題