int do_openFile(void)
{
   char *p ;                  /* pointer to outFileName */
   char *m ;                  /* pointer to report date */
   char *mon;                 /* month number in report date */



   /* get output file name   */
  m = pk->CNCRptdate;
  m += 2;
  strncpy(mon, m, 2);

  p = outFileName ;

  strcpy(p, pi->CNDExtransit);  
  p += 5;
  strcpy(p, monTab[atoi(mon)]);           
  p += 3;
  strcpy(p, ".");                         
  p += 1;
  strcpy(p, "TXT");

  /* Open a new file for writing, if file exists, its contents are destroyed
   */
 if ((outfile = fopen(outFileName, "w")) == NULL)
     {
	printf ("                                           \r");
	printf ("fopen failed\n");
	printf ("Please hit 'PRINT SCREEN' and then hit ENTER to abort...\n");
	getch();
	return (0);
     };
}

Issue: When the control hits the first strncpy() function, it throws "Unhandled exception at 0x00401acd in ICRSEX2B.exe: 0xC0000005: Access violation writing location 0x0040258f."

Pls let me know if any other information is required.

Recommended Answers

All 6 Replies

>strncpy(mon, m, 2);
Where does mon point?

char *mon;
mon is a character pointer variable.
Is there any problem with the declaration?

>mon is a character pointer variable.
I didn't ask you what it was, I asked you where it points. My question is an answer to your question. When you can answer my question satisfactorily, you'll learn what your problem is.

char *mon;
mon is a character pointer variable.
Is there any problem with the declaration?

strncpy() does not allocate memory for the destination address, that has to be done by the calling function. So you might want to declare it like this: char mon[255] = {0}; . That allocated enough memory to store 255 characters in that buffer. Change that number to whatever you think will be needed. It will also initialize each of the bytes to '\0';

commented: And my attempt to get the OP to actually use his brain is now wasted effort. Arigatou gozaimashita. -4

Narue, I forgot to you tell you one important info. I am totally new to C/C++/VC++. Basically I am a .NET programmer. I am being forced to do this job. But anyway it is a good learning for me.

Might be in your next reply you can be more descriptive considering me as a beginner

>I am totally new to C/C++/VC++.
Yes, I figured that out on my own.

>Might be in your next reply you can be more descriptive considering me as a beginner
I pointed out your exact error and described it in such a way that a little research on your part would produce a solution. Even a .NET programmer can figure out what to do with such a detailed hint. ;)

Here's a bigger hint: Uninitialized pointers are not pointers to infinite memory. They're pointers to random addresses. I'm sure you can imagine the havoc wreaked by using random memory blocks as if you own them, and you have at least one real world example now.

commented: Sometime vague hits does nothing but confuse. -5
commented: Hmm, strange, I don't see any confusion :) +7
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.