文件操作

kyxyljb 2012-09-12 08:49:23
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<time.h>
#include<error.h>
#include<signal.h>
#include<sys/time.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<sys/socket.h>
#include<sys/wait.h>
#include<unistd.h>
#include<netdb.h>

typedef struct Depost
{
char szSerial[10 + 1]; /* 流水号 */
char szPhone[13 + 1]; /* 手机号码 */
char szMoney[16 + 1]; /* 金额 */
char szTime[14 + 1]; /* 交易时间 */
char szTradeFlag[1 + 1]; /* 交易状态 */
}strDepost;


char* GetItemValue(int nIndex, char *src, char *split)
{
int n, i ;
char *pIndex ;
char *p ;
char tempbuff[64];

if (nIndex < 0 || src == NULL)
return NULL;

for (pIndex = src, i = 0; i < nIndex; i ++)
{
pIndex = strstr(pIndex, split);
if (pIndex == NULL) return NULL;
pIndex += strlen(split);
}

memset(tempbuff, 0, sizeof(tempbuff));

p = pIndex;
n = 0;
while (*p && memcmp(p, split, strlen(split)) != 0 && (n < 63))
tempbuff[n++] = *p ++;
tempbuff[n] = '\0';

return tempbuff;
}


int main(int argc,char** argv)
{
char szCrmFile[256];
strDepost strCrmDepost_s;
char szCrmLine[256];
FILE *pCrmFile;
char cmd[256];


memset(szCrmFile, 0, sizeof(szCrmFile));
sprintf(szCrmFile, "%s/crm1", getenv("FILESERVERDIR"));

/* 开始对帐,以CRM文件为参照物 */
if ((pCrmFile = fopen(szCrmFile,"r")) == NULL)
{
printf("打开CRM明细对帐文件错误\n");
return -1;
}

while(1)
{
memset(szCrmLine,0,sizeof(szCrmLine));
if (fgets(szCrmLine, sizeof(szCrmLine), pCrmFile) != NULL )
{
printf("crmLine %s\n",szCrmLine);
memset(&strCrmDepost_s, 0, sizeof(strCrmDepost_s));

printf("\nGetItemValue %s,",GetItemValue(0, szCrmLine, ","));
strcpy(strCrmDepost_s.szSerial, GetItemValue(0, szCrmLine, ","));
printf("strCrmDepost_s.szSerial %s\n",strCrmDepost_s.szSerial);

strcpy(strCrmDepost_s.szPhone, GetItemValue(1, szCrmLine, ","));
strcpy(strCrmDepost_s.szMoney, GetItemValue(2, szCrmLine, ","));
strcpy(strCrmDepost_s.szTime, GetItemValue(3, szCrmLine, ","));
strcpy(strCrmDepost_s.szTradeFlag, GetItemValue(4,szCrmLine , ","));

printf("ok\n");
}
else
{
break;
}
}

fclose(pCrmFile);
return 0;
}



crm1文件内容:
11111111,0,0,0,0
22222222,0,0,0,0

执行结果:
crmLine 17 11111111,0,0,0,0
49 49 49 49 49 49 49 49 44 48 44 48 44 48 44 48 10 ~~
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
GetItemValue 11111111,strCrmDepost_s.szSerial 1111lj@49 49 49 49 108 106 1 64 1 0 0 48 0 0 0 0 0 0 0 0 0 0 0 0 0 48 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 48 0 0 0 0 0 0 0 0 0 0 0 0 0 0 48 0
crmLine 17 22222222,0,0,0,0
50 50 50 50 50 50 50 50 44 48 44 48 44 48 44 48 10 ~~
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
GetItemValue 22222222,strCrmDepost_s.szSerial 22222222
50 50 50 50 50 50 50 50 0 0 0 48 0 0 0 0 0 0 0 0 0 0 0 0 0 48 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 48 0 0 0 0 0 0 0 0 0 0 0 0 0 0 48 0

为什么执行结果出错,红色标记部分?是因为tempbuff用的是本地临时变量的原因,在访问该返回地址的内容已被修改?能深入的解释一下
...全文
113 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2012-09-13
  • 打赏
  • 举报
回复
sscanf(szCrmLine,"%[^,],%[^,],%[^,],%[^,],%[^,]",
strCrmDepost_s.szSerial,
strCrmDepost_s.szPhone,
strCrmDepost_s.szMoney,
strCrmDepost_s.szTime,
strCrmDepost_s.szTradeFlag
);
晚安_好梦 2012-09-13
  • 打赏
  • 举报
回复
while(1)
{
memset(&strCrmDepost_s, 0, sizeof(strDepost)); memset(szCrmLine,0,sizeof(szCrmLine));

新增那么一行试试
晚安_好梦 2012-09-13
  • 打赏
  • 举报
回复
while(1)
{
memset(&strCrmDepost_s, 0, sizeof(strDepost)); memset(szCrmLine,0,sizeof(szCrmLine));

新增那么一行试试

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧