-
February 6th, 2006, 06:57 PM
#1
simple scanf problem
OK, maybe I forgot how to use scanf in C, but from the docs I can't seem to find out how to do this.
Basically I print out "Enter a filename, or press ENTER to print to the screen". Then the user can type in a filename, or just press ENTER to print whatever to the screen.
Basically:
Code:
int main()
{
char filename[256];
printf("Enter a file name or press ENTER to print to screen.\n");
if((scanf("%s", &filename)) == EOF) /* this is wrong. how can I do it? */
{
/* print stuff to the screen */
}
else
{
/* open the FILE with filename and print stuff to the file*/
}
}
The problem is, scanf doesn't return until you type somthing, then press ENTER. I hope it makes sense what I am asking. Is it even possible with scanf? I know it can be done easily with getc/getchar, but for some reason I thing I remember being able to do this with scanf long time ago. But can't now.
Thanks,
Latem
Being a pessimist is wonderful; you are either proven right, or pleasantly surprised.
-
February 6th, 2006, 07:19 PM
#2
Re: simple scanf problem
I don't think you can do what you want to do with scanf(). Use fgets() instead.
-
February 6th, 2006, 08:00 PM
#3
Re: simple scanf problem
Originally Posted by Latem
Code:
int main()
{
char filename[256];
printf("Enter a file name or press ENTER to print to screen.\n");
if((scanf("%s", &filename)) == EOF) /* this is wrong. how can I do it? */
First, the call to scanf() is incorrect:
Code:
scanf("%s", filename)
No "&".
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|