The reading from a file operation is performed using the following pre-defined file handling methods.
- getc()
- getw()
- fscanf()
- fgets()
- 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.
Example
#include<stdio.h>
#include<conio.h>
int main(){
FILE *fp;
char ch;
fp = fopen("D://program.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
Example 2
#include<conio.h>
int main(){
FILE *fp;
char ch;
fp = fopen("D://program.txt","r");
if ( fp == NULL )
{
printf ( "Could not open file " ) ;
return 1;
}
while(1)
{
ch = getc(fp);
if(ch == EOF)
break;
printf("\n ch = %c", ch);
}
fclose(fp);
getch();
return 0;
}
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.
#include<stdio.h>
#include<conio.h>
int main(){
FILE *fp;
int i,j;
fp = fopen("D://program.txt","w");
putw(65,fp); // inserts A
putw(97,fp); // inserts a
fclose(fp);
fp = fopen("D://program.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
fscanf( *file_pointer, typeSpecifier, &variableName ) - This function is used to read multiple datatype values from specified file which is opened in reading mode.
#include<stdio.h>
#include<conio.h>
int main(){
char str1[10], str2[10], str3[10];
int year;
FILE * fp;
fp = fopen ("D://file.txt", "w+");
fputs("We are in 2021", fp);
rewind(fp); // moves the cursor to begining of the file
fscanf(fp, "%s %s %s %d", str1, str2, str3, &year);
printf("Read String1 - %s\n", str1 );
printf("Read String2 - %s\n", str2 );
printf("Read String3 - %s\n", str3 );
printf("Read Integer - %d", year );
fclose(fp);
getch();
return 0;
}
Output
fgets( variableName, numberOfCharacters, *file_pointer ) - This method is used for reading a set of characters from a file which is opened in reading mode starting from the current cursor position. The fgets() function reading terminates with reading NULL character.
#include<stdio.h>
#include<conio.h>
int main(){
FILE *fp;
char str[7];
fp = fopen ("D://file.txt", "r");
fgets(str,6,fp);
printf("str = %s", str);
fclose(fp);
getch();
return 0;
}
Output
fread( source, sizeofReadingElement, numberOfCharacters, FILE *pointer ) -
This function is used to read specific number of sequence of characters
from the specified file which is opened in reading mode.
#include<stdio.h>
#include<conio.h>
int main(){
FILE *fp;
char str[7];
fp = fopen ("D://file.txt", "r");
fread(str,sizeof(char),5,fp);
str[strlen(str)+1] = 0;
printf("str = %s", str);
fclose(fp);
getch();
return 0;
}
Output
No comments:
Post a Comment