I finally get solution which compiles well
header:
Code:
#ifndef BCLASS_H
#define BCLASS_H
class MyClass
{
public:
char FullName[40];
char CompleteAddress[120];
char Gender;
double Age;
bool myBool;
};
int RWrecord (bool RW, long record, MyClass *mc);
#endif //BCLASS_H
function file:
Code:
int RWrecord (bool RW, long record, MyClass *mc)
{
std::string filename="myJunk.dat";
if (RW)
{
std::ifstream ifs(filename.c_str(), std::ios::binary);
ifs.seekg (sizeof(mc)*record, std::ios::beg);
ifs.read((char*)&mc, sizeof(mc));
ifs.close();
}
else
{
std::ofstream ofs(filename.c_str(), std::ios::binary);
ofs.seekp (sizeof(mc)*record, std::ios::beg);
ofs.write((char*)&mc, sizeof(mc));
ofs.close();
}
return 0;
}
main:
Code:
int main()
{
std::string filename="myJunk.dat";
remove(filename.c_str());
bool RW;
long record;
MyClass one;
strcpy(one.FullName, "Row two");
strcpy(one.CompleteAddress, "My row two adress");
one.Gender = 'M';
one.Age = 3.14;
one.myBool = true;
record = 2;
RW = true;
int RWrecord (bool RW, long record, MyClass *one);
//-------------------------------------------------------------------
record = 2;
RW = false;
MyClass two;
int RWrecord (bool RW, long record, MyClass *two);
std::cout << "My Information\n";
std::cout << "My Name: " << two.FullName << std::endl;
std::cout << "Address: " << two.CompleteAddress << std::endl;
if(two.Gender == 'f' || two.Gender == 'F')
std::cout << "Gender: Female" << std::endl;
else if(two.Gender == 'm' || two.Gender == 'M')
std::cout << "Gender: Male" << std::endl;
else
std::cout << "Gender: Unknown" << std::endl;
std::cout << "Age: " << two.Age << std::endl;
return 0;
}
1 delete file if exist
2 fill data values to copy of class "one"
3 set RW to write and record to 2
4 call RWrecord to write data to record 2 (171 byte from beggining of file)
5 call RWrecord to read data from record 2 to new variable "two"
6 print data from variable two to console
Program compiles well
but my data dont passes correct
I assume that problem lies in my general missunderstanding of strings and pointers.
So please if someone could correct my code to get it to working condition.
Thanks.