this is my code :

#include <stdio.h>
#include <stdlib.h>
int main(){
     FILE *fp;
      int i,n;
     char  task[ 30] ;
     char filename[ 30]="c:\\out.c";
     fp =fopen(filename,"w");
     if(fp != NULL){
          printf("Enter number of tasks for today!\n");
          scanf("%d", &n);
         for(i=0;i<n;++i){
         printf( "Enter tasks for today!\n");
         scanf("%s",task);
         fprintf(fp,"%s\n",task);
           }
         fclose(fp);
         printf("\nDone!");
     }else{
           printf("couldn't open the file!");
           exit(1);
     }
     return 0;
}

the only problem that I met is there is no file created
although I get the Done! message after entering the data

Are you checking in the root directory of "C:"?

Does fclose return 0?

Do you have permissions to write to the root directory?

Why is the file extention ".c" for the output? (.c is generally used for c source files. .txt is generally used for generic text files.)

the third question of yours is the answer to my problem thanks a lot

You just try this code snippet. It is simple.

FILE *fp;
fp=fopen("c:\\test.txt", "wb");
if(fp == null)
   return;
char x[10]="ABCDEFGHIJ";
fwrite(x, sizeof(x[0]), sizeof(x)/sizeof(x[0]), fp);
fclose(fp);

Or else you can done it in the following way as well to write a sentence in the file.

#include <stdio.h>
#include <stdlib.h>  /* For exit() function */
int main()
{
   char c[1000];
   FILE *fptr;
   fptr=fopen("program.txt","w");
   if(fptr==NULL){
      printf("Error!");
      exit(1);
   }
   printf("Enter a sentence:\n");
   gets(c);
   fprintf(fptr,"%s",c);
   fclose(fptr);
   return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.