Page 1 of 1

scanf, and gets problems

PostPosted: Sat Dec 31, 2011 9:17 pm
by silvia
My program is:
/****************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void main()
{
char str[80];
FILE *fp;

if((fp = fopen("Taxi Information.txt", "w"))==NULL)
{
printf("Cannot open file.\n"); exit(1);
}
do {
printf("Enter a string (CR to quit):\n");
scanf_s("%s", str); // Type slivia todorof
gets(str);
strcat(str, "\n");
} while(*str!='\n');
}
/****************************************/
If I type: silvia todorof , I have two problems:
1st problem: After typing "silvia todorof", appears "Enter a string (CR to quit)", as required, but if I hit Enter it did not quit and stuck there; but it goes out if I write ^z with two times Enter.
2nd problem: the text file is empty.

Thanks
Silvia

Re: scanf, and gets problems

PostPosted: Sat Dec 31, 2011 9:47 pm
by Robert Sample
You might want to display in hex the data you're passing to str -- I suspect you'll find that the enter is an actual character and must be recognized by your routine.

The file is empty since you never wrote anything into it.