2

I want to read strings and write them as full lines into a file, but I can't read more words into a buffer as a complete string.

Current problematic code:

printf("\nEnter how many sentences do you want to read: ");
    scanf("%d", &n);
    tab = (char**)malloc(n * sizeof(char*));
        for (int i = 0; i < n; i++) {
            printf("\nEnter sentence: ");
            scanf("%s", val);
            tab[i] = _strdup(val);
        }
        for (i = 0; i < n; i++)
            fprintf(f, "%s ", tab[i]);
        free(tab);

Previously I tried this: (problem is this only assigns one string)

printf("\nEnter how many sentences do you want to read: ");
    scanf("%d", &n);
    for (i = 0; i < n; i++) {
        printf("\nEnter sentence: ");
        scanf("%s", val);
        fprintf(f, "\%s ", val);
    }

Almost there, now i have sentences but i got one empty line as first line of file.

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string>
#define SIZE 30

void creare(char t[30]);

void main(void)
{
    FILE* f2;
    char name[30];
    printf("\nEnter name of file to work with: ");
    scanf("%s", name);
    creare(name);
    f2 = fopen(name, "r");
    if (f2 == NULL)
    {
        printf("\nOpen error!!");
        exit(0);
    }
    fclose(f2);
    printf("\n");
    _getch();
}


void creare(char t[30])
{
    FILE* f;
    int n,i;
    char val[30];
    f = fopen(t, "w");
    if (f == NULL)
    {
        printf("\nOpen error!!");
        exit(0);
    }
    printf("\nEnter how many sentences do you want to read: ");
    scanf("%d", &n);
    for (i = 0; i <= n; i++)
    {
        fgets(val, sizeof(val), stdin);
        fprintf(f, "% s", val);
    }
    fclose(f);
}
ChrisF
  • 134,786
  • 31
  • 255
  • 325
AndreiD
  • 113
  • 1
  • 11

1 Answers1

2

I have used fgets(val, sizeof val, stdin) to read the string, because to read string with spaces. The reason why that blank line comes in file is due to the fact that you are reading "\n" that is inputted after scanf("%d", &n);The keystroke "\n" will be read into val for the first time so it simply prints that "\n" to the file.

In order to read that "\n" use a character to read that "\n". Below is the complete program.

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#define SIZE 30

void creare(char t[30]);

int main(void)
{
    FILE* f2;
    char name[30];
    printf("\nEnter name of file to work with: ");
    scanf("%s", name);
    creare(name);
    f2 = fopen(name, "r");
    if (f2 == NULL)
    {
        printf("\nOpen error!!");
        exit(0);
    }
    fclose(f2);
    printf("\n");
}


void creare(char t[30])
{
    FILE* f;
    int n,i;
    char val[30],g;
    f = fopen(t, "w");
    if (f == NULL)
    {
        printf("\nOpen error!!");
        exit(0);
    }
    printf("\nEnter how many sentences do you want to read: ");
    scanf("%d", &n);
    scanf("%c",&g);
    for (i = 0; i <n; i++)
    {
        fgets(val, sizeof(val), stdin);
        fprintf(f, "%s", val);
    }
    fclose(f);
}
SRIDHARAN
  • 1,196
  • 1
  • 15
  • 35