filmov
tv
#49 Learn C Programming: how to read a file character by character #shorts

Показать описание
In this video you will learn how to read a file in C progamming.
Let's create a file one dot c using v i editor.
Start with adding a header file and the main function.
To read a file we need a file pointer, and a char variable.
We will use char variable to read the file one by one character.
We will call a function f open at line number eight, the first argument
is the name of file which we want to read, and the second argument
is the mode in which we want to open the file. r is used for read only mode.
f open function returns NULL if it fails to open the file.
at line number 10 we are checking if file pointer is NULL.
at line number 16, we will use a while loop to read the file chaacter by character
until we get end of file. In while loop we are using f getc function.
f getc function will read the char and assign it ot ch variable,
then we will print this variable on console.
We can create one file example dot text with some random text.
Lets compile and run this program now.
Let's create a file one dot c using v i editor.
Start with adding a header file and the main function.
To read a file we need a file pointer, and a char variable.
We will use char variable to read the file one by one character.
We will call a function f open at line number eight, the first argument
is the name of file which we want to read, and the second argument
is the mode in which we want to open the file. r is used for read only mode.
f open function returns NULL if it fails to open the file.
at line number 10 we are checking if file pointer is NULL.
at line number 16, we will use a while loop to read the file chaacter by character
until we get end of file. In while loop we are using f getc function.
f getc function will read the char and assign it ot ch variable,
then we will print this variable on console.
We can create one file example dot text with some random text.
Lets compile and run this program now.