このプログラムに問題があります。このプログラムは、基本的に構造体をchar*
ptrにコピーし、もう一度別のファイルにコピーして読み込みます。問題は、それはあなたが手動で構造体のフィールドのサイズによってoffset
をインクリメントが、フィールドの配置を考慮していない最後の構造内の文字の問題
#include<stdio.h>
#include<iostream>
#include<malloc.h>
#include<string>
#include<string.h>
using namespace std;
typedef struct
{
int tableId;
char tableName[30];
int tableHeaderPage; // NOT GETTING PRINTED
}sysTable;
int main()
{
sysTable record;
record.tableId=11;
strcpy(record.tableName,"babyyy");
record.tableHeaderPage=56;
char *recChar=(char*)malloc(sizeof(sysTable)+1);
memcpy(recChar,&record,sizeof(record));
char *recChar2=(char*)malloc(sizeof(sysTable)+1);
memcpy(recChar2,recChar,sizeof(record));
sysTable record2;
int offset=0;
memcpy(&record2.tableId,&recChar[offset],sizeof(record2.tableId));
offset+=sizeof(record2.tableId);
memcpy(&record2.tableName,&recChar[offset],sizeof(record2.tableName));
offset+=sizeof(record2.tableName);
memcpy(&record2.tableHeaderPage,&recChar[offset],sizeof(record2.tableHeaderPage));
cout<<"\n Record Details\n";
cout<<"\n table Id: "<<record2.tableId;
cout<<"\n table Name: "<<record2.tableName;
cout<<"\n tableHeaderPage: "<<record2.tableHeaderPage; // PROBLEM THIS IS NOT PRINTING PROPERLY.
return 0;
}
'tableHeaderPage'を印刷するときに何も出力されませんか、またはあなたが期待していないものが得られますか? –
プログラムはコンパイルされません。あなたの問題を明瞭にする*正確な*プログラムをコピー&ペーストできますか? http://sscce.org/を参照してください。 –
@Rob - 'tableHeaderPage'を太字で表示するOPの試みのためにコンパイルされませんか? –