控制台应用菜单无法在C中工作

时间:2016-12-14 12:04:37

标签: c visual-studio console-application

家伙!所以我有一个任务来创建一个结构,其中包含基于我需要存储的信息的变量,并带出一个调用不同函数和执行不同操作的菜单。问题在于,由于某些原因,我的createFile功能根本无法工作,而且我一直在寻找错误2天,而我根本无法找到它的来源。

忽略changeStud功能,因为我还在努力。我只想在输入名称时创建我的文件,然后选择函数的数字。 (在我的情况下>输入文件名>输入1>它只是循环菜单功能,它应该创建一个文件)

#define _CRT_SECURE_NO_WARNINGS
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Student {
    char ime[50];
    char fn[10];
    int ocfiz, ocmat, ocpro;
};
char filename[20];

FILE *fd;

Student ps;

void createFile() {
    fd = fopen(filename, "wb");
    fclose(fd);
    printf("File created!\n");
}

void readStud(Student *s) {
    printf("Enter data for new student");
    getchar();
    printf("Enter student name:"); 
    gets_s(s->ime);
    printf("\nEnter FN:");
    scanf_s("%s", s->fn);
    printf("\nEnter marks for Maths, Programming, Physics");
    scanf_s("%d %d %d", &s->ocmat, &s->ocpro, &s->ocfiz);
    getchar(); 
}
void addStud() {
    fd = fopen(filename, "a+b");
    char c;
    do {
        readStud(&ps);
        fwrite(&ps, sizeof(ps), 1, fd);
        printf("More Students? (y/n) : ");
        c = getchar(); getchar();
    } while (c == 'y');
    fclose(fd);
}

void writeAllStud() {
        fd = fopen(filename, "rb");
        printf("students in file\n");
        fread(&ps, sizeof(ps), 1, fd);
        while (!feof(fd)) {
            printf("%s fn: %s ocmat: %d ocpro: %d ocfiz: %d", ps.ime, ps.fn, ps.ocmat, ps.ocpro, ps.ocfiz);
            fread(&ps, sizeof(ps), 1, fd);
        }
        fclose(fd);
}/*
void changeStud() {
    char fn2[50];
    printf("enter FN:");
    gets_s(fn2);
    fd = fopen(filename, "r+b");
    fread(&ps, sizeof(ps), 1, fd);
    while (!feof(fd)) {
        if (strcmp(ps.fn, fn2)==0) {

            fseek(fd, -(long) sizeof(ps), SEEK_CUR);
            fwrite(&ps, sizeof(ps, 1, fd));
            break;
        }
    }
}*/

void Avg() {

}

void exportData() {
    FILE *txt;
    txt = fopen("students.txt", "wt");
    fd = fopen(filename, "rb");
    fread(&ps, sizeof(ps), 1, fd);
    while (!feof(fd)) {
        fprintf(txt, "%s %s marks fiz%d mat%d prog%d\n", ps.fn, ps.ime, ps.ocfiz, ps.ocmat, ps.ocpro);
        fread(&ps, sizeof(ps), 1, fd);
    }
    fclose(fd);
    fclose(txt);
    printf("students have been written to a text file.\n");
}
void main() {
    printf("Enter file name:");
    gets_s(filename);
    int c;
    do {
        printf("\nMenu:\n");
        printf("0 Exit\n");
        printf("1 Create file\n");
        printf("2 Add new student data\n");
        printf("3 Change data from FN\n");
        printf("4 AVG marks for maths, programming, physics\n");
        printf("5 export all AVG marks to .txt file\n");
        scanf("%d", &c);
        switch (c) {
        case 1: createFile; break;
        case 2: addStud; break;
        //case 3: changeStud; break;
        case 4: Avg; break;
        case 5: exportData; break;
        }
    } while (c != 0);
}

1 个答案:

答案 0 :(得分:1)

我认为你应该像这样使用你的struct变量:

struct Student {
char ime[50];
char fn[10];
int ocfiz, ocmat, ocpro;
}ps;

或使用

struct Student ps;

而不仅仅是学生ps,或者你可以在main函数中取消它。并且在一个functin中传递一个结构是

void readStud( struct Student *s)() {
//your code...
}