08 - StructuresAndFileHandling
08 - StructuresAndFileHandling
08 - StructuresAndFileHandling
Structures
Unit 8
elf © 2018
Structure
• Structure elements
• Variables that make up the structures
struct structure_tag_name{
type variable_name;
type variable_name;
type variable_name;
.
.
.
} structure_variables;
Structure declaration example
1)
struct stud {
char id[10];
char name[30];
char course[10];
int year;
};
struct stud stud_info;
Structure declaration example
2)
struct stud {
char id[10];
char name[30];
char course[10];
int year;
} stud_info, sdata;
Structure declaration example
3)
struct {
char id[10];
char name[30];
char course[10];
int year;
} stud_info;
Referencing Structure Elements
Example
gets(stud_info.id); printf(“%s”,stud_info.id);
gets(stud_info.name); printf(“%s”,stud_info.name);
gets(stud_info.course); printf(“%s”,stud_info.course);
scanf(“%d”,&stud_info.year); printf(“%d”,stud_info.year);
Array of Structures
#define MAX 20
struct stud {
char id[10];
char name[30];
char course[10];
int year;
};
struct stud stud_info[MAX];
Array of Structures
gets(stud_info[2].id);
gets(stud_info[2].name);
gets(stud_info[2].course);
scanf(“%d”,&stud_info[2].year);
printf(“%s”,stud_info[5].id);
printf(“%s”,stud_info[5].name);
printf(“%s”,stud_info[5].course);
printf(“%d”,stud_info[5].year);
Example on Structure Array
• CpE 1102L: Programming Logic and Design
Unit 8
elf © 2018
Stream
a device independent abstraction of an actual
device
2 Types 5 Predefined Streams
• Text Stream • stdin Keyboard
• a sequence of characters • stdout Screen
possibly not having a 1-to-1
correspondence with the • stderr Screen
actual characters found on
the device. • stdaux First Serial Port
• Binary Stream • stdprn Printer
• a sequence of bytes having a
1-to-1 correspondence to
those found on the device
File
• an actual device
• File Pointer
• a pointer to the info that defines various things
about a file
FILE *fp;
Legal Values for Mode
fclose( )
• used to close a previously opened stream
int fclose(FILE *fp);
Common Buffered File System Functions
• buffer: pointer to the region in memory that receives the data read
from a file; pointer to the data to be written to the file
• num_bytes : length in bytes of the items read/written
• size_count : determine how many items of num-bytes are to be
read / written
Common Buffered File System Functions