0% found this document useful (0 votes)
40 views13 pages

Chapter Six Structure and Seven-File

The document discusses structures in C++ programming, including defining and accessing structure variables, passing structures as function arguments, arrays of structures, pointers to structures, and file input/output operations using stream classes. Structures allow grouping of different data types under one name and are a user-defined data type in C++.

Uploaded by

wubie derebe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views13 pages

Chapter Six Structure and Seven-File

The document discusses structures in C++ programming, including defining and accessing structure variables, passing structures as function arguments, arrays of structures, pointers to structures, and file input/output operations using stream classes. Structures allow grouping of different data types under one name and are a user-defined data type in C++.

Uploaded by

wubie derebe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

6/14/2023

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

The format of the struct statement is:

6/14/2023 3

• struct [structure tag]


• {
member definition;
member definition;
...
member definition;
} [one or more structure variables];
• Example specifies one or more structure variables but it is optional. Here is the way you
would declare the Book structure:
• struct Books
• {
char title[50];
char author[50];
char subject[100];
int book_id;
• }book1, book2;
6/14/2023 4

2
6/14/2023

6,3 Accessing of structure

• To access any member of a structure, we use the member access


operator (.).

• The member access operator is coded as a period between the structure


variable name and the structure member that we wish to access.

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

strcpy( Book1.title, "Learn C++ Programming");

strcpy( Book1.author, "Ravichandriman");

strcpy( Book1.subject, "C++ Programming");

Book1.book_id = 6495407;

// book 2 specification

strcpy( Book2.title, "Data structures and Algorthims");

strcpy( Book2.author, "Adam d.");

strcpy( Book2.subject, "Algorthim");

Book2.book_id = 6495700;
• // Print Book1 info

6/14/2023 7

cout << "Book 1 title : " << Book1.title <<endl;


cout << "Book 1 author : " << Book1.author <<endl;
cout << "Book 1 subject : " << Book1.subject <<endl;
cout << "Book 1 id : " << Book1.book_id <<endl;
// Print Book2 info
cout << "Book 2 title : " << Book2.title <<endl;
cout << "Book 2 author : " << Book2.author <<endl;
cout << "Book 2 subject : " << Book2.subject <<endl;
cout << "Book 2 id : " << Book2.book_id <<endl;
return 0;
}
6/14/2023 8

4
6/14/2023

6.4 Functions and structures


• You can pass a structure as a function argument in very similar way as you
pass any other variable or pointer
• Example
#include <iostream>
#include <cstring>
using namespace std;
void printBook( struct Books book );
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
6/14/2023
// book 1 specification 9

strcpy( Book1.title, "Learn C++ Programming");


strcpy( Book1.author, "Chand Miyan");
strcpy( Book1.subject, "C++ Programming");
Book1.book_id = 6495407;
// book 2 specification
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Yakit Singha");
strcpy( Book2.subject, "Telecom");
Book2.book_id = 6495700;
// Print Book1 info
printBook( Book1 );
// Print Book2 info
printBook( Book2 );
return 0;
}
6/14/2023 10

5
6/14/2023

void printBook( struct Books book )

cout << "Book title : " << book.title <<endl;

cout << "Book author : " << book.author <<endl;

cout << "Book subject : " << book.subject <<endl;

cout << "Book id : " << book.book_id <<endl;

6/14/2023 11

6.5 Arrays of structures


• Example
#include<iostream.h>
#include<conio.h>
struct student {
int id;
char name[15];
};
void main()
{
//creating 5 student using an array
student s[5];
int i;
6/14/2023 12

6
6/14/2023

for(i=0; i < 5; i ++)


{
cout<<"\n Enter Student Id";
cin>>s[i].id;
cout<<"\nEnter Name";
cin>>s[i].name;
}
cout<<"\n Displaying student Info";
cout<<"\nStudent Id \t Student Name";
cout<<"\n============================";
for( i = 0; i < 5; i++)
cout<<endl<<s[i].id<<"\t\t\t"<<s[i].name;
return 0;
}
6/14/2023 13

6.6 Pointers and structures


• You can define pointers to structures in very similar way as you define pointer to any other
variable as follows:

• struct Books *struct_pointer;

• 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.

• A file can be defined by following class ifstream, ofstream, fstream, all


these are defined in fstream.h header file.
• If a file object is declared by ifstream class, then that object can be used
for reading from a file.
• If a file object is declared by ofstream class, then that object can be used
for writing onto a file.
• If a file object is declared by fstream class then, that object can be used
6/14/2023
for both reading from and writing to a file 15

Stream Classes and I/O operations


➢ In C++, I/O system supplies an interface to the programmer that is independent of the actual
device (terminals, disks, tape drives) being accessed.
➢ This interface is known as stream.
➢ A stream is a sequence of bytes. It acts as either as a source from which the input data can be
obtained or as a destination to which the output data can be sent.

➢ 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

istream_withassign iostream_withassign Ostream_withassgin

Figure :Stream classes for console I/O operations


6/14/2023 17

➢ ios : General input/output stream class.


➢ Contains basic facilities that are used by all other input and output classes.
➢ Declares constants and functions that are necessary for handling formatted input
and output operations.
➢ istream(input stream): Inherits the properties of ios.
➢ Declares input functions such as get(), getline() and read().
➢ Contains overloaded extraction operator >>
➢ ostream(output stream): Inherits the properties of ios.
➢ Declares output functions such as put() and write(). Contains overloaded
insertion operator <<
➢ iostream(input/output stream): Inherits the properties of ios, istream and ostream
6/14/2023 18

9
6/14/2023

Table File mode parameters


parameter Meaning

ios:: app Append to end-of-file


ios::ate Go to end-of-file on opening

ios::binary Binary file


ios::in open file for reading only
ios::nocreate Open fails if the file does not exist
ios::noreplace Open fails if the file already exists
ios::out Open file for writing only
ios::trunc Delete contents of the file if it exists

6/14/2023 19

File basic operation open file


#include <iostream>
#include <fstream>
using namespacestd;
int main () {
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile.close();
return0;
}
6/14/2023 20

10
6/14/2023

Writing this to a file


• This code creates a file called example.txt and inserts a
sentence into it in the same way we are used to do with cout,
but using the file stream myfile instead.
• Opening a file
• The first operation generally performed on an object of one
of these classes is to associate it to a real file.
• This procedure is known as to open a file.
• In order to open a file with a stream object we use its
member function open():
• open (filename, mode);
• ofstream myfile;
• myfile.open ("example.txt", ios::out | ios::app | ios::binary);
6/14/2023 21

A character can be written onto a file using the


put()function.
• #include<fstream.h>
• int main()
• {
• char c;
• ofstream outfile;
• outfile.open(“c:\\test.txt”,ios::out);
• if(outfile.fail())
• {
• cerr<< “\nError opening test.txt”;
• exit(1);
• }
• for(int i=1;i<=15;i++)
• {
• cout<< “\nEnter a character : ”; cin>>c;
• outfile.put(c);
• }
• output.close();
• }//end main

6/14/2023 22

11
6/14/2023

Closing a file

• When we are finished with our input and output operations on


a file we shall close it so that its resources become available
again.
• In order to do that we have to call the stream's member
function close().
• myfile.close();
• Once this member function is called, the stream object can be
used to open another file, and the file is available again to be
opened by other processes.

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

You might also like