Home > Mobile >  fgets() is not being executed
fgets() is not being executed

Time:06-02

I've been trying to write a program that lets a user open a .txt file in read, write or append mode and then edit its contents.

So far when I run the program, it's capable of opening a file in any selected mode, however the bit of code in the last if-statement doesn't seem to get executed. I've tried a few things and haven't been able to get it working. The fopen() function seems to work correctly, but the bit of code after it is not being run.

Is anyone able to tell me why it's not being executed?

I'm still quite new to coding and C, so I suspect that there may be something I'm not understanding either about the language or computer systems. I appologise in advance for not noticing any obvious mistakes in my code and logic. I'm grateful for all help and it would be seriously appreciated.

Here is the code:

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "bsp10048.h"
#include <string.h>
#define MAX_EINGABE 80


/*----- Dateiöffner und Editor -----*/


int main(void)
{
    FILE* file_ptr;
    char str[200], selection[2], mode[2], name[200];
    int repeat = 1; 

    printf("\nPlease enter filename: ");
    fgets(name, 200, stdin);

    name[strcspn(name, "\n")] = 0;

    strcat(name, ".txt");

    printf("\n''r'' = Read-mode (Open file for reading)");
    printf("\n''w'' = Write-mode (Create file for writing)");
    printf("\n''a'' = Append-mode (Open file for appending)");
    
    /*MODE SELECTION*/
    while (repeat) {
        printf("\n\nBitte Modus waehlen: ");
        fgets(selection, 2, stdin);
        
        switch (selection[0]) {

        case 'r':
            /*Lesemodus*/
            strcpy(mode, "r");
                printf("\nRead-mode selected\n");
            repeat = 0;
            break;
        
        case 'w':
            /*Schreibemodus*/
            strcpy(mode, "w");
                printf("\nWrite-mode selected\n");
            repeat = 0;
            break;
        
        case 'a':
            /*Appelliermodus*/
            strcpy(mode, "a");
                printf("\nAppend-mode selected\n");
            repeat = 0;
            break;

        default:
            printf("\nInvalid mode!");
            break;
        }
    }

    
    
    if ((file_ptr = fopen(name, mode)) != NULL) {
        printf("File successfully opened!");
    }

    if (mode != "r") {
        fgets(str, 200, stdin);
        while (str[0] != '\n') {
                fprintf(str, 200, file_ptr);
                fgets(str, 200, stdin);
            }
    }

    fclose(file_ptr);
    exit(0);
}

CodePudding user response:

You have multiple problems in your code.

  1. Comparing strings:
if (mode == "r")

You cannot compare a string by comparing with a string literal. This will compare addresses and very likely never be same.

That condition will always be true as both strings cannot have same address.

You might revisit your learning material and check how strings are handled correctly.

Use strcmp instead:

if (strcmp(mode,"r") != 0)
  1. Writing your output:
fprintf(str, 200, file_ptr);

As ShadowRanger already mentioned in comments, this is wrong. You should get a few warnings about that line. You messed up fprintf parameters with fwrite.

Use this instead:

fprintf(file_ptr,"%s\n", str);
  1. Use a debugger to watch what your program does.

You claim that the fgets in that if part is never executed. That is actually not true.

You have this line in your code:

fgets(selection, 2, stdin);

That will read only 1 byte from stdin and use second byte for terminating 0 byte. Any other character, including \n is left in the buffer.

If you then come here:

        fgets(str, 200, stdin);
        while (str[0] != '\n') {

You will read only a single \n into str and your while condition will immediately be false.

You should provide enough space to consume your \n in the fgets call above.

On a sidenote: If that condition wasn't false already on first iteration, you would probable face a segmentation fault due to passing invalid parameters to fprintf as mentioned earlier.

CodePudding user response:

The problem is with the if statement

if (mode != "r") {
    fgets(str, 200, stdin);
    while (str[0] != '\n') {
            fprintf(str, 200, file_ptr);
            fgets(str, 200, stdin);
        }
}

In C you don't compare strings like that, you use strcmp

if (strcmp(mode,"r") == 0) {
    fgets(str, 200, stdin);
    while (str[0] != '\n') {
            fprintf(str, 200, file_ptr);
            fgets(str, 200, stdin);
        }
}

Writing

if (mode == "r")

you are comparing the address of the array mode with the address where the string literal "r" is, which will always be different.

  • Related