So im trying to write a program that uses fprintf to print to stdout. Please note that stdout is not a file name, but an existing FILE* that always gets opened when I run a program.
But all I have so far is:
#include <stdio.h>
int main()
{
fprintf(stdout, "This program has printed to stdout\n");
// These next lines pause the computer.
printf("\n\nPress [ENTER] to exit the program.\n");
fflush(stdin);
getchar();
return 0;
}
Could someone please point me in the right direction?
printf("\n\nPress [ENTER] to exit the program.\n");
fflush(stdin);
getchar();
return 0;
}
Im trying to output a file using stdout.
Like I said before Im trying to write a program that uses fprintf to print to stdout. Note that stdout is not a file name, but anexisting FILE* that always gets opened when I run the program.
Listen, I happen to be pretty smart. If you think I missed something the first time through, don't just blandly repeat yourself. It is arrogant and rude.
It looks like you already have a pretty firm grasp on C. So what exactly is giving you trouble? Opening a file? Reading lines from file? Using fprintf() format specifiers? Using char arrays (aka strings)? What?
Try to rephrase exactly what you want to do (writing a file is a general topic).
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp;
char s[ 100 ];
fp = fopen( "text.txt", "r" );
if (!fp)
/* Fooey. Complain. */
fprintf( stderr, "I couldn't open the file.\n" );
else
{
/* Read one line of text */
fgets( s, 100, fp );
/* Print it to stdout */
printf( "%s", s );
}
/* These next lines pause the computer. */
printf( "\n\nPress [ENTER] to exit the program.\n" );
getchar();
return 0;
}