|
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int Get_Config (char * lpFileName, char * SerchBuf, char * szTmp)
{
FILE * stream;
char pBuf [256] = "";
char * p;
char * ptoken = NULL;
if ((stream = fopen (lpFileName, "rt")) == NULL)
{
printf ("opne file is error\n");
return -1;
}
while (! feof (stream))
{
memset (pBuf, 0x00, 256);
fgets (pBuf, 256, stream);
if (strstr (pBuf, SerchBuf))
{
p = strchr (pBuf, '=');
strncpy (szTmp, p + 1, sizeof (pBuf));
printf ("this is very important data% s\n", szTmp);
break;
}
else
continue;
}
fclose (stream);
return 1;
}
int main ()
{
char lpFileName [128] = "Config.ini";
char szTmp [128];
memset (szTmp, 0x00, 128);
if (Get_Config (lpFileName, "TMP_LEN =", szTmp) ==-1)
{
printf ("get config error\n");
}
printf ("this TMP_LEN is% s\n", szTmp);
Ranch
getchar ();
return 0;
}
When I execute this program, I can correctly read the contents of the configuration file once, but the second time I call the Get_Config function above, the lpFileName file cannot be opened normally. Excuse me, what went wrong there, I have been looking for a long time and still have no clue. |
|