Please enable JavaScript.
Coggle requires JavaScript to display documents.
Lecture 3 - Coggle Diagram
Lecture 3
Files and operation
NOTE
w and a mode, file does not exist, auto create
w mode, name file exists, content overwritten
several files open at same time
FILE *fp; fp=fopen("filename","mode");
filename:sting
fopen :return pointer to a file used in subsequent file operation
fp :pointer to data type FILE
Modes of opening
ab: binary file in write mode, pter to end of file
r+ : read/write mode, if file does not exist, will not be created
wb: binary file in write mode, beginning of file
w+: read/write mode, set pter to beginning of file
rb: open a bianary file in read mode, set a pointer to the beginning of file
a+: in read/append mode
a : write mode, end of file
r+b: open binary file in read.write mode. File does not exist, it will not be created.
w: write mode, beginning
w+b: binary file to read/wite mode, set pter to beginnig of file
r :read mode, set pointer to beginning of file
a+b :open a binary file to read/append mode
fille closing
all operation has been completed, must be closed
ensure all file data r written onto the file
gen format FILE *xyz xyz = fopen(text.txt:, "w"); fclose(xyz);
fclose (FILE pointer)
good pract to close file explicitly
syst resources are freed
auto when prog ends
not sure if info has been written to disk until closed
Read and wrtie oprtn
file version of scanf and printf gen format fscanf(filepter, string, list); fprintf( );
example fscanf(fp,"%d %f %f",&a,&x,&y);
f[rintf(out,"\nThe result is :%d",xyz);
nesting structure
struct Date { int dayl int month; };
mem operator r nested st[0]birthday.year=
File handling in C
why
some prog requires same set of data to be fed as input
prog generating lare vol output
data stored permanently
data files
usually consist text: text fiile
created, updated, processed by c prog
permanent storage of large amount
File and stream
c view: seq of bytes file ends with end- of -file markers
stream created when file opened
provides communiaction channels between files and prog
open a file = return a pointer to a file structure
Struct type arryas
need more struct type var.
typedef
struct {
float.real;
float.imag;}CPX;
CPX c[100];
access c[32].real data member.array ele