File Handling | Reading From a File Using fread() function | C Programming | PART 1.8

preview_player
Показать описание
In this video I have discussed reading from a file using fread() function.
fread() function is used to read string, id, float and any type of data types.

I have created this free of cost YouTube channel for computer science and information technology students. Through this channel, I have tried to explain some important topics in a simple way. This channel is very helpful for computer science engineering students may be from GATE, NET, M.TECH, B.Tech, BCA, BSC, MCA, MSC etc. In this channel, I am trying to cover previous years solved GATE questions, Data Structures, Algorithm Design, Operating System, Data Base Management System(DBMS), Theory of computation (TOC), compiler design, C programming etc.

My Channel URL:

#filehandling
#fwrite()
#fread()
#nargishgupta My Website URL:
Рекомендации по теме
Комментарии
Автор

5:05 sir please explain what is 1 in the syntax of fwrite

p.r
Автор

Sir what if we write
while (fread(&s, sizeof(s), 1, fp)==1)???

priyanshugupta
Автор

This code is incorrect sir. We need to use read binary mode in order to correctly read the text file.
Below is the code that I have corrected:
#include <stdio.h>
int main()
{
struct emp
{
int ID;
char name[50];
float salary;
};
struct emp e;
FILE *fp;
fp = fopen("file.txt", "rb");
if (fp == NULL)
{
printf("Cannot open file.\n");
return 1;
}
while (fread(&e, sizeof(e), 1, fp) == 1)
{
printf("%d %s %f\n", e.ID, e.name, e.salary);
}

fclose(fp);

return 0;
}

dokii.
Автор

This code is printing absurd values. Aren't they suppose to print the texts?

sunandachowdhury