C question

I tried this program but it doesnt work as i thought:
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;
}

It doesnt save the names i put in. Someone help me!
I think you simply don't know where the myfile.txt is created (it should be in the executable's directory).

The program works fine for me.
Yes it creates in there, but when i type in whatever i want, the myfile.txt has nothing inside and stays 0 bytes
SOLVED: I forgot i needed to type 3 times a name. It works now.
Last edited on
Topic archived. No new replies allowed.