I want to write a line ( including spaces ) to the file in one go with following code:-
//in main
char ch[40];
FILE *p;
char choice;
p=fopen("textfile.txt","w");
printf("%s\n","you are going to write in the first file" );
while (1)
{
gets(ch);// if i use scanf() here the result is same,i.e,frustrating
fputs(ch,p);
printf("%s\n","do you want to write more" );
choice=getche();
if (choice=='n'|| choice=='N')
{
break;
}
}
Result of above code is frustrating to me and is hard to explain.but still i will try. If i enter,say,
"my name is bayant."
and press enter the statment comes to screen is
"do you want to write more"
it is good till now,but when i prees a key other than 'n' or 'N' (as required by the logic of program to write more line) then the message
"do you want to write more"
prints again.Now if i press other key than 'n' or 'N' the same line prints on screen.this procedure is followed and prints the statement
"do you want to write more"
4 times,which is the number of words,i.e, 4 in this case.By following this inflexible procedure i get the desired line on my file but if in response to first time printing of statement
"do you want to write more"
i press 'n' or 'N' then only first word ,i.e, "my" in this case prints on file. so what is the solution to write a complete line on the file in one go?and why gets() and fputs() seems ineffective in this case? thanxxx in advance.