• CPE 1102L: Programming Logic and Design
Structures
Unit 8
elf © 2018
Structure
• Is a collection of variables that are referenced
under one name, providing a convenient
means of keeping related information together.
• Structure elements
• Variables that make up the structures
• Uses the keyword struct which tells the
compiler that a structure is being declared
General form of a structure declaration:
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
• Individual structure elements are referenced by
using the dot (.) operator
• General form:
• structure_name.element_name
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
Basic File Handling
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
“r” Open a text file for reading
“w” Create a text file for writing
“a” Append to a text file
“rb” Open a binary file for reading
“wb” Create a binary file for writing
“ab” Append to a binary file
“r+” Open a text file for read/write
“w+” Create a text file for read/write
“a+” Open or create a text file for read/write
Legal Values for Mode
“r+b” Open a binary file for read/write
“w+b” Create a binary file for read/write
“a+b” Open or create a binary file for read/write
“rt” Open a text file for reading
“wt” Create a text file for writing
“at” Append a text file
“r+t” Open a text file for read/write
“w+t” Create a text file for read/write
“a+t” Open or create a text file for read/write
Common Buffered File System Functions
putc( ) : used to write characters to a stream
previously opened for writing
int putc(int ch, FILE *fp);
getc( ) : used to read characters from a opened
stream in read mode
int getc (FILE *fp);
Common Buffered File System Functions
fopen( ) : opens a stream, links a file to it, and
returns a file pointer to that stream
FILE *fopen(const char *filename,
const char *mode);
• Filename : a string of characters that provides
a valid name & may include a path
• Mode : points a string containing the desired
open status
Common Buffered File System Functions
fclose( )
• used to close a previously opened stream
int fclose(FILE *fp);
Common Buffered File System Functions
fread() : reads data from the stream
size_t fread ( void *buffer, size_t
num_bytes, size_count, FILE *fp);
fwrite() : writes to a stream
size_t fwrite ( void *buffer, size_t
num_bytes, size_count, FILE *fp);
• 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
feof( ) : used to determine the end of file when reading binary
data
int feof (FILE *fp);
fseek() : positions the file pointer of a stream
int fseek(FILE *fp, long num_bytes, int
origin);
• num_bytes: number of bytes from origin to seek to
• origin :
Seeks from beginning of file SEEK_SET
Seeks from current position SEEK_CUR
Seeks from end of file SEEK_END
Common Buffered File System Functions
rewind( ) : resets the file position locator to the
beginning of the file
void rewind(FILE *fp);
rename() : renames a file
int rename (const char * oldname, const char *newname);
remove() : deletes a file, it’s a macro that translate
to a call to unlink
remove (filename);
Example of File Handling