Connecting Tech Pros Worldwide Forums | Help | Site Map

Access violation error

Newbie
 
Join Date: Jun 2007
Posts: 13
#1: Jun 20 '07
Hi...

I got exception access violation at

add1=strcat("c:\\",add); statement...
why?wats solution?


void main()
{
char * output;
char * output2;
char * add;
char * add1;
int msgno;
msgno=1;
char name1[20];
char name[20];

strcpy(name,"name");
output2=new char[2];

itoa((msgno+1),output2,10);
output2[1]='\0';
output=new char[strlen(name)+2];

strcpy(output,name);
output=strcat(output,output2);
output[strlen(name)+1]='\0';

add=new char[(strlen(output))+5];
add=strcat(output,".txt");
add[(strlen(name))+5]='\0';

add1=new char[(strlen(add))+4];
add1=strcat("c:\\",add);.......................... ..............exception
add1[(strlen(name))+8]='\0';

FILE *obj1;

obj1=fopen(add,"w");

delete []output;
delete []output2;
delete []add;
delete []add1;

}

gpraghuram's Avatar
Expert
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,258
#2: Jun 20 '07

re: Access violation error


HI,
Is the following line not throwing any warning?
add1=strcat("c:\\",add);.......................... ..............exce ption

Becos the stract takes the first parameter as a achar and what u are passing is a const char *.

Pass the string name (variable name) to which the string needs to be appended.
It will work fine

Thanks
Raghuram
Newbie
 
Join Date: Jun 2007
Posts: 13
#3: Jun 20 '07

re: Access violation error


Quote:

Originally Posted by gpraghuram

HI,
Is the following line not throwing any warning?
add1=strcat("c:\\",add);.......................... ..............exce ption

Becos the stract takes the first parameter as a achar and what u are passing is a const char *.

Pass the string name (variable name) to which the string needs to be appended.
It will work fine

Thanks
Raghuram

Hi,

Syntax of strcat is: char * strcat(char *,const char *)

I changed code as follows..but still same error

char * str="c:\\";

add1=strcat(str,add);

what should i do to append "c:\\" to the beginning of string add?

Thanks,
Rupali
Expert
 
Join Date: Feb 2007
Location: Bangalore
Posts: 182
#4: Jun 20 '07

re: Access violation error


Quote:

Originally Posted by Rupali12345

Hi,

Syntax of strcat is: char * strcat(char *,const char *)

I changed code as follows..but still same error

char * str="c:\\";

add1=strcat(str,add);

what should i do to append "c:\\" to the beginning of string add?

Thanks,
Rupali

strcat doesnot check for availability of space before appending.
when u give
char * str = "c:\\";
space is allocated just enough to hold c:\\
if you want more space say 100 or so you can use
char str[100];
strcpy(str,"c:\\");
and now use strcat(str,add);
Newbie
 
Join Date: Jun 2007
Posts: 13
#5: Jun 20 '07

re: Access violation error


Quote:

Originally Posted by svlsr2000

strcat doesnot check for availability of space before appending.
when u give
char * str = "c:\\";
space is allocated just enough to hold c:\\
if you want more space say 100 or so you can use
char str[100];
strcpy(str,"c:\\");
and now use strcat(str,add);




it's working fine..NO ERROR...

Thanks,
Rupali
Reply