Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

File Handling – Additional Functions from stdio.

h
Here is a list of functions that we normally use for file handling
fscanf()

The C library function int fscanf(FILE *stream, const char *format, ...) reads formatted
input from a stream.
Declaration

Following is the declaration for fscanf() function.


int fscanf(FILE *stream, const char *format, ...)
Parameters
stream − This is the pointer to a FILE object that identifies the stream.
format − This is the C string that contains one or more of the following items −Whitespace
character, Non-whitespace character and Format specifiers. #include <stdio.h>. The
formats are similar to scanf() function.
Example

#include <stdio.h>

#include <stdlib.h>

int main ()

char str1[10], str2[10], str3[10];

int year;

FILE * fp;

fp = fopen ("file.txt", "w+");

fputs("We are in 2012", fp);

rewind(fp);
fscanf(fp, "%s %s %s %d", str1, str2, str3, &amp;year);

printf("Read String1 |%s|\n", str1 );

printf("Read String2 |%s|\n", str2 );

printf("Read String3 |%s|\n", str3 );

printf("Read Integer |%d|\n", year );

fclose(fp);

return(0);

Output will be

Read String1 |We|

Read String2 |are|

Read String3 |in|

Read Integer |2012|

fprintf()
The C library function int fprintf(FILE *stream, const char *format, ...) sends formatted output to a stream.
Declaration
int fprintf(FILE *stream, const char *format, ...)
Parameters
stream − This is the pointer to a FILE object that identifies the stream.
format − This is the C string that contains the text to be written to the stream.
Example
#include <stdio.h>

#include <stdlib.h>

int main ()
{
FILE * fp;
fp = fopen ("file.txt", "w+");

fprintf(fp, "%s %s %s %d", "We", "are", "in", 2012);

fclose(fp);

return(0);

Output

We are in 2012

fgetc()
The C library function int fgetc(FILE *stream) gets the next character (an unsigned char) from the specified stream
and advances the position indicator for the stream.
Declaration
Following is the declaration for fgetc() function.
int fgetc(FILE *stream)
Parameters
stream − This is the pointer to a FILE object that identifies the stream on which the operation is to be performed.
Return Value
This function returns the character read as an unsigned char cast to an int or EOF on end of file or error.
Example
The following example shows the usage of fgetc() function.

#include <stdio.h>
int main ()

FILE *fp;

int c;

int n = 0;

fp = fopen("file.txt","r");

if(fp == NULL)

perror("Error in opening file");

return(-1);

do

c = fgetc(fp);

if( feof(fp) )

break ;
}

printf("%c", c);

while(1);

fclose(fp);

return(0);

The program assumes that we have a text file file.txt, which has the following content.
This file will be used as an input for our example program −
We are in 2012
Output

We are in 2012

fputc()
The C library function int fputc(int char, FILE *stream) writes a character (an unsigned char) specified by the
argument char to the specified stream and advances the position indicator for the stream.
Declaration
int fputc(int char, FILE *stream)
Parameters
char − This is the character to be written. This is passed as its int promotion.
stream − This is the pointer to a FILE object that identifies the stream where the character is to be written.
Return Value
If there are no errors, the same character that has been written is returned. If an error occurs, EOF is returned and the
error indicator is set.
Example
The following example shows the usage of fputc() function.
#include <stdio.h>

int main ()
{
FILE *fp;
int ch;
fp = fopen("file.txt", "w+");
for( ch = 33 ; ch <= 100; ch++ )
{
fputc(ch, fp);
}

fclose(fp);
return(0);
}

Output
On running the above program that will create a file file.txt in the current directory, which will have
following content −

!&quot;#$%&amp;&#39;()*+,-
./0123456789:;&lt;=&gt;?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcd

fgets()
The C library function char *fgets(char *str, int n, FILE *stream) reads a line from the specified stream and stores it
into the string pointed to by str. It stops when either (n-1) characters are read, the newline character is read, or the
end-of- file is reached, whichever comes first.
Declaration
char *fgets(char *str, int n, FILE *stream)
Parameters
str − This is the pointer to an array of chars where the string read is stored.
n − This is the maximum number of characters to be read (including the final null-character). Usually, the length of
the array passed as str is used.
stream − This is the pointer to a FILE object that identifies the stream where characters are read from.
Return Value
On success, the function returns the same str parameter. If the End-of- File is encountered and no characters have
been read, the contents of str remain unchanged and a null pointer is returned. If an error occurs, a null pointer is
returned.
Example

#include <stdio.h>

int main ()
{
FILE *fp;
char str[60];

/* opening file for reading */


fp = fopen("file.txt" , "r");
if(fp == NULL)
{
perror("Error opening file");
return(-1);
}

if( fgets (str, 60, fp)!=NULL )


{

/* writing content to stdout */


puts(str);
}

fclose(fp);
return(0);
}

We have to have a text file file.txt, let us suppose the following content exists in it. This file will be
used as an input for our example program –
We are in 2012
fputs()
The C library function int fputs(const char *str, FILE *stream) writes a string to the specified stream up to but not
including the null character.
Declaration
int fputs(const char *str, FILE *stream)
Parameters
str − This is an array containing the null-terminated sequence of characters to be written.
stream − This is the pointer to a FILE object that identifies the stream where the string is to be
written.
Return Value
This function returns a non-negative value, or else on error it returns EOF.
Example
The following example shows the usage of fputs() function.

#include <stdio.h>

int main ()
{
FILE *fp;
fp = fopen("file.txt", "w+");

fputs("This is c programming.", fp);


fputs("This is a system programming language.", fp);
fclose(fp);
return(0);
}

Output
This will create a file file.txt with the following content −

This is c programming.This is a system programming language.

fread() and fwrite()


The C library function size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) reads data from the given
stream into the array pointed to, by ptr. Similarly function size_t fwrite(const void *ptr, size_t size, size_t nmemb,
FILE *stream) writes data from the array pointed to, by ptr to the given stream.
Declaration
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
Parameters
ptr − This is the pointer to a block of memory with a minimum size of size*nmemb bytes.
size − This is the size in bytes of each element to be read.
nmemb − This is the number of elements, each one with a size of size bytes.
stream − This is the pointer to a FILE object that specifies an input stream.
Return Value
The total number of elements successfully read are returned as a size_t object, which is an integral data type. If this
number differs from the nmemb parameter, then either an error had occurred or the End Of File was reached.
Example
The following example shows the usage of fread() function.

#include <stdio.h>

#include <string.h>

int main ()

{
FILE *fp;
char c[] = "this is Internshala";
char buffer[100];

/* Open file for both reading and writing */


fp = fopen("file.txt", "w+");

/* Write data to the file */


fwrite(c, strlen(c) + 1, 1, fp);

/* Seek to the beginning of the file */


fseek(fp, 0, SEEK_SET);

/* Read and display data */


fread(buffer, strlen(c)+1, 1, fp);
printf("%s\n", buffer);

fclose(fp);
return(0);

Let us compile and run the above program that will create a file file.txt and write a content “this is Internshala”.
After that, we use fseek() function to reset writing pointer to the beginning of the file and prepare the file content.
Then fread() function is used for reading the same and the content is printed on the display.
Output

this is Internshala

fwrite()

You might also like