2017-01-01 33 views
-5

的文件,名为“grades.txt”,包含了学生成绩在用空格分隔followingformat课程:(示例文件如下)C,字符串,文件

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”。中期成绩是40%,最终成绩是60%。通过等级为50例输出文件如下:

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”,但我不能保存。这是我的代码;

#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

我建议您使用struct作为Student

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