How to store the integer value "0" in a .bin file? (C ++)

EDIT: Apparently the problem is with the read function: I checked the data in a hex editer

02 00 00 00 01 00 00 00 00 00 00 00

So zero is stored as zero, just not read as zero.

Because when I use my normal store-in-bin file function:

int a = 0;
file.write(reinterpret_cast<char*>(&a), sizeof(a));

      

It stores 0 as a version, char

or "\ 0", which obviously isn't saved (because it's a null value?), So when I call my function to read a null value, it reads the value to the right after it (or earlier if it will be the last one in the file). So how can I store null correctly in the .bin file?

EDIT: Here are some of the functions related to the read / write process:

//Init program: creates a sector.bin for another program to read from.
#include<fstream>
using namespace std;

int main()
{
    fstream file;
    file.open("sector.bin", ios::out | ios::binary);
    if(!file.is_open())
    {
        file.open("sector.bin", ios::out | ios::binary);
        file.close();
        file.open("sector.bin", ios::out | ios::binary);
        if(!file.is_open())
        {
            return -1;
        }
    }
    file.seekp(file.beg);
    int a = 2;
    int b = 1;
    int c = 0;
    file.write(reinterpret_cast<char*>(&a), sizeof(a));
    file.write(reinterpret_cast<char*>(&b), sizeof(b));
    file.write(reinterpret_cast<char*>(&c), sizeof(c));
    file.close();
    return 0;
}

//Read function:  part of another program that intializes variables based off
//of sector.bin
void sector::Init(std::fstream& file)
{
    int top_i = FileRead(file,0);
    std::cout<<top_i<<std::endl;
    for(int i = 0; i < top_i; i++)
    {
        accessLV[i] = FileRead(file,i+1);
        std::cout<<accessLV[i]<<std::endl;
    }
    std::cin.ignore();
    viral_data.add(new X1(5,5,'X'));
    viral_data.add(new X1(9,9,'X'));
    player.set(0,0,'O');
    return;
}
//the FileRead used in init
int FileRead(std::fstream& file, int pos)
{
    int data;
    file.seekg(file.beg + pos);
    file.read(reinterpret_cast<char*>(&data), sizeof(data));
    return data;
}

      

Also, the output for use sector::Init

looks like this:

2
1
1

      

The output I was trying to write to the trash was

2
1
0

      

So either 0 is read / written as 1 or not written and Init reads the last value twice.

0


a source to share


4 answers


It is not clear what you mean by "storing integer value 0" in the file. The files contain bytes, not integers. Do you need to store sizeof (int) 0 bytes or just one '\ 0' byte?

PS I also think the problem may be in your reading code. Have you looked at your .bin file in a hex editor?



PPS Your problem is with the use of seekg () function. Instead of passing the offset in bytes, you pass pos. It should be pos * sizeof (int) instead.

+1


a source



int num = 0;
write( fd, &num, sizeof( int ));

      



+2


a source


I'm not sure what you want to do, it seems to me that the code you provided does what you are asking for:

int main() {
   std::ofstream file("/tmp/tst.out");
   int a = 0;
   file.write(reinterpret_cast<char*>(&a), sizeof(a));
   return 0;
}

      

The result is a file of four bytes that contains the binary representation of a null integer:

$ hexdump /tmp/tst.out
0000000 0000 0000
0000004

      

If you want to store an integer as it's ASCII representation, you have to use a formatted stream with <<

:

std::ofstream file("/tmp/tst.out");
int a = 0;
file << a << std::endl;

      

This way you get:

$ cat /tmp/tst.out
0

      

+1


a source


You need to think about what format the binary should contain - something you don't need to do in the same way with text files, which is why a text file is used a lot.

Assuming a (32-bit) machine where sizeof (int) == 4 (and CHAR_BITS == 8), you can store 4 bytes, all zero of which are in the current file location, using native format, I think I need to work. You can experiment with other values ​​like 0x01020304, you will see the byte layout on your computer.

Of course, you need to be careful to read it back by changing the procedure used to write it. And don't forget to move the file before trying to re-read the data you just wrote.

0


a source







All Articles