Home > Back-end >  Program is not appending input taken from user to a text file?
Program is not appending input taken from user to a text file?

Time:04-13

Goal was to have a few default text samples written to a new text file, and then take more string inputs from user and append them to same file. But problem occurs in the write_input() function where it doesn't end the loop when enter is pressed nor does it append user input to text file. Maybe a normal char array should've been used as it would maybe been easier with inputs and testing? As of right now still practicing with double pointers and memory usage.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define max_rows 50
#define string_lenght 255

void input_starting_text(FILE *, char **);
void write_input(FILE *);
void free_mem(char **);

int main(void) {
    char *starting_text[] = { "First example of text.",
                                   "Second example of text.",
                                   "Third example of text." };
    const char *location = "textFile.txt";
    FILE *file;
    if ( (file = fopen(location, "w")) == NULL ) return 1;
    input_starting_text(file, starting_text);
    fclose(file);
    if ( (file = fopen(location, "a")) == NULL ) return 1;
    write_input(file);
    fclose(file);
    return 0;
}

void input_starting_text(FILE *file, char **text) {
    for (int i = 0; i < 3; i  )
    {
        if (i == 2)
        {
            fprintf(file, "%s", text[i]);
        }
        else fprintf(file, "%s\n", text[i]);
    }
}

void write_input(FILE *file) {
    char **input = malloc(max_rows * sizeof(char *));
    for (int i = 0; i < max_rows; i  )
    {
        input[i] = malloc(max_rows * sizeof(char));
    }
    int n = 0;
    do
    {
        printf("Enter text for writing to file: ");
        fgets(input[n], string_lenght, stdin);
        fprintf(file, "%s\n", input[n]);
        fflush(stdin);
        n  ;
    } while (input[n] != "\n");
    free_mem(input);
}

void free_mem(char **text) {
    for (int i = 0; i < max_rows; i  )
    {
        free(text[i]);
    }
    free(text);
}

CodePudding user response:

You are flushing your stdin instead of your pointer to the file. Try replacing your function :

void write_input(FILE *file) {
    char **input = malloc(max_rows * sizeof(char *));
    for (int i = 0; i < max_rows; i  )
    {
        input[i] = malloc(max_rows * sizeof(char));
    }
    int n = 0;
    do
    {
        printf("Enter text for writing to file: ");
        fgets(input[n], string_lenght, stdin);
        fprintf(file, "%s\n", input[n]);
        fflush(file);
        n  ;
    } while (input[n] != "\n");
    free_mem(input);

You can see that now contains fflush(file); instead of stdin.

CodePudding user response:

Solved: Changed the program up a bit, removing do while loop and tested for first charachter in a string for new line, now everything works as expected.

    while(1)
    {
        printf("Enter text for writing to file: ");
        fgets(input[n], string_lenght, stdin);
        if (input[n][0] == '\n')
        {
            break;
        }
        fprintf(file, "%s", input[n]);
        fflush(file);
        n  ;
    }
  • Related