0% found this document useful (0 votes)
245 views

1664651116711C With C - Training Report - 40 Pages

The document provides details about a 6-week industrial training completed in C and C++ at YCT Academy, Bhopal. It includes an introduction to the training, the organization where it took place, and the topics covered over 4 modules - including the basics of C like variables, data types, operators; control statements; functions; arrays; pointers; files in C++ like classes, objects, inheritance and polymorphism. The training aimed to provide practical exposure and enhance the students' understanding of C and C++ concepts.

Uploaded by

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

1664651116711C With C - Training Report - 40 Pages

The document provides details about a 6-week industrial training completed in C and C++ at YCT Academy, Bhopal. It includes an introduction to the training, the organization where it took place, and the topics covered over 4 modules - including the basics of C like variables, data types, operators; control statements; functions; arrays; pointers; files in C++ like classes, objects, inheritance and polymorphism. The training aimed to provide practical exposure and enhance the students' understanding of C and C++ concepts.

Uploaded by

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

SIX WEEK INDUSTRIAL TRAINING REPORT

ON

C with C++
COMPLETED AT
YCT Academy, BHOPAL

BY
xxxxxxxxxx
ROLL NO. 0110CSE15GT007

DEPARTMENT OF COMPUTER SCIENCE ENGINEERING


ABC INSTITUTE OF TECHNOLOGY, BHOPAL

Submitted To: Head of Dept: Submitted By:


xxxxxxxxxxxx
ACKNOWLEDGEMENT

It is with the great sense of satisfaction that we present our reveal


venture in training process in the form of project work. We are very fortunate to
have Mr.Pankaj Panjwani(CEO of YCT Academy) in YCT Academy as our
trainee and mentor.

While working on this project, their patience and valuable hints helped us in
sailing through the rough seas. They were generous enough to lend us their time,
experience and expertise.

We also acknowledge, with great thanks, our Head of the Centre and Program
Coordinator Mr.Pankaj Panjwani for their encouragement and suggestions that we
were provided at different stages while working on this project and all the staff
members of our department for all the help that they have extended to us.

Last but not the least, we are also thankful to all our friends without whose
help this process would have been unexpectedly protracted and the meeting of
deadlines would have been impossible.

xxxxxxxxxx
TABLE OF CONTENTS

1. Abstract ………….………………………………………… .
2. Introduction of Training……………………………………….
3. Introduction of the Organization..…………………………….
4. Training Details (with topics)………………………………….
Module 1: ………………………………………………

 What is C Language , History of C , First C Program


 Printf scanf , variables , datatypes , keyword , operators , escape sequence
 Control statements(if else , switch,loops,goto,ternary)
 Functions
 Array
 Pointers
 Dynamic Memory Allocation
 Structure , Union
 File Handling

Module 2:………………………………………………

 2.1 C++ Introduction


 2.2 C vs C++
 2.3 cout,cin,endl
 2.4 class and object
 2.5 Constructor
 2.6 Destructor

Module 3:………………………………………………….

 3.1 this pointer


 3.2 static data member and member function
 3.3 Inheritance
 3.4 Polymorphism
 3.5 Template

Module 4:………………………………………………

4.1 Files And Streams

5. Conclusion………………………………………………………..
6. Bibliography……………………………………………………..
1. ABSTRACT

This report discusses the training program at YCT Academy. It states the
concept of C and C++.

It shows a brief background about the place of training. It mentions some


facts about the department that was responsible for the training program.
The report describes about the history, characteristic, overview and
concepts of C and C++.
2. INTRODUCTION OF TRAINING

The purpose of industrial Training is to provide exposure for the students on practical
engineering fields. Through this exposure, students will have better understanding of engineering
practical in general and sense of frequent and possible problems. This training is part of learning
process. So the exposure that uplifts the knowledge and experience of a student needs to be
properly documented in the form of a report. Through this report, the experience gained can be
delivered to their peers. A properly prepared report can facilitate the presentation of the practical
experience in an orderly, precise and interesting manner.

I have chosen C and C++ as my training because it will help me in several ways. I have chosen
my languages course in C++ and by having little bit knowledge in this Subject will help me in
carrier growth. During learning this I came to understand much more concepts of other language.

2.1 Learning Outcomes from Training

General learning outcomes:


 An understanding of the principles and practice of c and c++ in the construction of softwares

 A competence to design, write, compile, test and execute straightforward programs using a high level
language;

 An awareness of the need for a professional approach to design and the importance of good
documentation to the finished programs

Specific learning outcomes: 


 Be able to implement, compile, test and run programs.
 
 Understand how to include our logic in a program.
Introduction of the Organization

YCT Academy an ISO 9001-2008 Certified Institute.

Description:

It is founded on 12th Aug 2014.YCT Academy also known as Your Computer Teacher Academy
providing best training to engineering candidate in almost all the branches ,so students gets
campus selected in various Organization like Microsoft, MicoBosch, IBM,L
&T ,Mahindra ,HCL,TCS,MUSIGMA,SINTEL,WIPRO INFOTECH. ETC.

Course Conducted by YCT Academy:

 .NET
 PHP
 C/C++
 CORE JAVA
 ADVANCED JAVA
 ORACLE
 ANDROID
 MINOR/MAJOR/LIVE PROJECTS
 EMBEDDED SYSTEM
 Soft Skills
 AUTOCAD
4. TRAINING CONTENT

Module 1
What is C Language

The C Language is developed for creating system applications that directly interact with the
hardware devices such as drivers, kernels, etc.

C programming is considered as the base for other programming languages, that is why it is
known as mother language.

History of C Language

C programming language was developed in 1972 by Dennis Ritchie at bell laboratories


of AT&T (American Telephone & Telegraph), located in the U.S.A.

Dennis Ritchie is known as the founder of the c language.

First C Program
To write the first c program, open the C console and write the following code:

1. #include <stdio.h>    
2. int main(){    
3. printf("Hello C Language");    
4. return 0;   
5. }  

printf() and scanf() in C


The printf() and scanf() functions are used for input and output in C language. Both functions are inbuilt library
functions, defined in stdio.h (header file).

Variables in C
A variable is a name of the memory location. It is used to store data. Its value can be changed, and it can be
reused many times.

It is a way to represent memory location through symbol so that it can be easily identified.

Let's see the syntax to declare a variable:

1. type variable_list;  
The example of declaring the variable is given below:

1. int a;  
2. float b;  
3. char c;  

Data Types in C

A data type specifies the type of data that a variable can store such as integer, floating, character,
etc.

Keywords in C

A keyword is a reserved word. You cannot use it as a variable name, constant name, etc. There
are only 32 reserved words (keywords) in the C language.
C Operators
An operator is simply a symbol that is used to perform operations. There can
be many types of operations like arithmetic, logical, bitwise, etc.

There are following types of operators to perform different types of


operations in C language.

o Arithmetic Operators
o Relational Operators
o Shift Operators
o Logical Operators
o Bitwise Operators
o Ternary or Conditional Operators
o Assignment Operator
o Misc Operator

Precedence of Operators in C
The precedence of operator species that which operator will be evaluated
first and next. The associativity specifies the operator direction to be
evaluated; it may be left to right or right to left.

Let's understand the precedence by the example given below:

1. int value=10+20*10;  

The value variable will contain 210 because * (multiplicative operator) is


evaluated before + (additive operator).
Category Operator Associativity

Postfix () [] -> . ++ - - Left to right

Unary + - ! ~ ++ - - (type)* & sizeof Right to left

Multiplicative */% Left to right

Additive +- Left to right

Shift << >> Left to right

Relational < <= > >= Left to right

Equality == != Left to right

Bitwise AND & Left to right

Bitwise XOR ^ Left to right

Bitwise OR | Left to right

Logical AND && Left to right

Logical OR || Left to right

Conditional ?: Right to left

Assignment = += -= *= /= %=>>= <<= Right to left


&= ^= |=

Comma , Left to right


Escape Sequence in C
An escape sequence in C language is a sequence of characters that doesn't
represent itself when used inside string literal or character.

It is composed of two or more characters starting with backslash \. For


example: \n represents new line.

List of Escape Sequences in C


Escape Sequence Meaning

\a Alarm or Beep

\b Backspace

\f Form Feed

\n New Line

\r Carriage Return

\t Tab (Horizontal)

\v Vertical Tab

\\ Backslash

\' Single Quote

\" Double Quote


\? Question Mark

\nnn octal number

\xhh hexadecimal number

\0 Null
C Control Statements

If Statement
The if statement is used to check some given condition and perform some
operations depending upon the correctness of that condition. It is mostly
used in the scenario where we need to perform the different operations for
the different conditions. The syntax of the if statement is given below.

1. if(expression){  
2. //code to be executed  
3. }  

Let's see a simple example of C language if statement.

1. #include<stdio.h>    
2. int main(){    
3. int number=0;    
4. printf("Enter a number:");    
5. scanf("%d",&number);    
6. if(number%2==0){    
7. printf("%d is even number",number);    
8. }    
9. return 0;  
10. }    

Output
Enter a number:4
4 is even number
enter a number:5
If-else Statement
The if-else statement is used to perform two operations for a single
condition. The if-else statement is an extension to the if statement using
which, we can perform two different operations, i.e., one is for the
correctness of that condition, and the other is for the incorrectness of the
condition. Here, we must notice that if and else block cannot be executed
simiulteneously. Using if-else statement is always preferable since it always
invokes an otherwise case with every if condition. The syntax of the if-else
statement is given below.

1. if(expression){  
2. //code to be executed if condition is true  
3. }else{  
4. //code to be executed if condition is false  
5. }  

Let's see the simple example to check whether a number is even or odd
using if-else statement in C language.

1. #include<stdio.h>    
2. int main(){    
3. int number=0;    
4. printf("enter a number:");    
5. scanf("%d",&number);     
6. if(number%2==0){    
7. printf("%d is even number",number);    
8. }    
9. else{    
10. printf("%d is odd number",number);    
11. }     
12. return 0;  
13. }    

Output
enter a number:4
4 is even number
If else-if ladder Statement
1. if(condition1){  
2. //code to be executed if condition1 is true  
3. }else if(condition2){  
4. //code to be executed if condition2 is true  
5. }  
6. else if(condition3){  
7. //code to be executed if condition3 is true  
8. }  
9. ...  
10. else{  
11. //code to be executed if all the conditions are false  
12. }  

The example of an if-else-if statement in C language is given below.

1. #include<stdio.h>    
2. int main(){    
3. int number=0;    
4. printf("enter a number:");    
5. scanf("%d",&number);     
6. if(number==10){    
7. printf("number is equals to 10");    
8. }    
9. else if(number==50){    
10. printf("number is equal to 50");    
11. }    
12. else if(number==100){    
13. printf("number is equal to 100");    
14. }    
15. else{    
16. printf("number is not equal to 10, 50 or 100");    
17. }    
18. return 0;  
19. }    

Output
enter a number:4
number is not equal to 10, 50 or 100
enter a number:50
number is equal to 50
C Switch Statement
The switch statement in C is an alternate to if-else-if ladder statement which
allows us to execute multiple operations for the different possibles values of
a single variable called switch variable. Here, We can define various
statements in the multiple cases for the different values of a single variable.

The syntax of switch statement in c language is given below:

1. switch(expression){    
2. case value1:    
3.  //code to be executed;    
4.  break;  //optional  
5. case value2:    
6.  //code to be executed;    
7.  break;  //optional  
8. ......    
9.     
10. default:     
11.  code to be executed if all cases are not matched;    
12. }    
Let's see a simple example of c language switch statement.

1. #include<stdio.h>  
2. int main(){    
3. int number=0;     
4. printf("enter a number:");    
5. scanf("%d",&number);    
6. switch(number){    
7. case 10:    
8. printf("number is equals to 10");    
9. break;    
10. case 50:    
11. printf("number is equal to 50");    
12. break;    
13. case 100:    
14. printf("number is equal to 100");    
15. break;    
16. default:    
17. printf("number is not equal to 10, 50 or 100");    
18. }    
19. return 0;  
20. }    

Output
enter a number:4
number is not equal to 10, 50 or 100
C Loops
The looping can be defined as repeating the same process multiple times
until a specific condition satisfies. There are three types of loops used in the
C language.

Why use loops in C language?

The looping simplifies the complex problems into the easy ones. It enables
us to alter the flow of the program so that instead of writing the same code
again and again, we can repeat the same code for a finite number of times.
For example, if we need to print the first 10 natural numbers then, instead
of using the printf statement 10 times, we can print inside a loop which runs
up to 10 iterations.

Advantage of loops in C

1) It provides code reusability.

2) Using loops, we do not need to write the same code again and again.

2) Using loops, we can traverse over the elements of data structures (array
or linked lists).

do while loop in C
The syntax of the C language do-while loop is given below:

1. do{  
2. //code to be executed  
3. }while(condition);  
Example 1
1. #include<stdio.h>  
2. #include<stdlib.h>  
3. void main ()  
4. {  
5.     char c;  
6.     int choice,dummy;    
7.     do{  
8.     printf("\n1. Print Hello\n2. Print C Programming\n3. Exit\n");  
9.     scanf("%d",&choice);  
10.     switch(choice)  
11.     {  
12.         case 1 :   
13.         printf("Hello");   
14.         break;  
15.         case 2:    
16.         printf("C Programming ");  
17.         break;  
18.         case 3:  
19.         exit(0);   
20.         break;  
21.         default:   
22.         printf("please enter valid choice");      
23.     }  
24.     printf("do you want to enter more?");   
25.     scanf("%d",&dummy);  
26.     scanf("%c",&c);  
27.     }while(c=='y');  
28. }  

Output
1. Print Hello
2. Print C Programming
3. Exit
1
Hello
do you want to enter more?
y

1. Print Hello
2. Print C Programming
3. Exit
2
C Programming
do you want to enter more?
n

do while example

There is given the simple program of c language do while loop where we are
printing the table of 1.

1. #include<stdio.h>  
2. int main(){    
3. int i=1;      
4. do{    
5. printf("%d \n",i);    
6. i++;    
7. }while(i<=10);   
8. return 0;  
9. }     

Output
1
2
3
4
5
6
7
8
9
10
while loop in C

The syntax of while loop in c language is given below:

1. while(condition){  
2. //code to be executed  
3. }  

Let's see the simple program of while loop that prints table of 1.

1. #include<stdio.h>  
2. int main(){    
3. int i=1;      
4. while(i<=10){      
5. printf("%d \n",i);      
6. i++;      
7. }  
8. return 0;  
9. }    

Output
1
2
3
4
5
6
7
8
9
10
for loop in C
The for loop in C language is used to iterate the statements or a part of
the program several times. It is frequently used to traverse the data
structures like the array and linked list.

The syntax of for loop in c language is given below:

1. for(Expression 1; Expression 2; Expression 3){  
2. //code to be executed  
3. }  

Let's see the simple program of for loop that prints table of 1.

1. #include<stdio.h>  
2. int main(){  
3. int i=0;        
4. for(i=1;i<=10;i++){      
5. printf("%d \n",i);      
6. }     
7. return 0;  
8. }     

Output
1
2
3
4
5
6
7
8
9
10
Module 3

C Functions
In c, we can divide a large program into the basic building blocks known as
function. The function contains the set of programming statements enclosed
by {}. A function can be called multiple times to provide reusability and
modularity to the C program. In other words, we can say that the collection
of functions creates a program. The function is also known
as procedure or subroutine in other programming languages.

Advantage of functions in C
There are the following advantages of C functions.

o By using functions, we can avoid rewriting same logic/code again and


again in a program.
o We can call C functions any number of times in a program and from
any place in a program.
o We can track a large C program easily when it is divided into multiple
functions.
o Reusability is the main achievement of C functions.
o However, Function calling is always a overhead in a C program.
SN C function aspects Syntax

1 Function declaration return_type function_name (argument list);

2 Function call function_name (argument_list)

3 Function definition return_type function_name (argument list)


{function body;}

Types of Functions
There are two types of functions in C programming:

1. Library Functions: are the functions which are declared in the C


header files such as scanf(), printf(), gets(), puts(), ceil(), floor() etc.

2. User-defined functions: are the functions which are created by the


C programmer, so that he/she can use it many times. It reduces the
complexity of a big program and optimizes the code.
Different aspects of function calling
A function may or may not accept any argument. It may or may not return
any value. Based on these facts, There are four different aspects of function
calls.

o function without arguments and without return value


o function without arguments and with return value
o function with arguments and without return value
o function with arguments and with return value
Example
1. #include<stdio.h>  
2. void printName();  
3. void main ()  
4. {  
5.     printf("Hello ");  
6.     printName();  
7. }  
8. void printName()  
9. {  
10.     printf("C Language");  
11. }  

Hello C Language
C Array
An array is defined as the collection of similar type of data items stored at
contiguous memory locations. Arrays are the derived data type in C
programming language which can store the primitive type of data such as
int, char, double, float, etc. It also has the capability to store the collection
of derived data types, such as pointers, structure, etc. The array is the
simplest data structure where each data element can be randomly accessed
by using its index number.

C array is beneficial if you have to store similar elements. For example, if we


want to store the marks of a student in 6 subjects, then we don't need to
define different variables for the marks in the different subject. Instead of
that, we can define an array which can store the marks in each subject at
the contiguous memory locations.

By using the array, we can access the elements easily. Only a few lines of
code are required to access the elements of the array.

Properties of Array

The array contains the following properties.

o Each element of an array is of same data type and carries the same
size, i.e., int = 4 bytes.
o Elements of the array are stored at contiguous memory locations
where the first element is stored at the smallest memory location.
o Elements of the array can be randomly accessed since we can
calculate the address of each element of the array with the given base
address and the size of the data element.
Advantage of C Array

1) Code Optimization: Less code to the access the data.

2) Ease of traversing: By using the for loop, we can retrieve the elements
of an array easily.

3) Ease of sorting: To sort the elements of the array, we need a few lines
of code only.

4) Random Access: We can access any element randomly using the array.

Declaration of C Array
We can declare an array in the c language in the following way.

1. data_type array_name[array_size];  

Now, let us see the example to declare the array.

1. int marks[5];  

Here, int is the data_type, marks are the array_name, and 5 is


the array_size.
Initialization of C Array
The simplest way to initialize an array is by using the index of each element.
We can initialize each element of the array by using the index. Consider the
following example.

1. marks[0]=80;//initialization of array  
2. marks[1]=60;  
3. marks[2]=70;  
4. marks[3]=85;  
5. marks[4]=75;  
C array example
1. #include<stdio.h>  
2. int main(){      
3. int i=0;    
4. int marks[5];//declaration of array       
5. marks[0]=80;//initialization of array    
6. marks[1]=60;    
7. marks[2]=70;    
8. marks[3]=85;    
9. marks[4]=75;    
10. //traversal of array    
11. for(i=0;i<5;i++){      
12. printf("%d \n",marks[i]);    
13. }//end of for loop     
14. return 0;  
15. }    

Output
80
60
70
85
75
Module 2

C++ Introduction
C++ is an object-oriented programming language. It is an extension to C programming.

C++ is a general purpose, case-sensitive, free-form programming language that supports object-
oriented, procedural and generic programming.

C++ is a middle-level language, as it encapsulates both high and low level language features.
C++ Basic Input/Output

C++ I/O operation is using the stream concept. Stream is the sequence of bytes or flow of data. It
makes the performance fast.

If bytes flow from main memory to device like printer, display screen, or a network connection,
etc, this is called as output operation.

If bytes flow from device like printer, display screen, or a network connection, etc to main
memory, this is called as input operation.
C++ Object and Class

Since C++ is an object-oriented language, program is designed using objects and classes in C++.

C++ Object

In C++, Object is a real world entity, for example, chair, car, pen, mobile, laptop etc.

In other words, object is an entity that has state and behavior. Here, state means data and
behavior means functionality.

Object is a runtime entity, it is created at runtime.

Object is an instance of a class. All the members of the class can be accessed through object.

Let's see an example to create object of student class using s1 as the reference variable.

1. Student s1;  //creating an object of Student  

C++ Class

In C++, object is a group of similar objects. It is a template from which objects are created. It can
have fields, methods, constructors etc.
Let's see an example of C++ class that has three fields only.

1. class Student    
2.  {    
3.      public:  
4.      int id;  //field or data member     
5.      float salary; //field or data member  
6.      String name;//field or data member    
7.  }    

C++ Constructor

In C++, constructor is a special method which is invoked automatically at the time of object
creation. It is used to initialize the data members of new object generally. The constructor in C++
has the same name as class or structure.

There can be two types of constructors in C++.

o Default constructor
o Parameterized constructor

C++ Destructor

A destructor works opposite to constructor; it destructs the objects of classes. It can be defined
only once in a class. Like constructors, it is invoked automatically.

A destructor is defined like constructor. It must have same name as class. But it is prefixed with
a tilde sign (~).

Note: C++ destructor cannot have parameters. Moreover, modifiers can't be applied on
destructors.
C++ this Pointer

In C++ programming, this is a keyword that refers to the current instance of the class. There can
be 3 main usage of this keyword in C++.

o It can be used to pass current object as a parameter to another method.


o It can be used to refer current class instance variable.
o It can be used to declare indexers.

C++ static

In C++, static is a keyword or modifier that belongs to the type not instance. So instance is not
required to access the static members. In C++, static can be field, method, constructor, class,
properties, operator and event.

Advantage of C++ static keyword

Memory efficient: Now we don't need to create instance for accessing the static members, so it
saves memory. Moreover, it belongs to the type, so it will not get memory each time when
instance is created.

C++ Inheritance

In C++, inheritance is a process in which one object acquires all the properties and behaviors of
its parent object automatically. In such way, you can reuse, extend or modify the attributes and
behaviors which are defined in other class.

In C++, the class which inherits the members of another class is called derived class and the
class whose members are inherited is called base class. The derived class is the specialized class
for the base class.

Advantage of C++ Inheritance

Code reusability: Now you can reuse the members of your parent class. So, there is no need to
define the member again. So less code is required in the class.

Types Of Inheritance

C++ supports five types of inheritance:


o Single inheritance
o Multiple inheritance
o Hierarchical inheritance
o Multilevel inheritance
o Hybrid inheritance

Derived Classes

A Derived class is defined as the class derived from the base class.

The Syntax of Derived class:

1. class derived_class_name :: visibility-mode base_class_name  
2. {  
3.     // body of the derived class.  
4. }  

C++ Polymorphism

The term "Polymorphism" is the combination of "poly" + "morphs" which means many forms. It
is a greek word. In object-oriented programming, we use 3 main concepts: inheritance,
encapsulation, and polymorphism.
Real Life Example Of Polymorphism

Let's consider a real-life example of polymorphism. A lady behaves like a teacher in a classroom,
mother or daughter in a home and customer in a market. Here, a single person is behaving
differently according to the situations.

C++ Templates

A C++ template is a powerful feature added to C++. It allows you to define the generic classes
and generic functions and thus provides support for generic programming. Generic programming
is a technique where generic types are used as parameters in algorithms so that they can work for
a variety of data types.

Templates can be represented in two ways:

o Function templates
o Class templates
Module 4
C++ Files and Streams

In C++ programming we are using the iostream standard library, it


provides cin and cout methods for reading from input and writing to output respectively.

To read and write from a file we are using the standard C++ library called fstream. Let us see
the data types define in fstream library is:

Data Type Description

Fstream It is used to create files, write information to files, and read information from files.

Ifstream It is used to read information from files.

Ofstream It is used to create files and write information to the files.

C++ FileStream example: writing to a file


Let's see the simple example of writing to a text file testout.txt using C++ FileStream programming.

1. #include <iostream>  
2. #include <fstream>  
3. using namespace std;  
4. int main () {  
5.   ofstream filestream("testout.txt");  
6.   if (filestream.is_open())  
7.   {  
8.     filestream << "Welcome to C++.\n";  
9.     filestream << "C++ Tutorial.\n";  
10.     filestream.close();  
11.   }  
12.   else cout <<"File opening is fail.";  
13.   return 0;  
14. }  

Output:

The content of a text file testout.txt is set with the data:


Welcome to C++.
C++ Tutorial.
C++ FileStream example: reading from a file
Let's see the simple example of reading from a text file testout.txt using C++ FileStream programming.

1. #include <iostream>  
2. #include <fstream>  
3. using namespace std;  
4. int main () {  
5.   string srg;  
6.   ifstream filestream("testout.txt");  
7.   if (filestream.is_open())  
8.   {  
9.     while ( getline (filestream,srg) )  
10.     {  
11.       cout << srg <<endl;  
12.     }  
13.     filestream.close();  
14.   }  
15.   else {  
16.       cout << "File opening is fail."<<endl;   
17.     }  
18.   return 0;  
19. }  

Note: Before running the code a text file named as "testout.txt" is need to be created and the content of a text
file is given below:

Welcome to C++.

C++ Tutorial.

Output:

Welcome to C++.
C++ Tutorial.

C++ Read and Write Example


Let's see the simple example of writing the data to a text file testout.txt and then reading the data from the file
using C++ FileStream programming.

1. #include <fstream>  
2. #include <iostream>  
3. using namespace std;  
4. int main () {  
5.    char input[75];  
6.    ofstream os;  
7.    os.open("testout.txt");  
8.    cout <<"Writing to a text file:" << endl;  
9.    cout << "Please Enter your name: ";   
10.    cin.getline(input, 100);  
11.    os << input << endl;  
12.    cout << "Please Enter your age: ";   
13.    cin >> input;  
14.    cin.ignore();  
15.    os << input << endl;  
16.    os.close();  
17.    ifstream is;   
18.    string line;  
19.    is.open("testout.txt");   
20.    cout << "Reading from a text file:" << endl;   
21.    while (getline (is,line))  
22.    {  
23.    cout << line << endl;  
24.    }      
25.    is.close();  
26.    return 0;  
27. }  

Output:

Writing to a text file:


Please Enter your name: Nakul Jain
Please Enter your age: 22
Reading from a text file: Nakul Jain
22
Conclusions

It is naive for an Extension professional to feel that if information is delivered during a learning
activity, the educational mission has been accomplished. The broader mandate that learning
generate change in behavior, practice, or belief requires a much more sophisticated science and
art. In today's information-rich culture, Extension's store of information no longer makes the
organization unique. Rather, Extension's organizational strength and uniqueness lie in the
experience and capability of its professionals to motivate individuals and groups to action.

It is important for Extension educators to develop and field test useful models for program
design and delivery that include behavior change. It is equally important for the models to be
linked to sound educational theory that will be valued by partnering agencies and understood by
the targeted clientele.

The process described in this article accomplished these objectives and resulted in information
that now provides a framework for quality training in a broad range of programming. Further
development of the model has resulted in additional insights with practical application beyond
the scope of this article.
Bibliography
Books:-
1. Let Us C by Yashavant Kanetkar
2. Computer Science, C++ by Sumita Arora
3. The Complete Reference, C++ by Herbert Schildt
4. Software Engineering by Roger S. Pressman
References:-
 www.tutorialspoint.com
 www.w3school.com

 www.allschoolstuff.com
 https://www.youtube.com/channel/UCRhH58xS3s_fvjZGBYE12wg/

You might also like