K
K
kytcenochka2018-04-02 09:02:16
C++ / C#
kytcenochka, 2018-04-02 09:02:16

C++. Creating and writing a binary file?

The task is to create a file and write it with data, but there is a parameter structure that must be respected:
n - index (4 bytes)
timestamp - timestamp (4 bytes)
unsigned int Ak - analog channel voltage value (2 bytes)
unsigned int SM - discrete channel voltage value (2 bytes)
There can be many channels, how the data will be received is still unknown.
Let's say 3 analog channels 2 discrete
The file must be recorded
n timestamp anal. discrete channels.
4 bytes 4 bytes 2 bytes 2 bytes 2 bytes 2 bytes 2 bytes
I am new to c++. Tell me, perhaps there is a more reasonable option for filling in these channels (not from the console).
And yes, my mistakes. And how to check whether the structure is respected.

Binary.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream> 
#include <fstream> 

using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{

    // Create Binary file
    struct Binaryfile{
        int n; //4 byte
        int timestamp;//4 byte
        unsigned int Ak;//2 byte
        unsigned int SM;//2 byte
    };
        Binaryfile s1,analog,discret;
        s1.n =3;
        s1.timestamp=45;
        int countAnalCh=2;
        int countDiscrCh=3;
        analog.Ak=0;
        discret.SM=0;

        fstream binary_file("D:\\Struct\\Struct\\ComtradeFormat\\fileConfiguration.dat",ios::out|ios::binary|ios::app);
        if(!binary_file)
        {
            cout << "Cannot open file.\n";
            return 1;
        }
        binary_file.write((char*)(&s1),sizeof(s1));//write number sample and timestamp

        for (int i=0; i<countAnalCh;i++)
        {
            cout <<"Value analog channel: ";
            cin >> analog.Ak;
            binary_file.write((char*)(&analog.Ak),sizeof(analog.Ak));

        }
        for (int j=0; j<countDiscrCh;j++)
        {
            cout <<"Value discret channel: ";
            cin >> discret.SM;
            binary_file.write((char*)(&discret.SM),sizeof(discret.SM));

        }
        
        binary_file.close();

  
  return 0;
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
T
tsarevfs, 2018-04-02
@tsarevfs

For types where size is important, it is desirable to use fixed width types .
You can fill in from a text file of your own format or one of the standard ones: ini, json, yaml. In the second case, you should use a ready-made library for reading files of the corresponding format.
You can also write a GUI, but that's another story.

R
res2001, 2018-04-02
@res2001

1. unsigned int on most platforms is 4 bytes, so use more appropriate types for signals: uint16_t for example.
2. Since the number of channels changes, it makes sense to separate the header (n and timestamp) from the actual channel data. Write the header separately, and store the channel data in arrays of the appropriate types and write the entire array.
3.Because If you do not know the number of channels in advance, then you should write this information to the file (include the number of analog and discrete channels in the header). 4. It also makes sense to enter a preamble (a set of 2-5 characters identifying your file type) and a file version into the header. This will help with subsequent modifications to the file structure.
5. It is convenient to check the resulting file using the HEX editor 010 Editor - there you can impose a structure on a binary file and view an already structured file. Structures are described in a C-like style, so you can learn in 20 minutes with ready-made examples.

K
kytcenochka, 2018-04-02
@kytcenochka

I am creating an array to fill in the analog channel values. The error is in this line
unsigned short analogValue[ChanellCount.analogChanell]; a constant is expected, but I cannot make a constant in the structure.
It is important that the dimension of the array is equal to the number of analog channels.
Suggest a solution!

struct InfaboutChanell
    { 
       int chanellcount;
       int analogChanell;
       int discretChanell;
    };
    InfaboutChanell ChanellCount;
    ChanellCount.chanellcount=5;
    ChanellCount.analogChanell=2;// количество аналоговых каналов
    ChanellCount.discretChanell=3;

unsigned short analogValue[ChanellCount.analogChanell];
    for(int i=0; i<ChanellCount.analogChanell; i++)
        analogValue[i] = 0+rand()%200;
    binary_file.write((char *) &analogValue, sizeof analogValue);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question