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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
01-07-2013, 01:43 AM
|
#1
|
|
LQ Newbie
Registered: Jan 2013
Posts: 3
Rep: 
|
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
|
|
|
|
01-07-2013, 01:53 AM
|
#2
|
|
Member
Registered: May 2011
Posts: 76
Rep: 
|
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.
|
|
|
|
01-07-2013, 02:43 AM
|
#3
|
|
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,079
|
> 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 [code] and [/code] tags if you paste code.
Last edited by NevemTeve; 01-07-2013 at 02:44 AM.
|
|
|
|
01-07-2013, 02:50 AM
|
#4
|
|
Member
Registered: May 2011
Posts: 76
Rep: 
|
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.
|
|
|
|
01-07-2013, 04:43 AM
|
#5
|
|
LQ Newbie
Registered: Jan 2013
Posts: 3
Original Poster
Rep: 
|
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.
|
|
|
|
01-07-2013, 05:02 AM
|
#6
|
|
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,079
|
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.
|
|
|
|
01-07-2013, 05:11 AM
|
#7
|
|
Senior Member
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 2,882
|
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.
|
01-07-2013, 05:28 AM
|
#8
|
|
LQ Newbie
Registered: Jan 2013
Posts: 3
Original Poster
Rep: 
|
Thanks for clearing doubt. It really helped me to understand the exact functionality of fscanf & scanf.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 03:31 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|