C++读写二进制文件问题
voidGameLoginView::SetUserData(){UserDatathePlayer;memset(&thePlayer,0,sizeof(thePlay...
void GameLoginView::SetUserData()
{
UserData thePlayer;
memset(&thePlayer, 0, sizeof(thePlayer));
FILE * fout;
fout = std::fopen("d:\\CityCode\\client_src_dx\\DXGameLobby\\bin\\a.dat", "ab+");
SexyString aString = m_pEditUserName->mString;
wchar_t str[16];
int i;
for( i=0;i<aString.length();i++)
str[i] = aString[i];
str[i] = '\0';
memcpy(thePlayer.name, str, sizeof(thePlayer.name));
std::fwrite(&thePlayer, sizeof(thePlayer), 1, fout);
std::fprintf(fout, "%d", thePlayer);
std::fclose(fout);
}
void GameLoginView::GetUserData()
{
FILE * fin;
UserData thePlayer[5];
ListItem *theItem[5];
memset(&thePlayer, 0, sizeof(thePlayer));
fin = std::fopen("d:\\CityCode\\client_src_dx\\DXGameLobby\\bin\\a.dat", "rb");
if(fin == NULL)
{
return ;
}
std::fread(&thePlayer[0], sizeof(thePlayer), 1, fin);
for(int i=0;i<5;i++)
{
if (thePlayer[i].name[0])
{
theItem[i] = new ListItem(thePlayer[i].name);
m_pUserList->AddBar(theItem[i], i);
m_pEditUserName->SetText(thePlayer[i].name);
}
}
std::fclose(fin);
}
先SetUserData()了一个结构体内容,为什么读出了1个结构体内容和一行乱码?
再SetUserData()添加新结构体内容,除了第一个添加的其他读出的都是乱码。
很少用到读写流,请大神讲解下原理!谢谢啦! 展开
{
UserData thePlayer;
memset(&thePlayer, 0, sizeof(thePlayer));
FILE * fout;
fout = std::fopen("d:\\CityCode\\client_src_dx\\DXGameLobby\\bin\\a.dat", "ab+");
SexyString aString = m_pEditUserName->mString;
wchar_t str[16];
int i;
for( i=0;i<aString.length();i++)
str[i] = aString[i];
str[i] = '\0';
memcpy(thePlayer.name, str, sizeof(thePlayer.name));
std::fwrite(&thePlayer, sizeof(thePlayer), 1, fout);
std::fprintf(fout, "%d", thePlayer);
std::fclose(fout);
}
void GameLoginView::GetUserData()
{
FILE * fin;
UserData thePlayer[5];
ListItem *theItem[5];
memset(&thePlayer, 0, sizeof(thePlayer));
fin = std::fopen("d:\\CityCode\\client_src_dx\\DXGameLobby\\bin\\a.dat", "rb");
if(fin == NULL)
{
return ;
}
std::fread(&thePlayer[0], sizeof(thePlayer), 1, fin);
for(int i=0;i<5;i++)
{
if (thePlayer[i].name[0])
{
theItem[i] = new ListItem(thePlayer[i].name);
m_pUserList->AddBar(theItem[i], i);
m_pEditUserName->SetText(thePlayer[i].name);
}
}
std::fclose(fin);
}
先SetUserData()了一个结构体内容,为什么读出了1个结构体内容和一行乱码?
再SetUserData()添加新结构体内容,除了第一个添加的其他读出的都是乱码。
很少用到读写流,请大神讲解下原理!谢谢啦! 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询