CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2004
    Location
    Canada
    Posts
    277

    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.

  2. #2
    Join Date
    May 2005
    Posts
    151

    Re: simple scanf problem

    I don't think you can do what you want to do with scanf(). Use fgets() instead.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: simple scanf problem

    Quote 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
  •  





Click Here to Expand Forum to Full Width

Featured