// ArrayAndFileHandling.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
/*
This program illustrates the use of MFC File Handling(CStdioFile) and string
Handling(CString) classes, writing your own class (CStudent),
Manipulating an array of objects(m_StudentArray[10])
Included these fies in stdafx.h
#include <stdio.h>
#include <iostream.h>
#include <afx.h>
*/
/*----------------------------------- Class Header ------------------------------------------*
Name : CStudent
Date of Creation : 9/17/2004 2:42:46 PM
Type : BASE
Functionality : This class is uses to store all
information about the student.
---------------------------------------------------------------------------------------------*/
class CStudent
{
public:
CString m_strName;
int m_iAge;
CStudent()//Constructor initialize all variables here
{
m_strName="";
m_iAge=-1;
}
~CStudent()//Destructor
{
}
};
int main(int argc, char* argv[])
{
CStudent m_StudentArray[10];
int iNoOfStudents=0;
char YesOrNo;
char strFileName[25];
cout<<"*************Student Database***************
";
cout<<"Please Enter Number of of students";
cin>>iNoOfStudents;
for(int i=0;i<iNoOfStudents;i++)
{
char strName[25];
CStudent Student;
cout<<"
Please Enter the student Name:-";
cin>>strName;
Student.m_strName=strName;
cout<<"
Please Enter the student Age:-";
cin>>Student.m_iAge;
//Hold this information in the array for future use.
m_StudentArray[i]=Student;
}
//Save the student informations.
cout<<"
Do you want to save these informations(y/n):-";
cin>>YesOrNo;
if(YesOrNo=='Y'||YesOrNo=='y')
{
cout<<"
Please Enter the File Name:-";
cin>>strFileName;
try
{
//Declare a file object
CStdioFile StudentDataFile;
//Open the file
StudentDataFile.Open(strFileName,CFile::modeWrite |
CFile::shareExclusive | CFile::modeCreate); //Opening the file with the specified name
//Write data to the file in this format
//name1,age1
//name2,age2
//etc
for(int i=0;i<iNoOfStudents;i++)
{
CString strDataToWriteToFile;
strDataToWriteToFile.Format("%s,%d
",m_StudentArray[i].m_strName,m_StudentArray[i].m_iAge);
StudentDataFile.WriteString(strDataToWriteToFile);
}
//Close the file
StudentDataFile.Close();
}
catch(...)
{
cout<<"Exception While Opening or writing to the file
";
}
}
cout<<"
Do you want to display all informations From File(y/n):-";
cin>>YesOrNo;
if(YesOrNo=='Y'||YesOrNo=='y')
{
}
return 0;
}