-1

guys! So I have an assignment to create a structure that includes variables based on what information I need to store and to bring out a menu that calls different functions and does different things. The problems is that for some reason my createFile function is not working at all and I've been looking for errors for 2 days and I simply cannot spot where this is coming from.

Ignore the changeStud funcion as I'm still working on it. I simply want to create my file when I enter the name for it and then chose the nnumber of the function. (in my case > enter filename> enter 1> it just loops the menu function and it should create a file)

#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);
}
Angel Vasilev
  • 77
  • 1
  • 9
  • 1
    "my `createFile` function is not working" - what exactly is not working in it??? – barak manos Dec 14 '16 at 12:07
  • 1
    On a semi-related note: don't user `void main`. It's `int main` – UnholySheep Dec 14 '16 at 12:09
  • That's exactly what I cannot figure out. Code-wise it's correct. Here is input I tried. After I enter the name and and then enter "1" it should display "File created" and I should see I file in the folder of the solution. Problem is it just loops. Same for the other funcions when I call them. Here is an example - http://i.imgur.com/zl4UkzR.png – Angel Vasilev Dec 14 '16 at 12:09
  • 1
    `case 1: createFile; break;` that's not how you call a function - it should be `case 1: createFile(); break;`: NOTE the parentheses after the function name – UnholySheep Dec 14 '16 at 12:10
  • @UnholySheep our professor requires it's void. – Angel Vasilev Dec 14 '16 at 12:11
  • @UnholySheep Oh my god.... THANK YOU! I really feel like an idiot. Have a nice day dude! – Angel Vasilev Dec 14 '16 at 12:12
  • 1
    Please also tell your professor that `void main` is not standard compliant and as such not portable. The standard defines `int main` as the required function signature (see [this answer](http://stackoverflow.com/a/2108208/2878796) for more details) – UnholySheep Dec 14 '16 at 12:13
  • @UnholySheep If you see our lectures, you'll be shocked. It's old-school C. They're working on Visual studio 2006 and when we use free community version of 2015 with c++14 standards we have to use scanf_s and such which makes it really uncomfortable. Errors occurs quite often and what not. – Angel Vasilev Dec 14 '16 at 12:15
  • 1) m`void main()` is invalid->undefined behaviour 2) `scanf_s` usage wrong. Read the docs of all functions you use! -> undefined behaviour. Not checking for function resuls, namely `fopen`, but others too -> problems or even undefined behaviour. 4) Usage of `feof` wrong->undefined behaviour. Fix the code, read the documentation. Enable warnings and don't ignore them! And see [ask]. We are not a debugging service. – too honest for this site Dec 14 '16 at 12:23

1 Answers1

1

i think you should use your struct variable like this:

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

or use

struct Student ps;

instead of just student ps,or you can decleare it in main function..And passing a struct in a functin is

void readStud( struct Student *s)() {
//your code...
}
Hira
  • 61
  • 5
  • 1
    @UnholySheep already found my problem. My code is floppy but the problem was that I didn't call for my funcions properly. I forgot "()" in my main function when I want to call them. Still I'll take what you said and fix it. Thanks a lot :) – Angel Vasilev Dec 14 '16 at 12:26