C++ Filing Information Needed
Jun 28, 2011 at 1:02pm UTC
Hi, i want to save list of my data into a file. Pls tell me the best way to do it. This code only displays the name i enterd and 0 values of age and id.
Once the records are entered, than i want to modify the any record of my choice... is it possible with filing...??
This is my Student Class Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#ifndef STUDENT_H
#define STUDENT_H
#include <string.h>
class Student
{
public :
Student(){id = 0; age = 0;}
Student(char *n, int i, int a)
{
strcpy(Name, n);
id = i;
age = a;
}
void setName(char * n)
{
strcpy(Name, n);
}
void setAge(int a)
{
age = a;
}
void setId(int i)
{
id = i;
}
char * getname()
{
return Name;
}
int getId()
{
return id;
}
int getAge()
{
return age;
}
private :
char Name[15];
int id;
int age;
};
This is my File Class Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
#ifndef FILING_H
#define FILING_H
#include <string.h>
#include <fstream.h>
#include "Student.h"
class Filing
{
public :
Filing(){}
Filing(char * n)
{
strcpy(fname, n);
}
char * OpenforRead()
{
file.open(fname, ios::in);
if (!file)
{
return "Error Opening File" ;
}
else
{
return "File Openend Successull!!" ;
}
}
char * OpenforWrite()
{
file.open(fname, ios::out);
if (!file)
{
return "Error Opening File" ;
}
else
{
return "File Openend Successull!!" ;
}
}
void Read(Student & ob)
{
file.read((char *)&ob, sizeof (ob));
}
void Write(Student & ob)
{
file.write((char *)&ob, sizeof (ob));
}
void Close()
{
file.close();
}
private :
fstream file;
char fname[50];
};
#endif
This is my Main.cpp Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#include <iostream.h>
#include "Filing.h"
#include "Student.h"
void main()
{
Filing file("Student_Data.txt" );
Student ss[3];
int count=0;
file.OpenforWrite();
{
char name[15];
int age;
int id;
cout << "Enter Name: " ;
cin >> name;
cout << "Enter Age: " ;
cin >> age;
cout << "Id: " ;
cin >> id;
Student s(name,id,age);
ss[count++] = s;
//ss[count++].setName(name);
//ss[count++].setAge(age);
//ss[count++].setId(id);
file.Write(ss[3]);
file.Close();
}
file.OpenforRead();
{
file.Read(ss[3]);
for (int i=0; i<count; i++)
cout << ss[i].getname()<<endl;
cout << ss[i].getAge()<<endl;
cout << ss[i].getId()<<endl;
file.Close();
}
}
Jun 29, 2011 at 8:20am UTC
Yes that can be done...You need to use some function like the below one:
1 2 3 4 5 6 7 8 9 10 11 12
void modify_the_val( string &source, string find, string replace )
{
size_t j;
ofstream myfile_out ("StudentData2.txt" ,ios::app);
for ( ; (j = source.find( find )) != string::npos ; ) {
source.replace( j, find.length(), replace );
}
myfile_out << source <<endl;
}
Topic archived. No new replies allowed.