https://www.facebook.com/Kinz.Crary
https://www.facebook.com/Kinz.Crary
-------------------------------------------
File and File I/O Operation Video 2 - Duration: 11:33.Welcome to C programming session. In today's session we
will be seeing how to do file I/O operations in C that's part two,
I hope so you have already seen my part one session
in which I have explained what is the need to file I/O? How to do
many more operations with files? That is how to create a new file?
How to open the file? How to read and write from the files? And how to
close to files? In that have to shown in Example
how to do all these operations on an integer value to be stored
into a file. Now comes the second part of the video.
In this video I'll be showing you what are the major
data types to be stored into the files and how we can do with different
types of functions
available in the library. First and foremost, I do expect that you know
file I/O. Okay so whenever we want
to store some set values into a secondary memory storage
we have to store it in the with the help of file I/O in C
programming, So datas instead of storing into the memory
will be transferring the data into secondary storage memory that is
hard disk.
Now comes the questions, that what different types of values to be stored?
Because as in C programming you will be finding that I may
deal with datas of character data type, I may deal
with string values, I may deal with float values double values,
integer values and finally if I have data in structure format then
how can I store these all different type data types into the
files? Let's see with it, with an example, I have created an example which I have shown
all different types of data types to be stored and read from the files
okay, First and foremost, we will be seeing how to store and read
character values from the files, let's see an example.
First and foremost, I have created a structure this is to show this structure
also
so just for the timing we skip that part and I have created all the variables
integer float, character, character array that string
structure variables and all okay, I have created two variables
for each data types that is ivalue is the value which I will be getting
from the value from the user and that value will be stored into the file
and once I have stored the file into the file that value will be read
into an irel. i for integer I'm considering
similarly for floats, similarly for character, similarly for string
and similarly for structure. Okay so this is simple
variable declaration section, as I have said in the previous example I have explained
my file I/O part one that file fp
that is the *fp is a pointer to the file
which is a structure declared in stdio.h, So I hope so you know that
so all the variables has been declared. Let's start with the operations.
First and foremost, how to read and write the character values?
First and foremost, you have to open the file, so I open the file one.txt
in the write mode, so it will create a new file name one.txt
with the blank data okay, first and foremost, I have to get value from the user
so I am asking users to enter the value if the value is stored in cvalue
which is a local variable of type character so still the value is in the
RAM that is in the memory it is not being send into the
secondary storage device. Now, if you want to send a character value
into a secondary storage device then you have to use fputc
fputc is a function which allows you to write
data from a memory or from a variable
to the secondary storage. How to do that? fputc takes two arguments
one is cvalue in which I am passing the variable name
which is having the value suppose I'm entering
"a" as a value "a" will be stored in cvalue that cvalue
I'm saying fputc please transfer the cvalue into the fp
what is fp? fp is currently pointing to one.txt
My task is over, so I'm closing the file pointer. Finally to check
whether the work is done or not I've again opened the file but this time I'm opening in
the read mode. Now to read the value character value from the file
we have to use the function fgetc
fgetc takes only single argument it ask the file pointer which knows the information
where the file it is
so I'm passing fp so it says I know one.txt
so fgetc will start reading from one.txt, one.txt will be read
it will read the single character and it will pass into crel
crel and cvalue are the character variables being declared by me
finally I'm printing the value of the character so
it prints if suppose "a" is being entered 'a" writes into the file
closes the file, again opens the file in the read modes
again reads the character that is again "a" is read into crel
and I'm printing the "a" value. So ultimately I should get "a" here.
Again I'm closing the file. Now comes as
so we understood how to do with character.
Okay, now how to do with strings? Yes, it is quite possible that you can write
the same thing in a loop by getting the
length of the string and all those things but there is some other options
are also available,
I will be most sticking into other options because I hope so
those who know fgetc and fputc they can understand that we can get the string
length.
Okay, while this character array is not having the null
okay, so null carries not find till now the loop is continuing we can do the same task
fgetc function, fputc function
can we repeatedly be called in the loops and we can do the string operation
but what another options are available? There is an another function
that is fprintf and fscanf, similar to fgetc and fputc
fprintf and fscanf are the functions with the help of which we can work
with different data types. I'm just doing for string
you can do the same fprintf and fscanf functions for double and
float also. First let me explain your first Strings,
so first and foremost, I'm again opening the same file
one.txt in write mode, I'm getting the value now the value is stored
in strvalue, it is in the memory I want to transfer it
fprintf takes three arguments, first and foremost, in which file you want to write
so I'm saying fp knows it so that's one.txt what data type you want to write
%s so indicating that it's a string value
what is the value? So I'm saying this variable knows it so this three arguments I passed it is
simply writes data into the file
finally task is over I closes the file.
Now comes again I want to read it and so again I have to open the file
in to the read mode, now instead of fprintf now I have to use fscanf
fscanf again the same three arguments you have to take that is
fp from where you want to read? what data type you want to read?
and where you want to store it? so previously, I have stored the
value in strvalue, strrel is a character array
or you can say a string type which is ready to take the values all the data is
stored into it
and it is being printed, if suppose I'm passing "Ashok" here
I'll be getting here also "Ashok", suppose I passed "Vinod Pillai" here
I'll be getting the "Vinod Pillai" okay.
So that is being cleared now, okay, we'll run
all of the program at a single time only
So at the last I'll be showing what is output and all those thing is? now comes
how to read integer value? I hope so you've seen my previous video in the previous I have shown
how to work with the
putw and getw so I'll l be not be repeating those codes here
let's concentrate on structure value, this is much important.
Many people are interested on how to write exactly the structure into the
files? okay, so first and foremost,
to read and write the structure you have open again the files, our file is ready
now second question comes, I want to get the value so structure is there structure
variable is there
previously if you have seen at the top I have created two structure variables
svalue and srel, both of them not having value first and foremost, I have
to enter the values into svalue
okay so I'm asking printf scanf svalue.id svalue.marks
okay so for if you not seen it, this is the structure
and it is having two variables that is id and marks
I have created, sorry, I have created two variables
of that structure, as struct Student svalue and srel
so when I say &svalue.id so svalue's id will be entered
svalue's marks will be entered
now comes how to enter how to write the data? There is a special function called
fwrite, now here you have to be importantly understand this thing
fwrite and fread are the functions to write a block of data or you can say
complete bytes so fread and fwrite says
I will write the data I will not write single structure or something
he is simply asking what block of data to be written
okay, so whenever you're writing block of data or you can say when
you're working with bytes
it is better to open the file in the binary mode
okay, so instead of simple write mode you have to specify binary
and .dat file is preferred in the binary case.
Okay it is not compulsory, it is preferred one. So, fwrite
is the method I'm calling okay fwrite says
I want four arguments, first and foremost, who is having the values?
give me the address of it. So I'm saying svalue is the variable address I'm passing
what is the sizeof a single block?
so I've sizeof svalue I'm saying sizeof svalue
so that is the sizeof single svalue, how many such blocks are coming? So I'm
saying currently I'm having
only svalue so I'm passing one and third
where to write that is fp. Now quite confusing what is one and all those
things
I'm not shown here but in most probably by next video or something
I will be showing how you can write complete array into the file also
it is all same so I'm not shown here if your request comes I will
too put that also that is f with the help of fwrite
we can write complete array data in a single command
that is instead of writing if suppose our array is of hundred we don't
have to call hundred times fwrite
single fwrite function will work how I have to specify the array name,
size of an array and here instead of one
what is the size of array, have to specify one single block takes this much space
he has to carry hundred spaces so it will take according space it will create
according spaces and it will send the data.
So this size is saying how many such data you are passing
in my current scenario svalue is a single variable is not an array,
so I am saying
one okay. I hope so you have understood, the task is over
I'm closing it finally I want to read the data
again I open it in the read binary mode, it should of fwrite
I'm saying fread svalue was having value srel was empty
so I'm saying load all the value into srel what is the sizeof srel?
how much srels are ready? Only one because one has been written
and I want to read one
only and finally I'm printing the value of srel. Let's see the output
what is it coming. First and foremost, I compile it
I run it, so it's asking me the character value
if you remember thats the first one we used to have it okay,
so this is right now it is asking the value so instead of "a"
I will type something like suppose "x" so it's showing
it has return into the file and it has read and it's working got it
and now it's asking for Strings, so I say "abc" and it is also working now it is asking
for structures, I say
"1" and "100" and it is also working so we can say that
all the data which we want it, that is character string
or structure we can read and write from the file
I hope so, I have cleared the queries regarding the file I/O that is
to do different types of operation or different data types.
Không có nhận xét nào:
Đăng nhận xét