C、字符串、文件

标签 c string file

名为“grades.txt”的文件包含类(class)的学生成绩,格式如下,以空格分隔:(示例文件如下)

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行,我认为继续这个代码会更难。

最佳答案

我建议您对 Student 使用 struct

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

然后从文件中读取并填充您的结构。您可以使用这样的函数从一行构建您的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

关于C、字符串、文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41419992/

相关文章:

c - 在链表中输入元素时进行迭代

c++ - 是否可以使用 malloc 来增加现有数组的大小?

objective-c - 将 Objective C 转换为 C 矩阵操作

java - 在java中查找括号字符串的有效性

java - 重命名文件并返回新文件指针

c# - "File in use"写入文本文件时出错

c - 为什么 __libc_start_main 的第三个参数名称为 ubp_av 而不是 argv?

matlab中垂直矩阵中的字符串串联

xml 的 Javascript 编码

java - 检测带有 ID3 标签的重复 MP3 文件吗?