Files
Files
2 File Operations
Files
3 File Handling functions
4 File Positions
Text File (or) ASCII File - The file that
contains ASCII codes of data like
File Types
digits, alphabets and symbols is
called text file (or) ASCII file.
2 Reading a file
Operations
on files 3 Writing a file
4 Closing a file
Creating / Opening
a file
Opens a text file in both reading and writing mode. It set the cursor position to
w+ the beginning of the file if it exists.
Opens a text file in both reading and writing mode. The reading operation is
a+ performed from begining and writing operation is performed at the end of the file.
Note
• The above modes are used with text files only. If we want to work with
2 getw()
Reading from
a file 3 fscanf()
4 fgets()
5 fread()
getc()
• getc( *file_pointer )
• This function is used to read a character from specified
file which is opened in reading mode.
• It reads from the current position of the cursor.
• After reading the character the cursor will be at next
character.
Code for getc()
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *fp;
char ch;
fp = fopen("a.txt","r");
printf("Reading character from the file: %c\
n",getc(fp));
ch = getc(fp);
printf("ch = %c", ch);
fclose(fp);
getch();
return 0;
}
Output
getw()
• getw( *file_pointer )
• This function is used to read an integer value form the
specified file which is opened in reading mode.
• If the data in file is set of characters, then it reads ASCII
values of those characters.
Code for getw()
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *fp;
int i,j;
fp = fopen("a1.txt","w");
putw(65,fp); // inserts A
putw(97,fp); // inserts a
fclose(fp);
fp = fopen("a1.txt","r");
i = getw(fp); // reads 65 - ASCII value of A
j = getw(fp); // reads 97 - ASCII value of a
printf("SUM of the integer values stored in
file=%d",i+j);//65+97=162
fclose(fp);
return 0;
}
Output
fscanf()
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *fp;
char str[100];
fp = fopen ("file.txt", "r");
fgets(str,6,fp);
printf("str = %s", str);
fclose(fp);
getch();
return 0;
}
fread()
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
FILE *fp;
char str[100];
fp = fopen("file.txt","r");
fread(str,sizeof(char),5,fp);
str[strlen(str)+1] = 0;
printf("str = %s", str);
fclose(fp);
return 0;
}
1 putc()
2 putw()
Writing into a
file 3 fprintf()
4 fputs()
5 fwrite()
putc()
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *fp;
char ch;
fp = fopen("file1.txt","w");
putc('A',fp);
ch = 'B';
putc(ch,fp);
fclose(fp);
return 0;
}
putw()
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *fp;
int i;
fp = fopen("file2.txt","w");
putw(66,fp);
i = 100;
putw(i,fp);
fclose(fp);
return 0;
}
fprintf()
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *fp;
char *text = "\nthis is example text";
int i = 10;
fp = fopen("file3.txt","w");
fprintf(fp,"This is line1\nThis is line2\n%d",i);
fprintf(fp,text);
fclose(fp);
return 0;
}
fputs()
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *fp;
char *text = "\nthis is example text";
fp = fopen("file4.txt","w");
fputs("Hi!\nHow are you?",fp);
fclose(fp);
return 0;
}
fwrite()
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *fp;
char *text = "Welcome to C Language";
fp = fopen("file5.txt","w");
fwrite(text,sizeof(char),5,fp);
fclose(fp);
return 0;
}
Closing a file
#include<stdio.h>
#include<conio.h>
int main() Output:
{
FILE *fp; Cursor position = 0
int position; Cursor position = 5
fp = fopen ("file4.txt", "r");
position = ftell(fp);
printf("Cursor position = %d\n",position);
fseek(fp,5,0);
position = ftell(fp);
printf("Cursor position = %d", position);
fclose(fp);
return 0;
}
Code for rewind()
#include<stdio.h>
#include<conio.h>
int main()
Output:
{
Cursor position = 0
FILE *fp;
Cursor position = 5
int position;
Cursor position = 0
fp = fopen ("file4.txt", "r");
position = ftell(fp);
printf("Cursor position = %d\n",position);
fseek(fp,5,0);
position = ftell(fp);
printf("Cursor position = %d\n", position);
rewind(fp);
position = ftell(fp);
printf("Cursor position = %d", position);
fclose(fp);
return 0;
}
Code for fseek()
#include<stdio.h>
#include<conio.h>
int main()
Output:
{
Cursor position = 0
FILE *fp;
Cursor position = 5
int position;
Cursor position = 12
fp = fopen ("file4.txt", "r");
position = ftell(fp);
printf("Cursor position = %d\n",position);
fseek(fp,5,0);
position = ftell(fp);
printf("Cursor position = %d\n", position);
fseek(fp, -5, 2);
position = ftell(fp);
printf("Cursor position = %d", position);
fclose(fp);
return 0;
}