×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Storing string into file

Storing string into file

Storing string into file

(OP)
I wanna write a program that storing the following struct of data into the binary file:

CODE

struct staff {
   string id; // maximum 8 characters.
   string name; // maximum 35 characters.
   string ic; // identity card number, maximum 14 character.
   unsigned short age;
   string remark; // no limit.
} employee;

I was noticed that the byte returned by sizeof(string) being 28, even though the string variable contains more then 28 characters. I tried the following:

CODE

write((const char *)&employee, sizeof(employee));

But next time when i retrieve by using function read(char *, size_t), the string fields are not what i expected.

So I break out the above structure by separating it from string fields, like this:

CODE

struct staff1 {
   unsigned id;
   unsigned name;
   unsigned ic;
   unsigned remark;
   
   unsigned short age;
} employee;

struct {
   string id; // maximum 8 characters.
   string name; // maximum 35 characters.
   string ic; // identity card number, maximum 14 character.
   string remark; // no limit.
} s;

I use the following way to collecting data and write to tge file:

CODE

#include <fstream>
#include <string>
#include <iostream>
using namespace std;

struct staff1 {
   unsigned id;
   unsigned name;
   unsigned ic;
   unsigned remark;
   
   unsigned short age;
} employee;

struct {
   string id; // maximum 8 characters.
   string name; // maximum 35 characters.
   string ic; // identity card number, maximum 14 character.
   string remark; // no limit.
} s;

int main() {
    if (0) {
        ofstream oStrF("str.dat", ios::out | ios::binary);
        ofstream oEmployeeF("employee.dat", ios::out | ios::binary);
        s.id = "01010101";
        employee.id = (s.id.length() + 1); // extra byte for null terminator.
        s.name = "ABC";
        employee.name = (s.name.length() + 1); // extra byte for null terminator.
        
        s.ic = "801010-12-5165";
        employee.ic = (s.ic.length() + 1); // extra byte for null terminator.
        s.remark = "OK";
        employee.remark = (s.remark.length() + 1); // extra byte for null terminator.
        employee.age = 21;

        oStrF.write((const char *)s.id.c_str(), employee.id);
        oStrF.write((const char *)s.name.c_str(), employee.name);
        oStrF.write((const char *)s.ic.c_str(), employee.ic);
        oStrF.write((const char *)s.remark.c_str(), employee.remark);
        oEmployeeF.write((const char *)&employee, sizeof(employee));
    }
    else {
        ifstream iStrF("str.dat", ios::in | ios::binary);
        ifstream iEmployeeF("employee.dat", ios::in | ios::binary);
        iEmployeeF.read((char *)&employee, sizeof(employee));
        // id.
        char *buf = new char[employee.id];
        iStrF.read((char *)buf, employee.id);
        cout << "ID: " << buf << endl;
        delete buf;
        // name.
        buf = new char[employee.name];
        iStrF.read((char *)buf, employee.name);
        cout << "NAME: " << buf << endl;
        delete buf;
        // ic.
        buf = new char[employee.ic];
        iStrF.read((char *)buf, employee.ic);
        cout << "IC: " << buf << endl;
        delete buf;
        // remark.
        buf = new char[employee.remark];
        iStrF.read((char *)buf, employee.remark);
        cout << "REMARK: " << buf << endl;
        delete buf;
        cout << employee.age << endl;
    }
}

Any better solution, i can't found any useful book for the I/O for it.

RE: Storing string into file

(OP)
I still cannot get the answer. The problem is, the string size  can only be known at runtime, otherwise I can just use the character array (fixed size) to eliminate all the problem. But  doing that way will waste up space in the file.

RE: Storing string into file

std::string binary serialization (unsafe!) variant:

CODE

void StrTo(const std::string& s, std::ostream& f)
{
  int sz = s.size();
  f.write((const char*)&sz,sizeof sz);
  if (sz)
     f.write(s.c_str(),sz);
}

void StrFrom(std::string& s, std::istream& f)
{
  int sz;
  f.read((char*)&sz,sizeof sz);
  if (sz > 0)
  {
     char* p = new char[sz];
     f.read(p,sz);
     s.assign(p,sz);
     delete [] p; // See [] before pointer in delete array.
  }
  else
     s.erase();
}
Add i/o error and EoF handling.
Binary mode stream assumed.
Remember that std::string may contain zero bytes.

RE: Storing string into file

Use data() instead of c_str(). Change StrFrom to use vector:

CODE

void StrTo(const std::string& s, std::ostream& f)
{
  int sz = s.size();
  f.write((const char*)&sz,sizeof sz);
  if (sz)
     f.write(s.data(),sz);
}

void StrFrom(std::string& s, std::istream& f)
{
  int sz;
  f.read((char*)&sz,sizeof sz);
  if (sz > 0)
  {
     std::vector<char> p(sz);
     f.read(p,sz);
     s.assign(p.begin(),p.end());
  }
  else
     s.erase();
}

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close