LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
LinkBack Search this Thread
Old 01-07-2013, 01:43 AM   #1
ddialani
LQ Newbie
 
Registered: Jan 2013
Posts: 3

Rep: Reputation: Disabled
fscanf does not print on screen


I am trying to take input from stdin and along with that i would like user an interactive output through fscanf. I used fscanf as given in example but it does not print anything and always returns an error that leads to execution of fprintf().


#include "stdio.h"

int main(int argc, char* argv[])
{
int height, width;
if ( fscanf( stdin, "Height: %i, Width: %i", &height, &width ) != 2 )
{
fprintf( stderr, "###Error with fscanf: bad input data.\a\n" );
return 0;
}

printf("Height %d, Width %d\n", height, width);
return 0;
}


Thanks in advance
 
Old 01-07-2013, 01:53 AM   #2
cristi92b
Member
 
Registered: May 2011
Posts: 82

Rep: Reputation: Disabled
1.try using #include <stdio.h> instead of #include "stdio.h"

2.you expect having two int values in stdin, but your stdin is empty...

fscanf( stdin, "Height: %i, Width: %i", &height, &width )

you have no input!

See what fscanf does: http://www.cplusplus.com/reference/cstdio/fscanf/

3. fprintf( stderr, "###Error with fscanf: bad input data.\a\n" ); always executes because your program fails to read a string (char*) from stdin wih format "Height: %i, Width: %i".

-------------

See this example:
(It works because now you have something in stdin, but also, there is no input)

-------------
Code:
#include <stdio.h>

 int main(int argc, char* argv[])
 {
 int height, width;
 //fprintf(stdin,"Height: %i, Width: %i",34,78);
 if ( fscanf( stdin, "Height: %i, Width: %i", &height, &width ) != 2 ) //same thing as scanf("Height: %i, Width: %i",&height,&width)
 { 
 fprintf( stderr, "###Error with fscanf: bad input data.\a\n" );
 return 0;
 }

 printf("Height %d, Width %d\n", height, width);
 return 0;
 }
---------------

the safest way of reading something from keyboard is:
1. declare a char array
char buffer[256];
2. use fgets to copy everything from stdin to buffer (Important: stdin must be empty if you want this function to ask you to type a string. If the stdin is not empty, then it will take what is already in stdin).
fgets(stdin,256,buffer);
3. use sscanf function to read your variables from the char array (in this case buffer)
sscanf(buffer,"%d %d",&height,&width);

---------------

Or you can simply use scanf...

Last edited by cristi92b; 01-07-2013 at 03:06 AM.
 
Old 01-07-2013, 02:43 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,456

Rep: Reputation: 445Reputation: 445Reputation: 445Reputation: 445Reputation: 445
> I used fscanf as given in example but it does not print anything

Nobody stated it would ever print anything. That's what (f)printf does.

And please use &#91;code] and &#91;/code] tags if you paste code.

Last edited by NevemTeve; 01-07-2013 at 02:44 AM.
 
Old 01-07-2013, 02:50 AM   #4
cristi92b
Member
 
Registered: May 2011
Posts: 82

Rep: Reputation: Disabled
what example? my example?
fprintf will print, and you will see that if you print on stderr or stdout.
fscanf does not print anything but fprintf does...

------------

Now I see I was wrong...

Just type something like: "Height: %i, Width: %i" and it will work.

fscanf will ask you for an input when you try to read from stdin.

The only mistake OP did was #include <stdio.h>

Last edited by cristi92b; 01-07-2013 at 03:00 AM.
 
Old 01-07-2013, 04:43 AM   #5
ddialani
LQ Newbie
 
Registered: Jan 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
if fscanf and scanf do not print strings (Height and Width given in code) then what is the use of these strings in fscanf and scanf functions.
 
Old 01-07-2013, 05:02 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,456

Rep: Reputation: 445Reputation: 445Reputation: 445Reputation: 445Reputation: 445
Basically nothing, leave them out:
Code:
scanf ("%d %d", &height, &width);
(Except if you had an input file containing lines like this:
Height: 32, Width: 40
Height: 40, Width: 1
...)

Last edited by NevemTeve; 01-07-2013 at 05:06 AM.
 
Old 01-07-2013, 05:11 AM   #7
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,000

Rep: Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003
the meaning is to parse the input text:
using: fscanf( stdin, "Height: %i, Width: %i", &height, &width ) and typing "this:34 and that: 56" will do nothing, but entering Height: 44, Width: 33 will find two integers.
try to check it
 
1 members found this post helpful.
Old 01-07-2013, 05:28 AM   #8
ddialani
LQ Newbie
 
Registered: Jan 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thanks for clearing doubt. It really helped me to understand the exact functionality of fscanf & scanf.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C fscanf function exvor Programming 14 06-05-2007 09:40 AM
using fread with fscanf andystanfordjason Programming 4 12-20-2006 09:49 AM
fscanf returns -1 trutnev Programming 2 06-23-2005 08:50 AM
fscanf c programming tomato Programming 14 01-09-2004 08:33 PM
using fscanf function Linh Programming 2 07-14-2003 12:58 PM


All times are GMT -5. The time now is 11:58 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration