| |

VerySource

 Forgot password?
 Register
Search
View: 1316|Reply: 10

Ask a question about pointers.

[Copy link]

3

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-2-13 14:00:01
| Show all posts |Read mode
#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?
Reply

Use magic Report

0

Threads

7

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-4-11 18:15:01
| Show all posts
After opening the file, turn it off.
fclose (stream);
Reply

Use magic Report

3

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-4-11 20:45:01
| Show all posts
I added this line to my code.
However, it can only be executed once, and the file cannot be opened when called again.
Reply

Use magic Report

0

Threads

19

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 United States

Post time: 2020-4-13 12:45:01
| Show all posts
strncpy (szTmp, p + 1, sizeof (pBuf));
-------------------------------------
The problem with this line, szTmp you only defined the size of 128 bytes, copy used sizeof (pBuf) = 256, resulting in out-of-bounds operation, the file name area is overwritten, no file name, of course, the second time can not open the file .
Reply

Use magic Report

0

Threads

19

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-4-13 14:00:01
| Show all posts
strncpy (szTmp, p + 1, sizeof (pBuf));
--------------------------
There is a problem with this line, sizeof (pBuf) = 256, and your szTmp is only 128 bytes in size, resulting in an out-of-bounds operation, overwriting the contents of lpFileName. When the function is called again, there is no file name, and of course there is an error opening the file.
Reply

Use magic Report

0

Threads

19

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 Invalid IP Address

Post time: 2020-4-13 16:45:02
| Show all posts
strncpy (szTmp, p + 1, sizeof (pBuf));
Cross-border operation, washed the file name
Reply

Use magic Report

0

Threads

19

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-4-14 13:45:01
| Show all posts
strncpy (szTmp, p + 1, sizeof (pBuf));
Out of bounds operation, the file name was overwritten, and an error occurred when opening again.
Reply

Use magic Report

0

Threads

19

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-4-14 14:15:01
| Show all posts
strncpy (szTmp, p + 1, sizeof (pBuf));
Cross-border operation, overwriting the file name.
Reply

Use magic Report

0

Threads

24

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-4-15 12:15:01
| Show all posts
Unlikely
Reply

Use magic Report

0

Threads

19

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-4-16 21:45:01
| Show all posts
strncpy (szTmp, p + 1, sizeof (pBuf));
There is something wrong with this line of code. The file name is overwritten.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list