Unit 5
Unit 5
Unit 5
Generally, a file is used to store user data in a computer. In other words, computer stores the data
using files.
File is a collection of data that stored on secondary memory like harddisk of a computer.
C programming language supports two types of files and they are as follows...
Text File (or) ASCII File - The file that contains ASCII codes of data like digits, alphabets and symbols
is called text file (or) ASCII file.
Binary File - The file that contains data in the form of bytes (0's and 1's) is called as binary file.
Generally, the binary files are compiled version of text files.
File Operations in C
The following are the operations performed on files in c programming langauge...
All the above operations are performed using file handling functions available in C.
5 w+ Opens a text file in both reading and writing mode. It set the cursor position to the b
it exists.
6 a+ Opens a text file in both reading and writing mode. The reading operation is perform
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 binary files we use
rb, wb, ab, rb+, wb+ and ab+.
1. getc()
2. getw()
3. fscanf()
4. fgets()
5. fread()
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.
int main(){
FILE *fp;
char ch;
clrscr();
fp = fopen("MySample.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( *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.
FILE *fp;
int i,j;
clrscr();
fp = fopen("MySample.txt","w");
putw(65,fp); // inserts A
putw(97,fp); // inserts a
fclose(fp);
fp = fopen("MySample.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);
getch();
return 0;
}
Output
return 0;
}
Output
int main(){
FILE *fp;
char *str;
clrscr();
fp = fopen ("file.txt", "r");
fgets(str,6,fp);
printf("str = %s", str);
fclose(fp);
getch();
return 0;
}
Output
int main(){
FILE *fp;
char *str;
clrscr();
fp = fopen ("file.txt", "r");
fread(str,sizeof(char),5,fp);
str[strlen(str)+1] = 0;
printf("str = %s", str);
fclose(fp);
getch();
return 0;
}
Output
Writing into a file
The writing into a file operation is performed using the following pre-defined file handling methods.
1. putc()
2. putw()
3. fprintf()
4. fputs()
5. fwrite()
putc( char, *file_pointer ) - This function is used to write/insert a character to the specified
file when the file is opened in writing mode.
Output
putw( int, *file_pointer ) - This function is used to writes/inserts an integer value to the
specified file when the file is opened in writing mode.
Output
fprintf( *file_pointer, "text" ) - This function is used to writes/inserts multiple lines of text with
mixed data types (char, int, float, double) into specified file which is opened in writing mode.
Output
fputs( "string", *file_pointer ) - TThis method is used to insert string data into specified file
which is opened in writing mode.
Output
Closing a file
Closing a file is performed using a pre-defined method fclose().
fclose( *f_ptr )
The method fclose() returns '0'on success of file close otherwise it returns EOF (End Of File).
1. ftell()
2. rewind()
3. fseek()
ftell( *file_pointer ) - This function returns the current position of the cursor in the file.
Output
rewind( *file_pointer ) - This function is used reset the cursor position to the beginning of the
file.
Output