I'm a beginner for programming still a student. I tried to create a .txt file in C to write data, save it and after to read what I have written. And I did, it running smoothly in integer format, but when I tried to use characters (sentences), when I use spaces in my input eg:- Hi, My Name is Dilan.... It's only print Hi
I tried to using different variables in both input and output codes but still getting same result.
This Is Data Writing Code
#include <stdio.h>
#include <stdlib.h>
int main()
{
char txt[400];
FILE *fptr;
fptr = fopen("D:\\program.txt","w");
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
printf(" Start Typing : ");
scanf("%s",&txt);
fprintf(fptr,"%s",&txt);
fclose(fptr);
return 0;
}
This Is Data Reading Code
#include <stdio.h>
#include <stdlib.h>
int main()
{
char txt[400];
FILE *fptr;
if ((fptr = fopen("D:\\program.txt","r")) == NULL){
printf("Error! opening file");
exit(1);
}
fscanf(fptr,"%s", &txt);
printf("You Have Entered :- %s", txt);`enter code here`
fclose(fptr);
return 0;
}
I need when I type "Hi, My Name is Dilan ...." instead of just "Hi" I need full sentence like "Hi, My Name is Dilan ...."