0

How can i properly write data from binary file to new char array. I know that this question was asked several times here, but still i couldn't figure out how to do it properly.

This what i have so far..

struct Computer_Details {

      char computer_type[99];
      int release_year;
      float price;

    };


    Computer_Details pc_details;

      cout << "Enter Computer Type: ";
      cin.getline(pc_details.computer_type, 255);
      cout << "Enter Computer Release Date: ";
      cin >> pc_details.release_year;
      cout << "Enter Computer Price: ";
      cin >> pc_details.price;
      cout << "\n\n";

      //Create File
      ofstream file;
      file.open("PC_Database.data", ios::binary | ios::app);

      if (!file) cout << "Couldn't open file\n";
      else {
        file.write((char*)&pc_details, sizeof(Computer_Details));
        file.close();
      }


      ifstream readFile;
      readFile.open("PC_Database.data", ios::binary);
      if (!readFile) cout << "Couldn't Open File\n";
      else {
        readFile.seekg(0, ios::end);
        int fileSize = readFile.tellg();
        int pcCount = fileSize / sizeof(Computer_Details);

        readFile.seekg(0, ios::beg);
        Computer_Details *pc_details = new Computer_Details[pcCount];
        readFile.read((char*)pc_details, pcCount * sizeof(Computer_Details));

        char *buff = new char[299];

        for (int i = 0; i < pcCount; i++)
        {
          //write to buff char
        }
        readFile.close();
    }
Andrew
  • 1,507
  • 1
  • 22
  • 42
  • You can use [`strcpy`](http://en.cppreference.com/w/cpp/string/byte/strcpy). – R Sahu May 08 '17 at 19:45
  • 2
    Possible duplicate of [Reading binary file into char array in c++](http://stackoverflow.com/questions/33935567/reading-binary-file-into-char-array-in-c) – didiz May 08 '17 at 19:55
  • 2
    this answer didn't help – Andrew May 08 '17 at 20:04
  • 1
    off topic: length mismatch between `cin.getline(pc_details.computer_type, 255);` and `char computer_type[99]` that allows for overrunning `computer_type`. – user4581301 May 08 '17 at 20:50

3 Answers3

1

Try

std::ifstream input(szFileName, std::ios::binary);
data = std::vector<char>(std::istreambuf_iterator<char>(input),
    (std::istreambuf_iterator<char>()));
char* charArray = &data[0];
size_t arraySize = data.size();

data vector's buffer is the needed char array. Its constructor's arguments are two iterators. The first one is the current reading position in ifstream (begin of stream in this case). The second one's constructor is default and it is treated as an end iterator.

Oliort UA
  • 1,568
  • 1
  • 14
  • 31
0

Probably problem is size of your structure, check sizes for structure and compare it to size of this structure:

struct Computer_Details {
      char computer_type[100];
      int release_year;
      float price;
    };

Same problem is when you trying to read/write structure which contains bool variable between two other types like int.

Try this :

readFile.read((char*)pc_details->computer_type, sizeof(Computer_Details::computer_type));
readFile.read((char*)pc_details->release_year, sizeof(Computer_Details::release_year));
readFile.read((char*)pc_details->price, sizeof(Computer_Details::price));

edit: look at examples in this comment: https://stackoverflow.com/a/119128/7981164

Community
  • 1
  • 1
semar201
  • 12
  • 1
0

My guess is that you want to shove pc_details into the buff so you can send it somewhere and reconstruct the data.

If that is the case, you could do this:

for( int i=0; i < pcCount; i++ )
{
    memcpy( buff, (char*)pc_details, sizeof(computer_details));
    buff += sizeof(computer_details);
    pc_details++;
}

However, when doing this you must be mindful of alignment and provide padding accordingly. And your code should check your array bounds.

Carlos
  • 358
  • 2
  • 10