Course Content
History of C Language
0/2
Basic Structure of C Program
0/1
Types of Errors
0/1
Language Fundamentals
0/1
Data Types and Modifiers
0/1
Programming with C Language
About Lesson

 

//creating a file with fprintf

#include

void main(){

FILE *fp;

fp = fopen(“c:\users\satyamitguru\desktop\file2.txt”, “w”);//creating a file at a specific location

fprintf(fp, “Hello file by fprintf…n”);//writing data into file

fclose(fp);//closing file

printf(“Done…”);

}

//Write to a text file

#include

#include

int main()

{

   int num;

   FILE *fptr;

   fptr = fopen(“program.txt”,”w”);

   if(fptr == NULL)

   {

      printf(“Error!”);

      exit(1);

   }

   printf(“Enter num: “);

   scanf(“%d”,&num);

   fprintf(fptr,”%d”,num);

   fclose(fptr);

   return 0;

}

//Read from a text file

#include

#include

int main()

{

   int num;

   FILE *fptr;

   if ((fptr = fopen(“program.txt”,”r”)) == NULL){

       printf(“Error! opening file”);

       // Program exits if the file pointer returns NULL.

       exit(1);

   }

   fscanf(fptr,”%d”, &num);

   printf(“Value of n=%d”, num);

   fclose(fptr);

   return 0;

}

You cannot copy content of this page