File Operation
File Operation
File pointer
• FILE *fp;
• * is pointer symbol
• Mode
• Etc …
Close file
• fclose( FILE *fp )
#include<stdio.h>
int main()
{
FILE *fp;
char ch;
fp = fopen(“file.txt", "w");
printf(“Input any texts: ”);
while( (ch = getchar()) != EOF) {
putc(ch, fp);
}
fclose(fp);
fp = fopen("file.txt", "r");
return 0;
}
fscanf & fprintf
• Recall : scanf & printf
int main()
{
FILE *p,*q;
char name[32];
int age;
p = fopen("file.txt", "w");
printf("Enter Name and Age:");
scanf("%s %d", name, &age);
fprintf(p,"%s %d\n", name, age);
fclose(p);
q = fopen("file.txt", "r");
fscanf(q,"%s %d", name, &age);
printf("%s %d\n", name, age);
fclose(q);
return 0;
}
Write or Append ?
• Both are used to write in a file. In both the modes, new file
is created if it doesn't exists already.