Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 174
  • Last Modified:

Help with variables...

I'm having issues with variables...

I am trying to load some configuration information from a text file and use it elsewhere in my application.

Here are snipets from the code:

char * CIP = "000.000.000.000";
char* IpAddress = CIP;

errno_t err;

if ((err = fopen_s(&instream, "config.txt", "r")) != 0)
{
      Memo1->AppendText("Configuration file failed to open.\r\n");
}
else
{
      Memo1->AppendText("Configuration file successfully opened.\r\n");
                        
      fscanf(instream, "%s\n", CIP);
}

I am getting an "attempted to read or write to protected memory" error when compiling. I know this has to do with pointers and such, but I've never been good with them and I'm stumpted on this one.

I am not able to change the CIP decleration or usage as the other 2000 lines of code work with it as it is. What I need is the read the first line of the text file and then assign that information to the CIP variable.

Any help you be appreciated as this problem is urgent...
0
demilune
Asked:
demilune
  • 3
1 Solution
 
sunnycoderCommented:
Hi demilune,

Change char * CIP = "000.000.000.000";
to
char CIP[] = "000.000.000.000";

String literals are often stored in read only memory ... by declaring it as array, you would be storing it in R/W memory (stack if declarede locally and data segment if declared globally)

Cheers!
sunnycoder
0
 
sunnycoderCommented:
>I am not able to change the CIP decleration or usage as the other 2000 lines of code work with it as it is.
This change should not affect your existing code. ... give it a shot
0
 
demiluneAuthor Commented:
THANKS THANKS THANKS...

I knew I was missing something simple like that. I had not realized literals were stored read only.

Thanks again!
0
 
sunnycoderCommented:
Glad to help :)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now