创建文件或转到该文件的第n行

时间:2012-02-19 08:47:32

标签: c

我一直在研究这个C编程任务,我似乎无法找出为什么它不像我期望的那样表现。该程序应该运行,命令0-nn有3种可能的选项,其中n是正整数。

当我执行程序时,键入0作为命令(如果尚未创建文件,则应创建一个文件),它只是循环返回,要求我输入命令。

命令-nn转到n指定的行并寻求它。 n打印第n行,而-n打印从n开始的所有行,直到它无法再从文本文件中读取。

如果有人能给我一两个暗示并引导我朝着正确的方向前进,我将非常感激。

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

#define BUFSIZE 256
#define LINESIZE 1024

int write(FILE *fp);
int grade_validation(int grade);
int id_validation(char studentid[]);
int display(FILE *fp, int cmd);
int display_all(FILE *fp, int cmd);

int main(int argc, char *argv[]) {
    if(argc != 2) {
        perror("invalid number of args");
        return 1;
    } else {
        FILE *fp;
        if((fp = fopen(argv[1], "wb+")) == 0) {
            perror("fopen");
            return 1;
        }
        write(fp);
        fclose(fp);
    }
    return 0;
}

int write(FILE* fp) {
    int grade;
    char studentid[BUFSIZE];
    char input[BUFSIZE];
    int cmd;
    while(1) {
        printf("Input 0 to append, n to view, or -n to view all records starting from n.\n");
        if(!fgets(input, LINESIZE, stdin)) {
            clearerr(stdin);
            return 0;
        }
        if((sscanf(input, "%d", &cmd) == 1)) {
            if(cmd == 0) {
                if((id_validation(studentid)) != 1 || (grade_validation(&grade) == -1)) {
                    continue;
                }
                fprintf(fp, "%s %3d ", studentid, grade);
            } else if(cmd > 0) {
                display(fp, cmd);
            } else if(cmd < 0) {
                display_all(fp, cmd);
            }
        }
    }
}   

int grade_validation(int grade) {
    char input[BUFSIZE];
    FILE *fp;
    if(grade >= 0 && grade <= 100) {
        return 1;
    } 
    if(sscanf(input, "%d", &grade) == 1) {
        if((grade_validation(grade)) == 1) {
            if(fprintf(fp, "%3d", grade) == 0) {
                return 0;
            }
            printf("Grade recorded successfully.\n");
            return 1;
        }
    }
    return 0;
}

int id_validation(char studentid[]) {
    char input[BUFSIZE];
    FILE *fp;
    size_t i = 0;
    if(strlen(studentid) == 9) {
        for(i = 0; i < 9; i++) {
            if(isdigit(studentid[i])) {
                return 1;
            }
        }
    }
    if(sscanf(input, "%s", studentid) == 1) {
        if((id_validation(studentid)) == 1) {
            if(fprintf(fp, "%s", studentid) == 0) {
                return 0;
            }
            printf("Student ID recorded successfully.\n");
            return 1;
        }
    }
    return 0;
}

int display(FILE *fp, int cmd) {
    char studentid[BUFSIZE];
    int grade;
    if(!fgets(cmd, BUFSIZE, fp)) {
        clearerr(stdin);
        return 0;
    }
    fseek(fp, cmd, SEEK_SET);
    fprintf(stderr, "%s %3d", studentid, grade);
}

int display_all(FILE *fp, int cmd) {
    char studentid[BUFSIZE];
    int grade;
    if(!fgets(cmd, BUFSIZE, fp)) {
        clearerr(stdin);
        return 0;
    }
    fseek(fp, cmd, SEEK_SET);
    while((sscanf(studentid, grade, "%s %3d", cmd)) != EOF) {
        fprintf(stderr, "%s %3d", studentid, grade);
    }
}

1 个答案:

答案 0 :(得分:2)

您正在尝试验证未初始化的studentidgrade

此外,您将&grade传递给grade_validation,期望和整数 这会引起警告,这会让你弄清楚出了什么问题。始终在启用警告的情况下进行编译,并将其视为错误(在gcc,-Wall -Werror中)。