How To Create Application For Handling Array And File Using VC++

Rate this article
0 out of 5
In this video tutorial we will show you how to use the Array and File handling MFC libraries shipping with VC++.   After viewing the video tutorial, download the source code and try to modify the code so as to get a feel of what is learned in this video tutorial.


Source Code
ArrayAndFileHandling.cpp

// 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;
}



Download Source Code

Joined Date :14-Nov-2012
Articles
Posted
60
Messages
Posted
3

KTS InfoTech Training division offers various short-term programming courses for Students and Professionals who are looking for a bright career in the field of Software development.

The programming video tutorials presented in this web site are taken from some of the training sessions conducted by KTS InfoTech.

You can also learn the Programming packages through Internet directly form our English Speaking Technical experts at an affordable rate.

For more details click here and submit your training requirements.




   
Messages
Posted:
Post Your Comments
Name (Max 50 Chars)
Comments
   Design  HTML

TekTipsDownload
GateExam
Academic Projects
TekTipsExperts



 
Site optimized for IE7, 1280 X 768 and above. Copyright © 2010 - 2018 KTS InfoTech
Site Developed Using KTS WebCloud