-1

Every time I input a string into my text file I want a space in between the strings inputted like this:

Input:

Stack

Overflow

Expected Output:

Stack Overflow

Current output:

StackOverflow

This is my function code:

void addDataToTextFile(FILE *fileptr)
{
    char text[100];
 
    fileptr = fopen("textfile.txt", "a");
    printf("\n\nPlease enter some text: ");
    fflush(stdin);
    gets(text);
    fprintf(fileptr, text);
    fclose(fileptr);
    printf("\n\nText has been added");
    getch();
    return;
}
Community
  • 1
  • 1

3 Answers3

3

gets is deprecated, use fgets and replace the trailing newline with a white space:

void addDataToTextFile(FILE *fileptr)
{
    char text[100];
    char *ptr;

    fileptr = fopen("textfile.txt", "a");
    printf("\n\nPlease enter some text: ");
    fflush(stdin); /* You want fflush(stdout); */
    fgets(text, sizeof text, stdin);
    if ((ptr = strchr(text, '\n'))
        *ptr = ' ';
    fprintf(fileptr, text); /* Wrong, use fprintf(fileptr, "%s", text); */
    fclose(fileptr);
    printf("\n\nText has been added");
    getch();
    return;
}
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
1

To start with

  1. always check the return value of fopen() before using the returned pointer.
  2. fflush(stdin); produces undefined behaviour, is buggy and useless.##
  3. gets() IS DANGEROUS. use fgets() instead.
  4. fprintf(fileptr, text); wrong usage.

Note: fgets() scans and stores the tralining \n (from ENTER key press) from the input. After correcting above issues, to add a space after each input string, you can search and replace the traling \n with a and then write the string to file. This is mentioned nicely in Mr. Alter Mann's answer below / above.


##: As per C11 standard, chapter 7.21.5.2, paragraph 2,

int fflush(FILE *stream);

If stream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined.

So, stdin which is not an outpust stream, will cause UB when used with fflush().

Some good reads : this and this

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
-1

FILE pointer was not declared fprintf was missing the second argument

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
        FILE *fileptr;
     char text[100];

        fileptr = fopen("textfile.txt", "a");
        printf("\n\nPlease enter some text: ");
        fflush(stdin);
        gets(text);
        printf("INPUT = %s\n",text);
        fprintf(fileptr,"%s ", text);
        fclose(fileptr);
        printf("\n\nText has been added");
}
George
  • 1,330
  • 1
  • 9
  • 12
  • 1
    How did you come to the conclusion `FILE pointer was not declared`? I think it is there in function paramater, isn't it? Also, please don;t encourage using `gets()`, it's really bad, AFAIK. – Sourav Ghosh Mar 19 '15 at 07:41