【问题标题】:C, String, FileC、字符串、文件
【发布时间】:2017-01-01 23:39:48
【问题描述】:

名为“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”。期中成绩占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 行,我认为继续使用这段代码会更难。

【问题讨论】:

  • 不要用feof()控制文件读取循环。
  • “程序出错”。什么错误?请具体。
  • 你在哪里尝试写入文件,你会得到一个错误?你遇到了什么错误?
  • 我编译成功了,当我运行它就停止工作了
  • strcat(line,str1[100][i]); 这是什么意思?由于多种原因,这是错误的。 1. str1 从未被初始化过。 2. str1[100] 是缓冲区溢出,因为100 不是该数组的有效索引。 3. str1[100][i] 是 char 而不是 char * 所要求的 strcat。

标签: c string file


【解决方案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

【讨论】:

  • 非常感谢 ^^,但我无法使用此代码获得“Veli Dalgaci 80 10 38”。为什么? :S
【解决方案2】:

#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);
}

已解决。

【讨论】:

  • 格式错误,用 UB 回答,不好回答。特别是当只有代码时。例如,您不验证 fopen() 的返回。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-20
  • 2013-03-29
  • 2020-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多