做过自动更新的朋友给点提示吧

shuirh 2009-03-08 11:23:41
我要做个自动更新程序,有个软件安装目录有个ini文件,然后服务器有个txt文件,比较其中内容,如果有更新,就更新.
我现在写读取ini文件的代码,因为ini一般都是
file1=1.0
file2=2.0
...
这样的形式,我写的代码只能读取第一行的,因为我在读取的时候用了rewind,老是跳到文件开头去了,后面的读取不了了,有经验的朋友提供一下解决方法啊,不胜感激.

#include <iostream>
#include <stdio.h>
using namespace std;

const N = 10; //"回车换行"的ascii码
FILE *fp = NULL;
char *pRow = NULL; //指向当前行的指针

//获得行内容
int GetContent(char *buf_)
{
//char *p = NULL;
//char *pRow = NULL;
char *pEnter = NULL; //寻找"回车换行"的指针
int rowlen_ = 0;

for (pEnter = buf_; pEnter < buf_ + 40; pEnter++)
{
if (*pEnter == N)
{
cout << "*pEnter的值为回车空格" << *pEnter;
cout << "第一行的长度为:" << rowlen_ << endl;
rewind(fp);
pRow = (char*)(malloc(rowlen_ + 1));
memset(pRow, 0, rowlen_+1);
fread(pRow, rowlen_, 1, fp);
cout << "第一行的内容为:" << pRow << endl;
return rowlen_;
}
rowlen_++;
}
};

//或得'='的位置
int WhereIs(int rowlen_)
{
int location_ = 0;
char *pEqual = NULL; //寻找'='的指针

for (pEqual = pRow; pEqual <= pRow + rowlen_; pEqual++ )
{
if (*pEqual == '=')
{
cout << "'='的位置在:" << location_ +1 << endl;
break;
}
location_++;
}
return location_;
}

//或得'='左边的内容
void GetLeft(int location_)
{
char *pleft = NULL;

pleft = (char *)malloc(location_ + 1);
memset(pleft, 0, location_ + 1);
rewind(fp);
fread(pleft, location_, 1, fp);
cout << "等号左边的内容为:" << pleft << endl;
}

//或得'='右边的内容
void GetRight(int rowlen_, int location_)
{
char *pright = NULL;

pright = (char *)malloc(rowlen_ - location_ +1);
memset(pright, 0, rowlen_ -location_ +1);
fseek(fp, 1L, 1);
fread(pright, rowlen_ - location_, 1, fp);
cout << "等号右边的内容为:" << pright << endl;
}

//获取文件中字符总数
int count(FILE *fp)
{
int sum_ = 0;

char ch =fgetc(fp);
while (ch != EOF)
{
//putchar(ch);
ch = fgetc(fp);
sum_++;
}
return sum_;
}

void main()
{
int sum; //文件中的总字节数
int rowlen; //行长度
int location; //'='在行中的位置
int restlen;
char buf[100] = ""; //存放行内容的缓冲区

fp = fopen("ver.ini", "r");

sum = count(fp);
cout << "该文件中共有" << sum <<"个字符" << endl;

rewind(fp);
fread(buf, 40, 1, fp);
cout << "读取的文件内容:" << endl << buf << endl;

rowlen = GetContent(buf);

location = WhereIs(rowlen);

GetLeft(location);

GetRight(rowlen, location);

//fclose(fp);
}
...全文
83 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
shuirh 2009-03-09
  • 打赏
  • 举报
回复
组长要用ini,我真是信他的邪,唉!!!
ForestDB 2009-03-09
  • 打赏
  • 举报
回复
那就不要rewind撒,不停的调gets/getline之类的函数,一行一行的取。
shuirh 2009-03-09
  • 打赏
  • 举报
回复
可是我还要分离出=号两边的内容.
  • 打赏
  • 举报
回复
帮up
hmt.sysfixer.net 2009-03-08
  • 打赏
  • 举报
回复
建议lz改用XML文件,然后进行序列化,就可以把XML文件中的数据读到变量里面,不用像你这么麻烦
lingyin55 2009-03-08
  • 打赏
  • 举报
回复
mark

65,195

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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