Johra Muhammad Moosa: Prepared by Cse, Buet Modified by
Johra Muhammad Moosa: Prepared by Cse, Buet Modified by
Johra Muhammad Moosa: Prepared by Cse, Buet Modified by
Computer Programming
File
Prepared by
Johra Muhammad Moosa
Assistant Professor
CSE, BUET
Modified by
Madhusudan Basak
Assistant Professor
CSE, BUET
Stream
• Abstraction between hardware and programmer
• Actual device providing I/O is file
– File can refer to disk file, screen, keyboard, memory, port, tape file or others
• Stream is a logical interface to a file
• When a C program started operating system opens three files and
associated streams
– Standard input: stdin
– Standard output: stdout
– Standard error: stderr
– Declared in stdio.h
Types of Streams (Files)
• Text
– Some character translations may take place
– There may not be one-to-one correspondence between what is sent to stream
and what is written to the file
• Binary
– No character translation will occur
– There is one-to-one correspondence
Open a File
FILE *fopen (char *filename, char *mode);
• Stdio.h
• Filename
– path
• Mode
– “r”, “w”, “a”, “r+”, “w+”,”a+”
– “rb”,”wb”, “ab”, “rb+”,”wb+”,”ab+”
– Consequence of those modes
• Returns a pointer to the opened file
• If fails returns NULL
Mode
• Read mode ("r")
– if file is unavailable error
• Write mode (“w")
– if file is unavailable created
• Append mode (“a")
– if file is unavailable created
Close a File
int fclose (FILE *fp);
• In order to improve efficiency most file system write data to disk one
sector at a time
• fclose flushes the buffer
Read/Write a Character
int fgetc (FILE *fp);
- Normal: reads the next byte from the file and returns it as integer
- If error/file reading ends: returns EOF
– fgetc(stdin) same as getchar()
See file_fgetc_fputc.c
EOF
• To denote end of file or
• An integer defined in stdio.h
• Value is different than any character values, normally is it -1
• Given by ctrl+z (if you want to manually give it)
• Used in file
• Used when input taken in continuous loop
End of File
int feof (FILE *fp);
– Returns non zero if fp reached end of file
– Returns 0 otherwise
See file_fprintf_fscanf_1.c
and file_fprintf_fscanf_2.c
Writing and reading in binary file
size_t fread (void *buffer, size_t size, size_t num, FILE *fp);
Size_t is a type able to represent the size of any object in bytes. It is guaranteed to be big enough to
contain the size of the biggest object the host system can handle. For simplicity, in general
setting, you can consider it as equivalent to unsigned long int (though may depend on
operating system properties)
• buffer: where the output of fread will be stored
• size: size of the object to be read
• num: number of the objects to be read
• fp: the stream of the source file from where we want to read
size_t write (void *buffer, size_t size, size_t num, FILE *fp);
• buffer: address of the memory which is the start address to write
• size: size of the object to be written
• num: number of the objects to be written
• fp: the stream of the destination file where we want to write
See file_fread_fwrite.c
freopen
FILE *freopen(const char *filename, const char *mode, FILE *stream)
•associates a new filename with the given open stream and at the same time
closes the old file in the stream.
For example,
freopen(“data_input4.txt","r",stdin);
stdin was the default stream originally associated with console (console is
considered as a file). After the freopen function call, stdin has been used to
open a new stream to file “data_input4.txt”. So, scanf will read its input
from “data_input4.txt”.
See file_freopen.c
Self-study
Random access in File
-fseek
-ftell
Reference : 9.6 of Teach Yourself (Herbert Schildt) – 3rd Edition
File Operations
• File handle : FILE *
• Open a file : fopen
• Input/Output
– Character IO : getc, putc
– String IO : fgets, fputs
– Formatted IO : fscanf, fprintf
– Raw IO : fread, fwrite
• Close a file : fclose
• Other operations :
– freopen, fseek, ftell
Sample Exercise
• Suppose, you are given two input files named “data_input1.txt” and
“data_input2.txt”. In both of the files, a single line contains an integer number.
Both the files contain same number of integers. There will be an output file named
“data_output.txt”. You have to take the 1st integer of input file 1 and the 1st integer
of input file 2, and output their summation in the 1 st line of the output file. Then
take the 2nd integers from both the inputs files and output their summation in the
second line of output file, and so on up to the end of the input files. Write the full C
code for this.
1st input file 2nd input file Corresponding Try yourself first. Then
sample data sample data output file
sample data see file_sample_exercise.c
2 100 102
4 4 8
-2 98 96
4 -100 -96
8 -8 0
References
• Teach Yourself C by Herbert Schildt (3rd Edition)
– Chapter 9 (9.1 – 9.6)
• https://www.geeksforgeeks.org/basics-file-handling-c/
Thank You