做过自动更新的朋友给点提示吧
我要做个自动更新程序,有个软件安装目录有个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);
}