//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;
}