C 码流写文件

#include <stdio.h>
#include <string.h>
#include <WinSock.h>
char alMsgType[5][24]=
{
{1,"aa"},
{2,"bb"},
{3,"cc"},
{4,"dd"},
};
int swap32(unsigned int  pulValue)
{
 pulValue = ((pulValue <<24) & 0xFF000000) |((pulValue << 8) & 0x00FF0000) |((pulValue >> 8) & 0x0000FF00) | ((pulValue >> 24) & 0x000000FF);
 return pulValue;
}
int swap16(unsigned short  pusValue)
{
pusValue = ((pusValue <<16) & 0xFF00) |((pusValue << 8) & 0x00FF) |((pusValue >> 8) & 0xFF00) | ((pusValue >> 24) & 0x00FF);
return pusValue;
}
char* GetMsgType(int ulMsgType)
{

char *pRetMsgType = alMsgType[ulMsgType];
if (NULL ==pRetMsgType)
{
return NULL;
}
return pRetMsgType;
}


typedef struct STRU_L2_MSG
{
unsigned int ulLen;
unsigned int ulmsgType;
unsigned char pMsgBlock[1024]; //大小由ulLen决定
}STU_L2_MSG;


int ParseMsg(unsigned char pcMsg[])
{
char acVal[1024+8]={0};
//int iA = (int) pcMsg;
STU_L2_MSG *pL2Msg = (STU_L2_MSG*)pcMsg;
unsigned int ulMsgType =swap32(pL2Msg->ulmsgType) ; 
char *pcMsgType = GetMsgType(ulMsgType);
unsigned char *pBMsg = pL2Msg->pMsgBlock;
if (NULL == pcMsgType)
{
sprintf(acVal,"%d %s\n",pcMsgType,pL2Msg->pMsgBlock);

}else 
{
sprintf(acVal,"%s %s\n",pcMsgType,pL2Msg->pMsgBlock);
}



sprintf(acVal, "%s\n", pcMsgType);
FILE *stream = NULL;
int  i, numread, numwritten;
if( 0 == fopen_s( &stream, "fread.out", "w+t" ))
{
numwritten = fwrite( acVal, sizeof( char ), sizeof(acVal), stream );
printf( "Wrote %d items\n", numwritten );
}
fclose( stream );


return 0;
}








struct record {
char name[12];
int age;
char company[12];
};
void TestFwrite()
{
struct record array[2] = {{"Ken", 24,"鼎桥"}, {"Knuth", 28,"DQ"}};
FILE *fp = fopen("recfile.log", "wt+");
if (fp == NULL) {
perror("Open file recfile");
}
fwrite(array, sizeof( record), 2, fp);
fclose(fp);
}
void Hex2Str( const char *sSrc,  char *sDest, int nSrcLen )
{
int  i;
char szTmp[4]={0};
for( i = 0; i < nSrcLen; i++ )
{
sprintf( szTmp, " %02X", (unsigned char) sSrc[i] );
memcpy( &sDest[i * 3], szTmp, 3 );
}
sDest[nSrcLen*3+1]='\0';
return ;
}






void TestFput()
{
FILE *fp = fopen("test.log","wt+");
if (NULL == fp)
{
printf("fp null!");
}

char acVal2[] = {01,02,03,04,05,06,07,11,12,13,14,15,01,02,03,04,05,06,07,11,12,13,14,15,
                01,02,03,04,05,06,07,11,12,13,14,15,01,02,03,04,05,06,07,11,12,13,14,15,
                01,02,03,04,05,06,07,11,12,13,14,15,01,02,03,04,05,06,07,11,12,13,14,15};
char acVal3[300] = {0};
for (int j=0;j<4;j++)
{
char acVal[10]="abcd";
strcat(acVal, "\n");
fputs(acVal,fp);
Hex2Str(acVal2,acVal3, sizeof(acVal2));
strcat(acVal3, "\n\0");
fputs(acVal3,fp);

}


fclose(fp);
}
typedef struct TAG_MSG_STRU
{
int ulMsgLen;
short sPid;
short sMsgType;
char* pBuf;
}MSG_STRU;
void GUOHandleMsg(char* pMsgBlock)
{
MSG_STRU* pMsg = (MSG_STRU*)pMsgBlock;
int ulLen = swap32(pMsg->ulMsgLen);


char acArry[10]={0};
memcpy(acArry,pMsgBlock+8, ulLen);
for (int j=0;j<sizeof(acArry);j++)
{
printf(" %02x",acArry[j]);
}


}
int main()
{
char acVal[]={00, 00, 00, 05,
   00, 00, 00, 02, 
   01, 04, 06, 06,06};
//ParseMsg(acVal);
TestFwrite();
TestFput();
GUOHandleMsg(acVal);
printf("******");
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值