How to Read in Entire File 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 single character from the file.
- fgets() – This role is used to read strings from files.
- fscanf() – This function is used to read the cake of raw bytes from files. This is used to read binary files.
- fread() – This part is used to read formatted input from a file.
Steps To Read A File:
- Open a file using the function fopen() and shop the reference of the file in a FILE arrow.
- Read contents of the file using whatever of these functions fgetc(), fgets(), fscanf(), or fread().
- File close the file using the function fclose().
Allow's begin discussing each of these functions in detail.
fgetc()
fgetc() reads characters pointed by the function pointer 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 next character. This office 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 role by reading characters one by one.
- Do-While loop will be used which volition read character until information technology reaches and of file.
- When it reaches end information technology returns EOF character (-1).
Using EOF:
Below is the C program to implement the above approach-
C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
main()
{
FILE
* ptr;
char
ch;
ptr =
fopen
(
"test.txt"
,
"r"
);
if
(Aught == ptr) {
printf
(
"file can't be opened \due north"
);
}
printf
(
"content of this file are \due north"
);
do
{
ch =
fgetc
(ptr);
printf
(
"%c"
, ch);
}
while
(ch != EOF);
fclose
(ptr);
return
0;
}
Input File:
GeeksforGeeks | A information science portal for geeks
Output:
In the above lawmaking, the arroyo is to read one grapheme from the file and check if it is not EOF, if it is not then print information technology and if information technology is then cease reading.
Using feof():
feof() function takes file pointer as statement and returns true 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() office cheque for cease of file. since feof() returns true afterwards it reaches the terminate.
- Use logical Not operator(!) so that when it reaches end status become 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
main()
{
FILE
* ptr;
char
ch;
ptr =
fopen
(
"exam.txt"
,
"r"
);
if
(Zilch == ptr) {
printf
(
"file can't exist 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 calculator science portal for geeks
Output:
fgets()
fgets() reads one string at a time from the file. fgets() returns a cord if it is successfully read past part or returns NULL if tin not read.
Syntax:
char * fgets(char *str, int size, FILE * ptr);
Hither,
str: It is cord in which fgets() store string after reading it from file.
size: It is maximum characters to read from stream.
ptr: It is file pointer.
Approach:
- In this arroyo, the contents of the file are read one character at a time until we reach the finish of the file.
- When we attain the end of the file fgets() tin can't read and returns Aught and the program will terminate reading.
Below is the C plan to implement the above approach:
C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
main()
{
FILE
* ptr;
char
str[50];
ptr =
fopen
(
"examination.txt"
,
"a+"
);
if
(Cypher == ptr) {
printf
(
"file tin't be opened \northward"
);
}
printf
(
"content of this file are \n"
);
while
(
fgets
(str, 50, ptr) != NULL) {
printf
(
"%south"
, 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, …)
Approach:
- fscanf reads formatted information from the files and stores it in variables.
- The data in the buffer is printed on the console till the finish of the file is reached.
C++
#include <stdio.h>
int
main()
{
FILE
* ptr =
fopen
(
"abc.txt"
,
"r"
);
if
(ptr == NULL) {
printf
(
"no such file."
);
return
0;
}
char
buf[100];
while
(
fscanf
(ptr,
"%*s %*due south %south "
,
buf)
== 1)
printf
(
"%southward\n"
, buf);
return
0;
}
Output:
fread()
fread() makes it easier to read blocks of data from a file. For instance, in the case of reading a structure from the file, information technology becomes an easy job to read using fread.
Syntax:
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
ptr: This is the arrow to a block of memory with a minimum size of size*nmemb bytes.
size: This is the size in bytes of each chemical element to exist read.
nmemb: This is the number of elements, each ane with a size of size bytes.
stream: This is the pointer to a FILE object that specifies an input stream.
Approach:
- Information technology first, reads the count number of objects, each i with a size of size bytes from the given input stream.
- The full amount of bytes reads if successful is (size*count).
- Co-ordinate to the no. of characters read, the indicator file position is incremented.
- If the objects read are not trivially copy-able, then the beliefs is undefined and if the value of size or count is equal to zero, then this program will only return 0.
C++
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct
Grade {
char
cname[30];
char
sdate[30];
};
int
main()
{
FILE
* of;
of =
fopen
(
"test.txt"
,
"westward"
);
if
(of == Zippo) {
fprintf
(stderr,
"\nError to open up the file\due north"
);
exit
(1);
}
struct
Course inp1 = {
"Algorithms"
,
"30OCT"
};
struct
Course inp2 = {
"DataStructures"
,
"28SEPT"
};
struct
Form inp3 = {
"Programming"
,
"1NOV"
};
fwrite
(&inp1,
sizeof
(
struct
Class),
1, of);
fwrite
(&inp2,
sizeof
(
struct
Class),
i, of);
fwrite
(&inp3,
sizeof
(
struct
Grade),
1, of);
if
(
fwrite
!= 0)
printf
(
"Contents to file written successfully !\n"
);
else
printf
(
"Error writing file !\n"
);
fclose
(of);
FILE
* inf;
struct
Class inp;
inf =
fopen
(
"test.txt"
,
"r"
);
if
(inf == NULL) {
fprintf
(stderr,
"\nError to open the file\northward"
);
exit
(one);
}
while
(
fread
(&inp,
sizeof
(
struct
Course),
1, inf))
printf
(
"Course Name = %southward Started = %s\due north"
,
inp.cname, inp.sdate);
fclose
(inf);
}
Output:
Source: https://www.geeksforgeeks.org/c-program-to-read-contents-of-whole-file/
0 Response to "How to Read in Entire File C+"
Postar um comentário