[Webinar] Streamline your web hosting managementRegister Today

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

How to copy this char array?

Dear Experts,
the program is reading records from an access database.
i'm using VC++ 6.0.
it extract the record to the program.
i want to copy the record (string).
there is no error when i compile it.
but when i run it, the system show this:


application error:
the instruction at "0x004209f1" referenced memory at "0x0044a1e8". the memory could not be "written".


this is the code:


void MainWnd::OpenDB ()
{
      _RecordsetPtr   spRS;
                _ConnectionPtr  spCON;
      char * temp = "";

      int count;
    try{
        CREATEiNSTANCE(spCON,Connection);
        spCON->ConnectionString = L"driver={sql server};SERVER=(local);Database=pubs;"
                                  L"UID=sa; PWD=;";
         spCON->ConnectionString =L"DRIVER={Microsoft Access Driver (*.mdb)};"
                                        L"DBQ=MPEG_List_db.mdb;DefaultDir=C:\\zWAMP_Dev\\VC_Code\\Sample_ADOVC1\\Code;";
 
        spCON->Open( "", "", "", -1 );
        CREATEiNSTANCE(spRS,Recordset)
        spRS->PutRefActiveConnection( spCON );
        spRS->Open("select directory, filename from TabMPEG", vtMissing, adOpenKeyset,
                    adLockBatchOptimistic, -1);

        count = spRS->RecordCount;

        for (int index =0; index <2 ; index++){
      

//i copy the record from here.

                strcpy(temp,(char *) _bstr_t(RsITEM(spRS,"directory")));
      tempFileList[index] = "";
      strcpy(tempFileList[index],temp);




}
        spRS->Close();
        spCON->Close();
       
    }
    catch( _com_error &e){
        _bstr_t bstrSource(e.Source());
        _bstr_t bs =  _bstr_t(" Error: ") + _bstr_t(e.Error()) + _bstr_t(" Msg: ")
            + _bstr_t(e.ErrorMessage()) + _bstr_t(" Description: ")
            + _bstr_t(e.Description());
       
        MessageBox(0,bs,bstrSource, MB_OK);
    }          
}



thanks alot.

regards,
Kgc
0
Kgc
Asked:
Kgc
  • 3
  • 2
  • 2
  • +1
1 Solution
 
jhshuklaCommented:
......
     char * temp = "";
......
                //put a break-point here to check the correctness of my resolution of the problem
                strcpy(temp,(char *) _bstr_t(RsITEM(spRS,"directory")));
                tempFileList[index] = "";
                strcpy(tempFileList[index],temp);

temp is a char * and it is pointing somewhere in the memory - you don't know where. usually it is the case that when you do assignment using a string literal (example: "a string") it ends up in a read-only part of the memory. obviously whe you try to strcpy to read-only space, you will get an error. you need to dynamically allocate memory for temp using malloc(). and after you are done using the memory free up the resources using free().
Additionally you will also get an identical error with copying string to tempFileList[index] because you are doing the same kind of assignment.
btw, why don't you directly strcpy to tempFileList[index]?
0
 
AxterCommented:
You don't need to use malloc for the required memory.

You can use a fixed size variable, or use new instead, and then delete when finish.

fix size variable method:
char  temp[99] = "";
0
 
KgcAuthor Commented:
thanks jhshukla and Axter.
i'm very sorry and i think i left out some message.

at the header file, i have declare this:

char *  tempFileList [30];
// i was trying to create an array with 30 strings

thanks.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
AxterCommented:
>>char *  tempFileList [30];
// i was trying to create an array with 30 strings

That would create an array of 30 pointers, and it's not what you want.

Instead use the following:
char tempFileList[30];
0
 
AxterCommented:
If you need an array of strings, you can use the following declaration:
char tempFileList[30][30];
0
 
KgcAuthor Commented:
Dear Experts,

I just want to extract the records (String) from an access database and store it in a list.
The list will be use by the program after that.

Dear jhshukla, the temp is just for testing and i forget to comment it. Sorry about that.

thanks.
0
 
atxondoCommented:
I think you must declare a function to copy all the string to another array of char pointers.

char ** duplicate (char * chararray[30])
{
      char *  tempFileList [30];

      for(int i=0;i<30;i++)
      {
           strcpy(tempFileList[i], chararray[i]);
      }
       return tempFileList;
}

I think something like this should work. I´m not sure if this is the exact code but i think it should be something like this.
0
 
jhshuklaCommented:
>> You don't need to use malloc for the required memory.
Forgot that this is C++. not C

for the rest of it, go by Axter's suggestion.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

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