English 中文(简体)
C++记忆中的书写数据
原标题:Writing data to memory in C++
  • 时间:2011-10-17 07:47:38
  •  标签:
  • c++

I want to write a a mix of int, char, real in void *data. I am using a file pointer to run through the data block. Now my question is that since the data type is void, I have to typecast it to int while writing integer and char for writing string. While typecasting I used the following sample code:

*((int *)data+0) = 14;      //writing int
*((int *)data+4) = 5;       //writing int, left a space of 4 bytes for int
*((char *)data+8) =  a ;    //writing char
*((char *)data+9) =  f ;    //writing char

但是,在阅读这些价值观时,它会带来正确的价值。

cout<<*((int *)data+0);
cout<<*((int *)data+3);
cout<<*((char *)data+8);

我的法典是否正确? 由于数据无效,我对此表示怀疑。

问题回答
*((int *)data+4) = 5; // writing 4th int
cout<<*((int *)data+3); // but reading third one

仅就这种情况而言,(int *)数据+4)指第4类(即第16类按体大小=4),而不是第4类。 这就是说,你用0至3、16至19、8、9、0至3号手法 over。 您可能指的是:*(int *)(char*)data + X

Edited,以纠正MSalter所指出的错误

除了其他人提到的打字(data+3,而不是data+4)之外,你还需要改变例如。

*((int *)data+4)

纽约总部

*((int *)data+1)

because adding 4 纽约总部 an int * doesn t add 4 纽约总部 the address, it adds 4 * sizeof (int).

If you need 纽约总部 write 纽约总部 an offset that is not a multiple of sizeof(int) (say, 7), you need:

*(int *)((char *)data+7)

For this reason, it might be better 纽约总部 make data a char * 纽约总部 start with, so you can just say

*(int *)(data+7)

1. 使用一个或一个单元。

Here, pointer arithmetics is misleading you. When you add 4 to an int * you are adding actually four times sizeof int.

如果您的数据始终如一,那么你为什么不只使用。 ......

struct MemoryLayout {
    int _first;
    int _second;
    char _c1;
    char _c2;
};

?

您将第二稿改写如下:

*((int *)data+4) = 5;    // offset = 4

改为:

cout<<*((int *)data+3);  // offset = 3

In addition, the (int*) cast is binding to data, not data+4 so that your 4 is scaled up by the size of an int.

如果你真的想这样做(并且由于数据格式的差异,不可能有<条码>/代码>),那么你就应当将<条码>/条码>插入<条码>至<条码>,然后在你所希望的类型上添加到<条码><>>>><>>>>>>><>>>>>>>>>>。

这将是:

*((int*)((char*)(data + 0)) = 14;    //writing int
*((int*)((char*)(data + 4)) = 5;     //writing int
*((char*)data + 8) =  a ;            //writing char
*((char*)data + 9) =  f ;            //writing char
int Data;
//char Data;
//float Data;

FILE *File = fopen("File.txt","wb");
fwrite((char *)&Data,sizeof(Data),1,File);
fclose(File);

File = fopen("File.txt","rb");
fread((char *)&Data,sizeof(Data),1,File);
fclose(File);

......

int DataInt1 = 200;
char DataChar1 =  N ;

FILE *File = fopen("File.txt","wb");
fwrite((char *)&DataInt1,sizeof(DataInt1),1,File);
fwrite((char *)&DataChar1,sizeof(DataChar1),1,File);
fclose(File);

int DataInt2 = 0;
char DataChar2 = 0;

File = fopen("File.txt","rb");
fread((char *)&DataInt2,sizeof(DataInt2),1,File);
fread((char *)&DataChar2,sizeof(DataChar2),1,File);
fclose(File);

printf("%d %d!
",DataInt2,DataChar2);




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签