K
K
kytcenochka2018-05-24 10:54:03
C++ / C#
kytcenochka, 2018-05-24 10:54:03

Writing a vector to a binary file. C++ Read binary file?

Hello! Help me figure out what is written to the binary file
, there is an input.txt file:
1 2 3 4 5 I
write these numbers into a vector and then into a binary file. It is necessary that each number is written in 2 bytes I look
through the file, the result. I don't understand where these numbers come from.
00 00 00 00 SS SS SS SS B8 D5 16 00 C2 D5 16 00 C4 D5 16 00

short i_n;
    vector <short> vec;
    ifstream input("input.txt");
    for(int i = 0; i < 5; i++)
    {
        input >> i_n;
        vec.push_back(i_n);
    }
    input.close();
  
   std:: ofstream binary_file("fileConfiguration.dat",std::ios::out|std::ios::binary|std::ios::app);
   binary_file.write((char*)&vec, sizeof(vec));
    binary_file.close();

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman, 2018-05-24
@kytcenochka

write((char*)&vec[0],vec.size()*sizeof(vec[0]));

R
Rsa97, 2018-05-24
@Rsa97

The vector itself does not contain your numbers. It contains only a description of the area of ​​memory allocated for storing data.
In this case, the first element of the vector is at 0x0016D5B8, the last one is at 0x0016D5C2.
https://stackoverflow.com/questions/27466373/memor...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question