File Input/Output

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?
Is there something wrong with what you posted?

(BTW. Don't fflush(stdin).)
#include <stdio.h>
#include <stdlib.h>

int main()


{
FILE *fp;

fprintf(stdout, FILE *fp= fopen("text.txt","r");



// These next lines pause the computer.

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).

In your code, you've mixed some things. Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#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;
  }


Don't fflush(stdin).
Please use [code] tags.

I find that cplusplus.com has a very good reference. http://www.cplusplus.com/reference/clibrary/cstdio/
I also use cppreference a lot. http://www.cppreference.com/stdio/index.html

Hope this helps.
thanks a lot for posting Duoas.
I try to be useful. :-P

Did you learn what you wanted to know? You didn't forget that you'll need a loop to read every line?
Topic archived. No new replies allowed.