-
Saving spaces in arrays?
OK, heres whats happening. This part of the program can open a .app which the user specifies. It works perfectly on everything... except when there are spaces. For example (I am on a mac) if I was to open itunes, I would simply type in itunes, but if I want to open Photo Booth, im screwed because of the spaces. Now I already have the program to set quotes, but I think the problem is that I can't scan for spaces. I am using scanf() right now to get the program name, tryed gets() but that didn't work at all, it just skipped right past it. but anyway heres the code. This was kinda hard to explain so if you have a question just ask.
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");
scanf(" %s", applic); //scaned here
sprintf(applic2, "\"%s\"", applic);
sprintf(empty1, "open -a %s", applic2);
sprintf(empty2, "%s.app", empty1);
system(empty2);
}
-
Look into using fgets() to read into a large buffer. Much better than scanf().
-
well I am not reading from a file. this is just user keyboard. I am not sure how to do what I am about to say or if it is possible this part of C has always confused me ALOT. but are you talking about taking stdin (is that what it is) and using that?
-
stdin counts as a file. Just put stdin where you'd put a file pointer and it reads the keyboard input.
-
so you do something like this...
Code:
char string[20];
fgets(string, 20, stdin);
puts(string);
That would take everything I hit on the keyboard and print it to the console correct?
-
fgets works on streams. Stdin is one such stream, to the default input (console window unless you redirect it).
When opening files via fopen, the files are wrapped in a stream.
-
ok now I did that, and it did what I wanted... ish.. only there was something in the buffer I think its called.. so without me doing anything it entered .app for me.. and so obviously didn't work.. is there a way I can clear all of that before I use fgets()?