Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m trying to write a string of text to both standard output and a file using f

ID: 3872111 • Letter: I

Question

I'm trying to write a string of text to both standard output and a file using fprintf, but I'm getting an error on the "stdout" command saying "Parameter type mismatch: Incompatible pointer types 'const char*' and 'FILE*'". So i tried typing const char *stdout; underneath FILE *fp; and it still would not print to screen. I need to be able to output to both the screen and a text file using the same fprintf command, rather than using the fprintf command to write to a text file and a separate printf command to write to the screen. So what is the syntax for performing this execution?

FILE *fp; fp = fopen(..program3.txt" "w"); , if (disc

Explanation / Answer

Dear Student,

here is the answer...

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

fprintf() function is act just like printf(), printf() function print the data on the standard output, while sprintf() function is used to print the data to a file.

Suppose you want to print "Hello, World" in the standard output what would you right..??

You will write the statement as follows..

printf("Hello, Word");

just like above if you want to print a string like "Hello, World" to a file then you should write as below.

fprintf(fp, "Hello, World");

Finally i am going to correct your code...

in your case you correct statement would be.Note: In your case you are printing only string so below code is correct.

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

File *fp;

fp = fopen("program3.txt", "w");

if(disk < 0)

{

fprintf(fp, "NO REAL ROOTS");                     //this is the correct statement.

return -1;

}

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

Let's take another example for clearing your doubt about fprintf() function.

Note: This is just to make you understand.

Suppose you want print a data into file then you have to also pass the format specifier and the variable name.

see below

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

int n = 10;

File *fp;

fp = fopen("program3.txt", "w");

fprintf(fp, "%d", n);        //this statement will print the 10 to the file program3.txt.

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

Kindly Check and Verify Thanks...!!!