Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how fprintf(),fscanf() work

Status
Not open for further replies.

ssrc

Programmer
May 20, 2003
5
IN

hello
i want to know how fprintf(),fscanf() works..


suppose i have data like below?

item number price quantity
aaa-1 111 23.76 115
bbb-2
c-3 125 31.67 104

now suppose i am using in a LOOP fscanf(stdin,"%s%d%f%d", item,&number,&price,&quantity);--then how file pointer will work? and what it this STDIN???
 
stdin is a file pointer that represents the standard input. That's usually the keyboard, unless you've redirected it somehow.

How does the file pointer work? The file pointer itself doesn't "do" anything, it just represents something (a file, usually) that you can get data from or write data to.

fscanf reads characters from the file pointer. Specifically, it reads sequences of characters separated by whitespace. Each sequence is a token.

When it manages to read a token, it looks at the next formatting symbol in the string you gave it and tries to convert that token into the corresponding data type.

For example, %s means to convert to a char*. %d means to convert to a decimal integer.

Once it converts to the proper data type, it stores it in the corresponding argument to fscanf.


fprintf works backwards; it takes data from your program as arguments and tries to convert it to the character representation specified by the formatting string. Then it writes it to the given file pointer.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top