Reputation: 2183
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.
Upvotes: 0
Views: 188
Reputation: 901
Do something like this, its a very crude program but should give you an idea
your mistakes, you had only made a pointer to a char in your program, you need to allocate memory for the pointer using malloc, or the other option is just to make an array of chars. which I have done.
#include <stdio.h>
#include <stdlib.h>
int main(void){
char ch[100];
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
int c =0;
fgets(ch,100,stdin);
fputs(ch,p);
printf("%s\n","do you want to write more" );
choice=getchar();
if (choice=='n'|| choice=='N')
{
break;
}
while ((c = getchar()) != '\n' && c != EOF);
}
return 0;
}
you program was repeating the printf("%s\n","do you want to write more" );
because the input buffer had a \n written to it, you need to clear the buffer before reading. this line removes the newline from buffer while ((c = getchar()) != '\n' && c != EOF);
check this scanf() leaves the new line char in buffer?
Upvotes: 2
Reputation: 942
If you use
scanf("%s",ch);
(Which I assume is what you mean by "scanf"), this will read one string. If you type
"my name is bayant."
This will result in 4 strings: "my", "name", "is" and "bayant.".
Notice that, from your description, you don't want to read strings, you want to read lines. To read a whole line of text with scanf, you could use:
scanf("%[^\n]", ch);
scanf("%*c");
Which means: Line 1: "read everything until you find a \n character".
Line 2: "read and ignore that '\n' character (which was left in the buffer)".
I should say that this is not a secure solution, as the user can easily overflow the "ch" buffer, but I'm sure you can find better ways to do that if this is a real concern in your particular case.
Upvotes: 2