#include <stdio.h>
#include <string.h>
#define NTP_TIME "/etc/ambaipcam/IPC_Q313/config/NTPaddr.txt"
int main()
{
FILE *NTPfp ;
char szNTP[50][255],ntpjudge[100];
char addr[255],addrbuf[255];
char NTPaddress[255]; //-------------要改的NTP地址
strcpy(NTPaddress,"192.168.1.139");
int i,Lid=-1;
int judge;
memset(szNTP,0,sizeof(szNTP));
strcpy(ntpjudge,"1");
if((NTPfp=fopen(NTP_TIME,"r"))==NULL)
{
printf("can not open file!\n");
}
while(fgets(addr,256,NTPfp))
{
Lid++;
memset(addrbuf,0,sizeof(addrbuf));
if(strstr(addr,"NTP")!=NULL) //判断NTP是否打开
{
snprintf(addrbuf,sizeof(addrbuf)," NTP=%s\n",ntpjudge);
strcpy(szNTP[Lid],addrbuf);
}
else if(strstr(addr,"address")!=NULL) //判断文件中哪一行是address
{
snprintf(addrbuf,sizeof(addrbuf)," address=%s\n",NTPaddress);
strcpy(szNTP[Lid],addrbuf);
}
else
strcpy(szNTP[Lid],addr);
}
fclose(NTPfp);
if((NTPfp=fopen(NTP_TIME,"w"))==NULL) //写入NTPaddr.txt
{
printf("can not open file!\n");
return;
}
else
{
for(i=0;i<=Lid;i++)
{
fputs(szNTP[i],NTPfp);
}
fclose(NTPfp);
}
printf("ntpend=%s\n",szNTP[0]);
printf("address=%s\n",szNTP[1]);
}