I saw few similar problems in this website and tried to follow those methods but either those methods don't work properly or I couldn't understand properly.
I am having trouble with saving strings with spaces into file and recalling them.
#include<stdio.h>
int main()
{
char a[200];
char b[200];
char c[200];
char bug[100];
int s;
FILE *fp;
printf("Press 1 to add\nPress 2 to show\n");
scanf("%d",&s);
if(s==1)
goto level1;
else
goto level2;
level1:
gets(bug);
printf("Name: ");
gets(a);
printf("Address: ");
gets(b);
printf("Comment: ");
gets(c);
fp=fopen("practice2.txt", "a+");
fprintf(fp, "\n%s\t%s\t%s\t",a,b,c);
fclose(fp);
printf("\n1 for add\n2 for show\n");
scanf("%d",&s);
if(s==1)
goto level1;
else
goto level2;
level2:
fp=fopen("practice2.txt", "r");
if(fp==0)
printf("\nEmpty\n");
else
{
while(!feof(fp))
{
fscanf(fp, "\n%s\t%s\t%s\t",&a,&b,&c);
printf("%s\n%s\n%s\n\n",a,b,c);
}
}
fclose(fp);
return 0;
}
This code works if I input strings with no spaces like "Hello_people". But if I input strings with spaces like "Hello people", then it doesn't show proper output. I am working on a project in c programming and got stuck with this problem.
Btw, in line number 17, I used gets(bug). Because I found out that if i use gets() after scanf(), then it doesn't take the first gets() input. So I tried using an extra gets() after scanf() and then it works.
I am a very beginner in programming. Please can anyone help me by fixing my code to make it work perfectly?
Additional info:
If I input:
Name: Shane Watson
Address: Australia
Comment: I like him
Then I expect it to show the result exactly this way.