Chapter Six Structure and Seven-File
Chapter Six Structure and Seven-File
Computer Programming
Chapter Six:
Structures
6/14/2023 1
6.1. Introduction
• A Structure is a collection of simple variables.
• The Variables in a structure can be of different types.
• Some can be int, some can be float, and so on.
• The data items in a structure are called the members of the structure.
• Declaration of structure
• To define a structure, you must use the struct statement.
• The struct statement defines a new data type, with more than one
member.
6/14/2023 2
1
6/14/2023
6/14/2023 3
2
6/14/2023
6/14/2023 5
• #include <iostream>
• #include <string.h>
• using namespace std;
• struct Books
• {
char title[50];
char author[50];
char subject[100];
int book_id;
• };
• int main( )
• {
struct Books Book1; // Declare Book1 of type Book
struct Books Book2; // Declare Book2 of type Book
• // book 1 specification
6/14/2023 6
3
6/14/2023
Book1.book_id = 6495407;
// book 2 specification
Book2.book_id = 6495700;
• // Print Book1 info
6/14/2023 7
4
6/14/2023
5
6/14/2023
6/14/2023 11
6
6/14/2023
• Now, you can store the address of a structure variable in the above defined pointer variable.
To find the address of a structure variable, place the & operator before the structure's name
as follows:
• struct_pointer = &Book1;
• To access the members of a structure using a pointer to that structure, you must use the
(Arraw)-> operator as follows:
• struct_pointer->title;
6/14/2023 14
7
6/14/2023
Chapter Seven
File Operations (File Input/output)
• A file is a collection of related data stored in a particular area on the disk.
• Programs can be designed to perform read or write operations on these
files.
• This involves two kinds of data communication:
1. Data transfer between the console unit and the program
2. Data transfer between the program and a disk file.
➢ The source stream that provides data to the program is called input stream and the destination
stream that receives output from the program is called the output stream.
➢ A program extracts the bytes from an input stream and inserts bytes into an output stream.
Extraction from
input stream
Input
Device
Input stream
Program
Output
Device Insertion into
Output stream output stream.
6/14/2023 Figure: Data streams 16
8
6/14/2023
ios
pointer
istream
streambuf ostream
input
output
iostream
9
6/14/2023
6/14/2023 19
10
6/14/2023
6/14/2023 22
11
6/14/2023
Closing a file
6/14/2023 23
• #include <iostream>
• #include <fstream>
• using namespacestd;
• int main () {
ofstream myfile ("example.txt");
if(myfile.is_open())
{
myfile << "This is a line.\n";
myfile << "This is another line.\n";
myfile.close();
}
else
cout << "Unable to open file";
return 0;
•}
6/14/2023 24
12
6/14/2023
• #include <iostream>
• #include <fstream>
• #include <string>
• using namespacestd;
int main () {
string line;
ifstream myfile ("example.txt");
if(myfile.is_open())
{
while(! myfile.eof() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}
elsecout << "Unable to open file";
return 0;
• } 6/14/2023 25
13