tags:

views:

484

answers:

5

how does fprintf works??

if i write fprintf(outfile, "test %d %d 255/r", 255, 255);

what does it mean?? i know outfile, is the name my of output file. what would the other values mean?

+5  A: 

"test %d %d 255/r" tells that after it will be arguments (and they are there: 255, 255) and they are expected to be of integer type. And they will be placed instead of %d.

In result you'll get string test 255 255 255 in your file.

For more infrormation read fprintf reference.

Kirill V. Lyadvinsky
+1  A: 

It's similar to printf, it just prints the output to a file.

Aziz
+3  A: 

The first parameter is a file handle, the second is a formatting string, and after that there is a variable number of arguments depending on how many format specifiers you used in your 2nd parameter.

Check the documentation, it contains all of the information you are asking.

Brian R. Bondy
+1  A: 

The second argument is the format string. Any additional arguments are the parameters to the specifier in the format string (in this case, %d). Check out http://www.cppreference.com/wiki/c/io/printf for a good intro to printf-style functions.

John Ledbetter
+3  A: 

it's a formatted output to a file stream. It works like printf does, the difference is that printf always outputs to stdout.If you wrote

fprintf(stdout, "test %d %d 255\n", 255, 255);

it would be the same as the printf equivalent.

The second argument to it is the format string. The format string contains format specifiers, like %s, %d, %x. Yours contains two %ds. Each format specifier must have a corresponding argument in fprintf. Yours has two %d specifiers, so there are two numerical arguments:

fprintf(outfile, "Here are two numbers: %d, %d", 4, 5);

likewise, you could use string specifiers (%s), hexadecimal specifiers (%x) or long/long long int specifiers (%ld, %lld). Here is a list of them: http://www.cppreference.com/wiki/c/io/printf. Note that they are the same for all of the C formatted i/o functions (like sprintf and scanf).

Also, in your original example, "/r" will just literally print "/r". It looks like you were trying to do a carriage return ("\r").

Carson Myers