How to Read From File Char C++
C programming language supports four pre-defined functions to read contents from a file, defined in stdio.h header file:
- fgetc() – This function is used to read a unmarried graphic symbol from the file.
- fgets() – This function is used to read strings from files.
- fscanf() – This function is used to read the block of raw bytes from files. This is used to read binary files.
- fread() – This function is used to read formatted input from a file.
Steps To Read A File:
- Open a file using the office fopen() and shop the reference of the file in a FILE pointer.
- Read contents of the file using whatever of these functions fgetc(), fgets(), fscanf(), or fread().
- File shut the file using the function fclose().
Let'due south begin discussing each of these functions in detail.
fgetc()
fgetc() reads characters pointed by the office arrow at that fourth dimension. On each successful read, it returns the character (ASCII value) read from the stream and advances the read position to the adjacent character. This part returns a constant EOF (-1) when there is no content to read or an unsuccessful read.
Syntax:
int fgetc(FILE *ptr);
Approach:
- This program reads the whole content of the file, using this office past reading characters one by 1.
- Practise-While loop will be used which will read graphic symbol until it reaches and of file.
- When information technology reaches terminate information technology returns EOF grapheme (-one).
Using EOF:
Below is the C program to implement the higher up approach-
C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
primary()
{
FILE
* ptr;
char
ch;
ptr =
fopen
(
"examination.txt"
,
"r"
);
if
(Null == ptr) {
printf
(
"file can't be opened \n"
);
}
printf
(
"content of this file are \northward"
);
do
{
ch =
fgetc
(ptr);
printf
(
"%c"
, ch);
}
while
(ch != EOF);
fclose
(ptr);
return
0;
}
Input File:
GeeksforGeeks | A figurer science portal for geeks
Output:
In the above code, the approach is to read one character from the file and check if it is not EOF, if information technology is not then impress it and if it is then finish reading.
Using feof():
feof() part takes file pointer every bit argument and returns truthful if pointer reaches the stop of the file.
Syntax:
int feof(FILE *ptr);
Arroyo:
- In this approach, a character is read using fgetc().
- Using feof() function check for end of file. since feof() returns true after it reaches the end.
- Use logical Not operator(!) and then that when it reaches terminate condition get false and loop stop.
Beneath is the C program to implement the above approach:
C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
primary()
{
FILE
* ptr;
char
ch;
ptr =
fopen
(
"test.txt"
,
"r"
);
if
(Cipher == ptr) {
printf
(
"file can't be opened \n"
);
}
printf
(
"content of this file are \n"
);
while
(!
feof
(ptr)) {
ch =
fgetc
(ptr);
printf
(
"%c"
, ch);
}
fclose
(ptr);
return
0;
}
Input File:
GeeksforGeeks | A information science portal for geeks
Output:
fgets()
fgets() reads one string at a time from the file. fgets() returns a string if it is successfully read by function or returns NULL if can not read.
Syntax:
char * fgets(char *str, int size, FILE * ptr);
Here,
str: It is string in which fgets() store string later on reading it from file.
size: Information technology is maximum characters to read from stream.
ptr: Information technology is file pointer.
Approach:
- In this approach, the contents of the file are read one grapheme at a time until nosotros attain the terminate of the file.
- When we reach the cease of the file fgets() tin't read and returns Zip and the program will end reading.
Below is the C program to implement the above approach:
C
#include <stdio.h>
#include <stdlib.h>
#include <cord.h>
int
main()
{
FILE
* ptr;
char
str[50];
ptr =
fopen
(
"test.txt"
,
"a+"
);
if
(Nothing == ptr) {
printf
(
"file tin can't exist opened \n"
);
}
printf
(
"content of this file are \n"
);
while
(
fgets
(str, 50, ptr) != NULL) {
printf
(
"%s"
, str);
}
fclose
(ptr);
return
0;
}
Input File:
GeeksforGeeks | A computer science portal for geeks
Output:
fscanf()
fscanf() reads formatted input from a stream.
Syntax:
int fscanf(FILE *ptr, const char *format, …)
Arroyo:
- fscanf reads formatted data from the files and stores it in variables.
- The data in the buffer is printed on the console till the terminate of the file is reached.
C++
#include <stdio.h>
int
main()
{
FILE
* ptr =
fopen
(
"abc.txt"
,
"r"
);
if
(ptr == Nil) {
printf
(
"no such file."
);
return
0;
}
char
buf[100];
while
(
fscanf
(ptr,
"%*s %*s %due south "
,
buf)
== 1)
printf
(
"%s\due north"
, buf);
return
0;
}
Output:
fread()
fread() makes it easier to read blocks of data from a file. For instance, in the instance of reading a structure from the file, it becomes an piece of cake job to read using fread.
Syntax:
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
ptr: This is the pointer to a block of retention with a minimum size of size*nmemb bytes.
size: This is the size in bytes of each element to be read.
nmemb: This is the number of elements, each one with a size of size bytes.
stream: This is the pointer to a FILE object that specifies an input stream.
Approach:
- It first, reads the count number of objects, each one with a size of size bytes from the given input stream.
- The total amount of bytes reads if successful is (size*count).
- According to the no. of characters read, the indicator file position is incremented.
- If the objects read are not trivially re-create-able, then the beliefs is undefined and if the value of size or count is equal to zero, then this program will simply return 0.
C++
#include <stdio.h>
#include <stdlib.h>
#include <cord.h>
struct
Course {
char
cname[xxx];
char
sdate[30];
};
int
main()
{
FILE
* of;
of =
fopen
(
"exam.txt"
,
"w"
);
if
(of == NULL) {
fprintf
(stderr,
"\nError to open the file\n"
);
exit
(ane);
}
struct
Grade inp1 = {
"Algorithms"
,
"30OCT"
};
struct
Course inp2 = {
"DataStructures"
,
"28SEPT"
};
struct
Course inp3 = {
"Programming"
,
"1NOV"
};
fwrite
(&inp1,
sizeof
(
struct
Class),
one, of);
fwrite
(&inp2,
sizeof
(
struct
Course),
1, of);
fwrite
(&inp3,
sizeof
(
struct
Form),
1, of);
if
(
fwrite
!= 0)
printf
(
"Contents to file written successfully !\n"
);
else
printf
(
"Error writing file !\northward"
);
fclose
(of);
FILE
* inf;
struct
Class inp;
inf =
fopen
(
"test.txt"
,
"r"
);
if
(inf == Zilch) {
fprintf
(stderr,
"\nError to open the file\n"
);
exit
(1);
}
while
(
fread
(&inp,
sizeof
(
struct
Course),
1, inf))
printf
(
"Course Name = %due south Started = %s\n"
,
inp.cname, inp.sdate);
fclose
(inf);
}
Output:
Source: https://www.geeksforgeeks.org/c-program-to-read-contents-of-whole-file/
Post a Comment for "How to Read From File Char C++"