Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 27

fgets()

• The fgets() function stands for file get string. The fgets()
function is used to get a string from a stream.
• Syntax:
char *fgets(char *str , int size, FILE *stream);
• The fgets() function reads at most one less than the number
of characters specified by size(gets size -1 characters) from
the given stream and stores them in the string str.
• The fgets() function terminates as soon as it encounters either
a newline character, EOF, or any other error.
• When all the characters are read without any error, a ‘\0’
character is appended to the end of the string.
• C
• On successful completion, fgets() will return str.
• However, if the stream is at EOF, the EOF
indicator for the stream will be set and fgets will
return a NULL pointer.
• In case, fgets() encounters any error while
reading, the error indicator for the stream will
be set and NULL pointer will be returned.
fgetc()
• The fgetc() function returns the next character
from stream, EOF- if the end of file is reached,
or if there is an error.
• Syntax:
int fgetc(FILE *stream);
• fgetc() reads a single character from the current
position of a file.
• After reading the character, the function
increments the associated file pointer to point to
the next character.
• If the stream has already reached the end of file,
the EOF indicator for the stream is set.
#include <stdio.h>
int main()
fread()
• The fread() function is used to read data from a
file.
• Syntax:
int fread(void *str, size_t, size, size_t num,
FILE *stream);
• The fread() function reads number of objects
and places them into the array pointed to by str.
• The data is read from the given input stream.
• Upon successful completion, fread() returns
the number of bytes successfully read.
• If size or num is 0, fread() will return 0 and the
contents of str and the state of the stream
remain unchanges.
• In case of error, the error indicator for the
stream will be set.
Writing Data to Files
• C provides the following set of functions to
read data from a file.
1. fprintf()
2. fputs()
3. fputc()
4. fwrite()
fprintf()
• The fprintf() function is used to write formatted
output to stream.
• Syntax:
int fprintf(FILE *stream, const char * format, …);
• The function writes data that is formatted as specified
by the format arguments to the specified stream.
• After the format parameter, the function can have as
many additional arguments as specified in the format.
• The parameter format in fprintf() is nothing but a C
string that contains the text that has to be written on
to the stream.
• Although not mandatory, fprintf() can optionally
contain format tags that are replaced by the values
specified in subsequent additional arguments and are
formatted as requested.
• The prototype of the format tag can be given as
%[flags][width][.precision][length] specifier
Each format specifier must begin with a % sign. The %
sign is followed by:
Flags which specifies output justification such as decimal
point, numerical sign, trailing zeros, or octal or
hexadecimal prefixes.
Flags in printf()
• - : left justify within the given data field width.
• + : Displays the data with its numeric
sign(either + or -)
• # : used to provide additional specifiers such
as o, x, X, O, ox, or OX for octal and hexadecimal
values, respectively, for values except zero.
• O : the number is left-padded with zero(0)
instead of spaces.
• Width specifies the minimum number of
characters to print after being padded with
zeros or blank spaces.
• Precision specifies the maximum number of
characters to print
1. for integer specifiers(d, I,o,u,x,X):
precision flag specifies the minimum number
of digits to be written.
2. for character strings, precision specifies
the maximum number of characters to be
printed.
• Specifier is used to define the type and the
interpretation of the value of the
corresponding arguments.
• %d to specify a signed decimal integers.
• %-10.10s. Here- indicates that the characters
must be left aligned. There can be a minimum
0f 10 characters as well as a maximum of 10
characters(.10) in the strings.
• %f to specify a floating point number.
fputc()
• The fputc() function is just the opposite of fgetc()
and is used to write a character to the stream.
• Syntax: int fputc(int c, FILE *stream);
• The fputc() function will write the byte specified by C
to the output stream pointed to by stream.
• On successful completion, fputc() will return the
value it has written.
• Otherwise in case of error, the function will return
EOF and the error indicator for the stream will be
set.
fputs()
• The fputs() function is used to write a line to a
file.
• Syntax: int fputs(const char *str, FILE *stream);
• On successful completion, fputs return 0, in case
of any error, fputs() returns EOF.
#include<stdio.h>
main()
{
FILE *fp;
char feedback[100];
fp=fopen(“comments.TXT”,”w”);
If(fp==NULL)
{
printf(“\n the file could not be opened”);
exit(1);
}
printf(“\n provide feedback on this book:”);
gets(feedback);
fflush(stdin);
fputs(feedback, fp);
fclose(fp);
}
Output:
Provide feedback on this book:good
fwrite()
• The fwrite() function is used to write data to a file.
• Syntax: int fwrite(const void *str, size_t size, size_t count, FILE *stream);
• The fwrite () function will write objects of size specified by size, from the array pointed to
by str to the stream pointed to by stream.
• The file position indicator for the stream will be advanced by the number of bytes
successfully written.
• If the error occurs, the resulting value of the file-position indicator for the stream is
unspecified.
• On successful completion, the fwrite() function returns the number of objects successfully
written.
• If an error occurs, the resulting value of the file position indicator for the stream is
unspecified.
• On successful completion, the fwrite() function, returns the number of objects successfully
written.
• The number of objects will be less than count if an error is encountered.
• If size or count is ), fwrite() will return 0 and the contents of the stream remains unchanged.
• In case of error, the error indicator for the stream will be set.
• To write to a binary file, you need to use the
function fwrite(). The functions takes four
arguments: Address of data to be written in
disk, Size of data to be written in disk, number
of such type of data and pointer to the filed
where you want to write.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main()
{
FILE *fp;
Size_t count;
Char str[]=“good morning”;
fp=fopen(“welcome.txt”, ”wb”);
If(fp==NULL)
{
printf(“\n the file could not be opened”);
exit(1);
}
Count=fwrite(str, 1, strlen(str), fp);
Printf(“\n %d bytes were written to the file”, count);
fclose(fp);
return 0;
}
Output:
13 bytes were written to the file
DETECTING THE END-OF-FILE
• When reading or writing data to files, we often do not know exactly how long the file is. For example,
while reading the file, we usually start reading from the beginning and proceed towads the end of the
file.
• In C, there are two ways to detect EOF:
1. while reading the file in text mode, character by character, the programmer can compare
the character that has been read with EOF, which is a symbolic constant defined in stdio.h with a
value -1.
while(1)
{
c= fgetc(fp);
if(c==EOF)
break;
printf(“%c”, c);
}
2. the other way is to use the standard library function feof() which is defined in stdio.h .
The feof() function is used to distinguish between two cases:

. When a stream operation has reached the end of a file


. When the EOF error code has returned an error indicator even when the
end of the file has not been reached.
• Syntax: int feof(FILE *fp);
• The function takes a pointer to the FILE structure of the
stream to check as an argument and returns zero when the
end of file has not been reached and a one if the end of file
has been reached.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main()
{
FILE *fp;
Char str[80];
fp=fopen(“student.DAT”, ”r”);
if(fp==NULL)
{
printf(“\n the file could not be opened”);
exit(1);
}
While(!feof(fp))
{
fgets(str, 79, fp);
Printf(“\n %s”, str);
}
Printf(“\n\n file read. Now closing the file”);
fclose(fp);
return 0;
}

You might also like