开发者

C++ and binary files - newbie question

I have the following code and i am trying to write some data in a binary file. The problem is that i don't have any experience with binary files and i cant understand what i am doing.

#include <iostream>
#include <fstream>
#include <string>

#define RPF 5

using namespace std;

int write_header(int h_len, ofstream& f)
{
    int h;
    for(h=0;h<h_len;h++)
    {
        int num = 0;
        f.write((char*)&num,sizeof(char));
    }
    return 0;
}
int new_file(const char* name)
{
    ofstream n_file(name,ofstream::binary);
    write_header(RPF,n_file);
    n_file.close();
    return 0;
}

int main(int argc, char **argv)
{
    ofstream file("file.dat",ofstream::binary);
    file.seekp(10);
    file.write("this is a message",3);
    new_file("file1.dat");
    cin.get();
    return 0;
}

1. As you can see i am opening file.dat and writing inside the word "thi". Then i open the file and i see the ASCII value of it. Why does this happen?

  1. Then i make a new file file1.dat and i try to write in it the number 0 five times. What i am supposed to use?

this

f.write((char*)&num,sizeof(char));

or this

f.write((char*)&num,sizeof(int));

and why i cant write the value of the number as is and i have to cast it as a开发者_运维百科 char*? Is this because write() works like this or i am able to write only chars to a binary file?

Can anyone help me understand what's happening?


Function write() that a pointer to your data buffer and the length in bytes of the data to be streamed to the file. So when you say

file.write("this is a message",3);

you tell the write function to write 3 bytes in the file. And that is "thi".

This

f.write((char*)&num,sizeof(char));

tells the write function to put sizeof(char) bytes in the file. That is 1 byte. You probably want it

f.write((char*)&num,sizeof(int));

as num is a int variable.


  1. You are writing the ASCII string "thi" to file.dat. If you opened the file in a hex editor, you would see "74 68 69", which is the numeric representations of those characters. But if you open file.dat in an editor that understands ASCII, it will most likely translate those values back to their ASCII representation to make it easier to view. Opening the ofstream in ios::binary mode means that data is output to file as is, and no transformations may be applied by the stream before hand.

  2. The function ofstream::write(const char *data, streamsize len) has two parameters. data is like this so that write is operating on individual bytes. That is why you have to cast num to a char* first. The second parameter, len, indicates how many bytes, starting from data that will be written to the file. My advise would be to use write(static_cast<char*>(num), sizeof(num)), then set num to be a type big enough to store the data required. If you declare int num, then on a 32bit platform, 20 zero bytes would be written to the file. If you only want 5 zero bytes, then declare as char num.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜