Access violation error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Rupali12345
    New Member
    • Jun 2007
    • 13

    Access violation error

    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,"na me");
    output2=new char[2];

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

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

    add=new char[(strlen(output) )+5];
    add=strcat(outp ut,".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
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    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

    Comment

    • Rupali12345
      New Member
      • Jun 2007
      • 13

      #3
      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

      Comment

      • svlsr2000
        Recognized Expert New Member
        • Feb 2007
        • 181

        #4
        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) ;

        Comment

        • Rupali12345
          New Member
          • Jun 2007
          • 13

          #5
          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

          Comment

          Working...