Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Program is not appending input taken from user to a text file?

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);
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading