-
clearing the buffer??
Not sure if that it what needs to happen, but heres what is happening and needs to not happen... hah. OK so I am using fgets() to capture spaces as well as text. But I am guessing the buffer isn't clear so instead of letting me type and it take from the stdin. There was something there and it auto chose it. So I guess what I am asking is how do I clear the buffer. If you want to see the code just ask, or if you have questions just ask, I'm happy to answer them.
-
A forum search will tell you how to do it, but I would suggest you find out what's actually in the stream, what put it there, and correct the source of the problem rather than the symptom.
-
ok ya I know what is in there and it is in the same program so I think it is just to be there, but I just need to get it out.
-
Well, I found fflush() but it didn't actually work, here is the code with the problem, but before what happens is, when I am "getting" from stdin it automatically puts in ".app" what is going on?!?!
Code:
char empty1[25];
char empty2[55];
char applic[15];
char applic2[18];
printf("Type in the name of the app\n");
fflush(stdin);
fgets(applic, 15, stdin);
sprintf(applic2, "\"%s\"", applic);
sprintf(empty1, "open -a %s", applic2);
sprintf(empty2, "%s.app", empty1);
system(empty2);
}
-
Probably because fflush(stdin) is considered to exhibit undefined behavior by all of the C and C++ standards.
-
well do you know how to get the .app out of there? or if it will work? basically the whole issue is because I want to be able to enter something with a space which is the reason for fgets()..
-
First, did you take out fflush(stdin)?
Second, what exactly did you type in (including tabs, returns, etc) and what exactly did you get in the buffer (if you print it out)?
-
Code:
sprintf(empty2, "%s.app", empty1);
*cough*
-
OK, now I think there might be a code error.. here is the output of the code...and then the code itself.. I am not sure what is going on.. it seems to me it should work.
ok heres the output to the console.. btw this is unix.
new-host:C programs Alex$ ./a.out
This program can open applications or delete files
Please select what you like to do
enter 'Delete' to delete files, or 'open' to open
open
Type in the name of the app
Unable to find application named '
.app'
new-host:C programs Alex$
ok and code
Code:
if(toupper(choice[0]) == 'O')
{
char empty1[25];
char empty2[55];
char applic[15];
char applic2[18];
printf("Type in the name of the app\n");
fflush(stdin);
fgets(applic, 15, stdin);
sprintf(applic2, "\"%s\"", applic);
sprintf(empty1, "open -a %s", applic2);
sprintf(empty2, "%s.app", empty1);
system(empty2);
}
PLEASE HELP :0
oh and btw w/o the fflush(stdin); it does the same thing
-
So instead of taking out the bad fflush() statement, you put another one in? Brilliant.
-
The .app is there because you're using open to try and open a Mac OS X program. You still need to remove the newline which fgets() reads at the end of your input.
You still don't want fflush(stdin). Undefined behavior is bad.
-
settle down Mac. I had it copied and I pasted it twice, realized I did so, and then edited it..
-
well I know that, but I want to get it out. Like the only reason for doing any of this because a program like "Photo Booth" has a space and I need the space.. but when I get to the part where fgets is supposed to wait for my input it auto inputs .app and just goes right along.. I don't quite understand what you are saying read the link too.
-
call this function instead of fflush(stdin)
Code:
void clear_buffer(void)
{
int ch;
while((ch = getchar()) != '\n' && ch != EOF);
}
If you would have searched, i might have written this code around 1000 times lol
ssharish
-
The .app in the output is normal. It's from running open without a valid program name. When you finish typing your input you press return, which signifies a newline in the terminal. fgets() reads this newline into the string right along with everything else you typed in. Note the comment in the code in the FAQ article I linked to "Now test for and remove..."