views:

535

answers:

4

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

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

So the zero is being 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 the char version, or "\0", which obviously isn't stored (because it's a null value?) so when I call my function to read the zero value, it reads the value right after it (or right before if it would be the last in the file). So how can I store zero in a .bin file properly?

EDIT: Here are some of the functions relating 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 using sector::Init is as follows:

2
1
1

The ouput that I was trying to write into the bin was

2
1
0

So either the 0 is being read/written as a 1, or its not being written and Init is reading the last value twice.

+1  A: 

It's not clear what do you mean by "storing integer value 0" in a file. Files contain bytes, not integers. Do you need to store sizeof(int) 0-bytes, or just one '\0' byte?

P.S. I also would guess the problem might be in your read code. Did you look at your .bin file in a hex editor?

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

Igor Krivokon
+2  A: 
Nikolai N Fetissov
"fd undeclared identifier"
Keand64
This is funny :)
Nikolai N Fetissov
A: 

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

Assuming a (32-bit) machine where sizeof(int) == 4 (and CHAR_BITS == 8), then you can store 4 bytes that are all zero at the current file location using native format, then what you've got there should work, I think. You could experiment with other values such as 0x01020304, you will see the byte layout on your machine.

Of course, you need to be careful reading it back in, reversing the procedure used for writing. And don't forget to reposition the file before trying to re-read the data just written.

Jonathan Leffler
+1  A: 

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

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

This results in a file of four bytes size that contains the binary representation of a zero integer:

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

If you want to store the integer as it's ASCII representation you should use formatted stream output with <<:

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

This way you get:

$ cat /tmp/tst.out
0
sth