1

Some backstory
I'm an engineering student (first year) and we had to create a program that could manage a list of students. Basically putting information of students in an array of structs (student name, birth date, grades). It had to be possible to add students, delete students, sort students, change information, add grades, save progress, load progress and generate a mark list(html/css).

My problem
I wanted to add settings to my project. I put some information in a file I called settings.txt. The first and second line are integers and can be 1 or 0. The third line is a string. Creating this file and reading from the file works fine, but of course I wanted the user to be able to change his settings too. However, when I run the code below, my .exe crashes. It's probably because of something stupid I'm doing but I've been looking at this for a couple of hours now and my 'fixes' make things worse. I hope someone can help me, thanks in advance! :)
I tried adding some comments in english, originally the outputs in printf were dutch. If anything is unclear, please ask!

Code

char getBool (char option[], char booly[]) {
    if (option[0]!='0') {
        strcpy(booly,"true");
    } else {
        strcpy(booly,"false");
    }
}

char editSettings (char settings[], char startLoad[]) {
    //This function will allow the user to edit some settings
    //Declare variables: file: filename, lijn: storage for xth line of file,booly: true/false output. Settings:reset: 1/0,load: 1/0,startfile: filename
    char file[15]="settings.txt",lijn[25],booly[10],reset,load,startfile[25];
    int inp,i;
    do {
        i=1;
        //Read file and output current settings to user
        FILE * bestand;
        bestand=fopen(file,"rt");
        fgets(lijn,999,bestand);
        while (!feof(bestand)) {
            //i determines what line we're on -> There are always 3 lines
            //1st line can be 0 or 1 -> Reset screen after cmd?
            //2nd line can be 0 or 1 -> Load file
            //3th line is a string (filename) -> .txt is automatically added when loading file
            getBool(lijn,booly);
            if (i==1) {
                printf("%d - Reset screen after every command: %s\n",i,booly);
                reset=lijn;
            } else if (i==2) {
                printf("%d - Load file on startup: %s\n",i,booly);
                load=lijn;
            } else if (i==3) {
                strcpy(startfile,lijn);
            }
            fgets(lijn,999,bestand);
            i++;
        }
        fclose(bestand);

        printf("Pick a setting to change or enter 0 to cancel.\nChoose: ");
        //Let user choose a value: 0, 1 or 2. Anything else won't
        inp=inlezen("012");
        printf("\n");

        //Process users choice
        if (inp=='1') {
            //Toggle reset option, remain other options
            bestand=fopen(file,"wt");
            if (reset=='0') {
                reset='1';
            } else if(reset=='1') {
                reset='0';
            }
            fprintf(bestand,"%s\n",reset);
            fprintf(bestand,"%s\n",load);
            fprintf(bestand,"%s\n",startfile);
            fclose(bestand);
        } else if (inp=='2') {
            //Toggle load on startup option + read filename to start, remain reset option
            bestand=fopen(file,"wt");
            if (load=='0') {
                load='1';
            } else if(load=='1') {
                load='0';
            }
            fprintf(bestand,"%c\n",reset);
            fprintf(bestand,"%c\n",load);
            if (load=='1') {
                printf("\nWhich file must be loaded on startup?\nGive name: ");
                scanf("%s",&startfile);
            }
            fprintf(bestand,"%s\n",startfile);
            fclose(bestand);
        }
    } while (inp!='0');
    printf("\n");
}
Vallas
  • 510
  • 1
  • 6
  • 21
  • 2
    `fgets(lijn,999,bestand)` --> `fgets(lijn, 25,bestand)` ----> best `fgets(lijn, sizeof(lijn),bestand)` – LPs Dec 20 '16 at 10:53
  • 2
    [why-is-while-feof-file-always-wrong](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – LPs Dec 20 '16 at 10:54
  • 2
    LPs is right, you declared `lijn` to be only 25 bytes long. You must have a single line longer than 23 bytes in your text file – please check that. (23, not 25, because `fgets` wants space for the return and the closing zero.) – Jongware Dec 20 '16 at 10:56
  • 3
    When programming, I recommend that you stick to English for everything, including variable and function names. Even for Dutch people, it is a bit weird to see Dutch names amidst English keywords and English comments. And as soon as you publish your code or have others contribute to your code, it will become an unnecessary barrier. – G. Sliepen Dec 20 '16 at 10:59
  • 1
    Just a question - have you checked `libconfig`? – Andrejs Cainikovs Dec 20 '16 at 10:59
  • 1
    Thanks for your quick responses! LPs, thanks for pointing those out. I changed fgets right away. However I'm going to stick to feof this time since thats wat our professor told us to use but I'll keep it in mind next time! Rad Lexus, I increased the length but the longest line has 7 characters on it. G. Sliepen: I agree and I will from now on, thank you. Andrejs Cainikovs, I have not but I never had to so I don't know what it does. – Vallas Dec 20 '16 at 11:40
  • 1
    @Vallas Well, `libconfig` is a cross-platform C/C++ library, that deals with config files, it's quite advanced and simple to use at the same time. I see that you mentioned that this is your study work, so I think you should go, of course, with your implementation. But when you'll become professional, there are bunch of handy libraries out there, so you don't have to reinvent the wheel. – Andrejs Cainikovs Dec 20 '16 at 12:07
  • @Andrejs Cainikovs, Oh I see. I'll definitely take a look then. Thank you for your good explanation! – Vallas Dec 20 '16 at 12:08

1 Answers1

0

However, when I run the code below, my .exe crashes.

You define char …reset,load, and then you pass those characters to fprintf() with the wrong conversion specifier s:

            fprintf(bestand,"%s\n",reset);
            fprintf(bestand,"%s\n",load);

If your compiler didn't warn about inconsistent usage of those variables, you should use a better compiler; if it did, you should pay attention to the warnings.

Armali
  • 18,255
  • 14
  • 57
  • 171
  • Thank you for your answer. I don't remember if I got any warnings back then (it's been two years haha). But back then, I had only been writing C for a couple of months so rookie mistakes are bound to happen. – Vallas Jan 04 '19 at 19:29