Sie sind auf Seite 1von 13

Reading and Writing Text Files though input and output streams

1
Block Input and Output

2
File Modes

3
File-Opening Modes

4
File States

5
Types of Standard Input/Output Functions

6
 Binary files can be written in blocks
 The extension for binary files would be .bin
 The function for reading from a binary file is
fread() and the function for writing into binary
files is fwrite()
 The different modes for opening a binary file are
same as opening text files, with an added ‘b’:
 rb, wb, ab, rb+, wb+, ab+ which are equivalent
to r,w,a,r+,w+,a+ of text files respectively

7
Syntax for fread:
 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.

Parameters
 ptr − This is the pointer to a block of memory
 size − This is the size in bytes of each element to be read.
 nmemb − This is the number of elements to be read, 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.

8
Block - File Read Operation

9
Syntax for fwrite:
 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.

Parameters
 ptr − This is the pointer to the array of elements to be written.
[ e.g.: int *ptr=(int *)malloc(40) ]
 size − This is the size in bytes of each element to be written.
 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 output stream.

Return Value
 This function returns the total number of elements successfully returned as a size_t
object, which is an integral data type. If this number differs from the nmemb
parameter, it will show an error.

10
Block - File Write Operation

11
Note

Formatted input/output, character input/output, and string


input/output functions can be used only with text files.

Text files store data as a sequence of characters; binary files


store data as they are stored in primary memory.

clearerr(fp) : error status of the file pointer is reset.


Otherwise, once an error occurs, the subsequent calls to
ferror(fp) return on-zero.

12
Example for fread and fwrite

#include<stdio.h>
int main()
{
char str[]="Welcome to programming!";
FILE *fp=fopen("Test.txt","w");
fwrite(str,1,18,fp); // write a block of 18 units each of 1 byte from str to file
fclose(fp);

fp=fopen("Test.txt","r");
fseek(fp,0L,2); //go to end of the file
int n=ftell(fp);

printf("Number of characters in the file: %d\n",n);


rewind(fp); //go the beginning of the file
putchar(fgetc(fp));
char arr[n];
fread(arr,1,n,fp); //read n values each of 1 byte from the file to arr
fclose(fp);
puts(arr);
return 0;
}

13

Das könnte Ihnen auch gefallen