By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,197 Members | 1,205 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,197 IT Pros & Developers. It's quick & easy.

Access violation error

P: 13
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;

}
Jun 20 '07 #1
Share this Question
Share on Google+
4 Replies


gpraghuram
Expert 100+
P: 1,275
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
Jun 20 '07 #2

P: 13
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
Jun 20 '07 #3

Expert 100+
P: 181
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);
Jun 20 '07 #4

P: 13
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
Jun 20 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.