I am trying to add data in a file using fstream
function of C++ but some how my one field call name is not showing inside the file as well as not in my buffer hear is the code and can you please give me some tips to debug this kind of problem. I really can't debug this kind of problem in general.
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdlib.h>
using namespace std;
class student
{
char usn[3];
char name[10];
char age[5];
char buf[25];
public:
void pack();
void unpack();
void searh();
void add();
void dele();
}s;
void student::pack()
{
strcpy(buf,usn);
strcat(buf,"|");
strcat(buf,name);
strcat(buf,"|");
strcat(buf,age);
for (int i=strlen(buf);i<20;i++)
strcat(buf,"#");
}
void student::unpack()
{
strcpy(usn,strtok(buf,"|"));
strcpy(name,strtok(NULL,"|"));
strcpy(age,strtok(NULL,"#"));
}
void student::add()
{
cout << " give student's details \n";
cout << "name : \n";
cin >> name;
cout << "usn : \n";
cin >> usn;
cout << "age : \n";
cin >> age;
pack();
fstream fout;
fout.open("student.txt",ios::in);
cout << buf;
fout << buf;
}
int main()
{
cout << "main";
s.add();
}