Skip to main content

C++ program for store student detail in a file | File handling

C++ program for store student detail in a file:-


In this post i am going to write a C++ program that illustrate the use of file handling in C++. File handling requires when we want to save data for in future, if the data require then we  can easily get them. Since, the life of a variables which we make in a program less than the life of the program. As soon as the program end the variables also vanished.

The following program construct a structure student containing the field for Roll no., Name, Class, Year and total marks.
          

Let's begin the program:- 

#include <iostream>
#include <fstream>
using namespace std;
struct student
{
int RollNo;
char Name[30];
int year;
char class1[30];
float total_marks;
};
void ReadStudent(student & TempStud)
{
cout << "\n Enter roll no.: ";
cin >> TempStud.RollNo;
cin.clear();
fflush(stdin);
cout << "\n Enter name: ";
cin.getline(TempStud.Name,30);
cout << "\n Enter class: ";
cin.clear();
fflush(stdin);
cin.getline(TempStud.class1,30);
cout << "\n Enter year";
cin>>TempStud.year;
cin.clear();
fflush(stdin);
cout<< "\n Enter total marks :";
cin>>TempStud.total_marks;
}
void WriteStudent(student TempStud)
{
cout << "\n The roll no.: ";
cout << TempStud.RollNo;
cout << "\n The name: ";
 cout<< TempStud.Name;
cout << "\n Class: ";
cout << TempStud.class1;
cout << "\n Year : ";
cout<< TempStud.year;
cout<< "\n Total marks: ";
cout<< TempStud.total_marks;
}
int main()
{
cout<<"\n 1. Enter data:";
cout<<"\n 2. Display data:";
cout<<"\n Enter your choice:";
short i;
cin>>i;
switch(i){
case 1:
{
struct student Student_Out;
ofstream StudFile_Out;
StudFile_Out.open("student.dat", ios::out | ios::binary | ios::trunc);
if(!StudFile_Out.is_open())
cout << "File cannot be opened \n";
char Continue = 'y';
do
{
ReadStudent(Student_Out);
StudFile_Out.write((char*) &Student_Out, sizeof(struct student));
if(StudFile_Out.fail())
cout << "File write failed";
cout << "Do you want to continue? (y/n): ";
cin >> Continue;
} while(Continue != 'n');
StudFile_Out.close();
break;
}
case 2:
{
struct student Student_In;
ifstream StudFile_In("student.dat", ios::in | ios::binary);
while(!StudFile_In.eof())
{
StudFile_In.read((char*) &Student_In, sizeof(struct student));
if(StudFile_In.fail())
break;
WriteStudent(Student_In);
}
StudFile_In.close();
break;
}
default:
cout<<"\n Invalid :";
main();
}
return 0;
}

Output:-

When user choice is 1:

C++ program for store student detail in a file | File handling

When user choice is 2:

C++ program for store student detail in a file | File handling

Comments

Popular posts from this blog

Advantage and disadvantages of Structure and object oriented programming

In my last post, i discussed about structured oriented programming, object oriented programming and language processors. Today i am going to discuss about :- 1. Advantages and Disadvantages of structured oriented programming. 2. Advantages and Disadvantages of object oriented programming. 3.Difference between them. And in the next post i will discuss about:- 1. 1st C++ program and explanation. 2. Structure of a C++ program. 3. Data types, Identifiers, Variable and Keywords. Let's start: Advantages of Structure oriented programming:- 1. It is easy to understand. 2. It is to test a program that is divided into modules(sub-problem) and sub-module(sub-sub-problem). 3. Modification is easier. 4. It results in speedy execution of the program. 5. Development of modules is easier. Disadvantages of Structured oriented programming:- 1. Modules occupy more space in the memory. 2.  Testing of every module is time consuming process. 3. Combination of modules becomes di

size and range of basic data types and modifing the basic types.

Size and range of basic data types:-   Modifying the basic types:-  Except for type void, the basic data types may have various modifiers preceding them.  You use a modifier to alter the meaning of the base type to fit various situations more  precisely. The list of modifiers is shown here: signed unsigned long short You can apply the modifiers signed, short, long, and unsigned to integer base  types. You can apply unsigned and signed to characters. You may also apply long to  double. The above  table shows all valid data type combinations, along with their minimal ranges and approximate bit widths. (These values also apply to a typical C++  implementation.) Remember, the table shows the minimum range that these types will  have as specified by Standard C/C++, not their typical range. For example, on  computers that use two's complement arithmetic (which is nearly all), an integer will have a range of at least 32,767 to –32,768. The use of signed on integers is al

C++ tokens, rule for naming variable and rule for constructing integer constant, real constant and character constant

 C++ character set :- The following image shows the valid character that are used in C++.  C++ data types:- 1st of all we have to know that What is a data? Data is a raw material for constructing a information. For example, the following list are the data:- Ram,17, XII C. And from this data we can construct the information as Ram Aged 17 years is in class XII C. Data may be any number,words, amount,quantity. Now, Let's come on the topic: Latter i will discus about 'structure', 'Union', 'Class', Array', 'function','pointer','reference' for now there are five basic data types that are: 1. int(integer)  2. char(Character) 3. float 4. double  5. void  Now look at variable:  Variable is a entity whose value may change and in programming language it is used to store data. A integer type variable can hold a integer number(number must be a whole number either in positive or in negative but not in fraction),  a ch