|
#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);
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. In this case, it appears as opne file is error. Excuse me, prawns? |
|