Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

scanf, gets and fflush 2

Status
Not open for further replies.

silviatodorof

Programmer
Dec 31, 2011
2
This is a portion of a program:
/******************************************************/
if((fp = fopen("Taxi Information.txt", "w"))==NULL) {
printf("Cannot open file.\n");
exit(1);
}
do {
printf("Please enter the car name(CR to quit):\n");
scanf_s("%s",carname);
fflush(stdin); gets(carname);
strcat(carname, "\n");/*add a newline */
fputs(carname,fp);
} while(*carname!='\n');
/******************************************************/

I had a problem with suing scanf_s and gets together. The problem was: I was stuck with the carname and cann't get out of it.
I looked for a solution in the net and found out that using fflush between scanf and get can solve the problem, but this created a new problem. The problem is that fflush caused the text file to be empty.
Thanks
 
You either use scanf_s or gets: not both. They use different buffers. fflush is used for clearing output buffers. stdin is an input buffer.
Code:
do {
   printf ("Please enter the car name (CR to quit):\n");
   /* Make sure everything is printed first */
   fflush (stdout);
   /* get the car name */
   gets(carname);
   strcat (carname, "\n");
   fputs (carname, fp);
} while (*carname != '\n');

 
Thank you for your reply. It is very useful. But the real code is bigger than that and I have to use scanf in the begining:
/*********************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX 100

void print_menu(int n);

void main()
{
int c, a[MAX],temp[MAX],reg,top = 0;
char carname[80];
FILE *fp;

print_menu(1);

scanf_s("%d", &c);
while (c != 2)
{
if (c == 1)
{
// Scan in registration
printf("Please enter Taxi Registration Number\n");
scanf("%d", &reg);
a[top] = reg;
top++;

if((fp = fopen("Taxi Information.txt", "w"))==NULL) {
printf("Cannot open file.\n");
exit(1);
}
do {
printf ("Please enter the car name (CR to quit):\n");
fflush (stdout);
/* get the car name */
gets(carname);
strcat (carname, "\n");
fputs (carname, fp);
} while (*carname != '\n');

fclose(fp);
// Call print_menu
print_menu(1);
scanf("%d", &c);
}
else if (c == 2)
{
//remove from front
int b;

for(b = 0; b <= top; b++)
{
temp = a[b+1];
}

for (b = 0; b <= top; b++)
{
a = temp;
}

top--;
printf("Taxi has left\n");
//print menu
print_menu(1);
scanf("%d", &c);
}
}
}
// Print menu
void print_menu(int n)
{
printf(" 1. Arrive \n");
printf(" 2. Leave \n");
}
/*************************************************************/

Thanks
 
If you have to interleave scanf and gets, it might be better if you use gets to read the input and sscanf or sscanf_s to extract the data from the string.

This is very compiler dependent. On some compilers, gets and scanf read from the same buffer, on others they are from different buffers. You may find the same problem with printf and puts. The text can appear out of sequence when they are mixed.
 
You should use fgets instead of gets, fgets is safe, not gets.
 
Sorry? some additions:

1. Both scanf and gets read data from the same input stream stdin. Both are not bear a relation to buffering or compiler dependencies.

2. The gets function read a (rest of) line including '\n' character.

3. The scanf read characters upto the first whitespace only. So you can't enter such car names as Crown Victoria. Why?

4. The first while loop in your main is controlled by uninitialized variable c (undefined behavior). Apropos, int main() but never void main...

5. Better use return 1 instead of exit(1) on error in main body.

6. See joel76's post: never, ever use gets in your codes!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top