The use of fprintf?

Hi!

Can I use fprintf to print text from a .txt file?
What I mean is I want to print what the .txt file contains into the program.
If not can you teach me what function should I use to do this?

Thanks!
fprintf is a function that takes in a object stream, stderr, stdin, stdout which are used in *nix. A stream can also be a file... so yes you can.

http://www.cplusplus.com/reference/clibrary/cstdio/fprintf/ :

Writes to the specified stream a sequence of data formatted as the format argument specifies.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/* fprintf example */
#include <stdio.h>

int main ()
{
   FILE * pFile;
   int n;
   char name [100];

   pFile = fopen ("myfile.txt","w");
   for (n=0 ; n<3 ; n++)
   {
     puts ("please, enter a name: ");
     gets (name);
     fprintf (pFile, "Name %d [%-10.10s]\n",n,name);
   }
   fclose (pFile);

   return 0;
}
i personally think that's the luser way.... you might want to invest your time with file descriptors
Topic archived. No new replies allowed.