0

I am very new to programming, Thank you for bearing with me. I am in need of kind help to solve the issue I am facing, I need to read strings from an excel file (I am using the famous libxl.h library), store it in memory, and rearrange (with some logic) and send it out to a new excel file.

below is my code.

#include <iostream>
#include <windows.h>
#include <conio.h>
#include "libxl.h"

using namespace libxl;
const wchar_t* s1;
const wchar_t* r64ms[64]; //I intend to create an array of 64 inputs

int main() 
{   
Book* book2 = xlCreateBook();
if(book2)
{
    if(book2->load(L"..\\xxxx.xls"))
    {   
        Sheet* sheetms = getSheetByName(book2, L"Sheet1");
        if(sheetms)
        {   
            for (int i=0; i<= 10; i+=1) 
                { s1 = sheetms->readStr(i, 1);
                  r64ms[i]=s1;
                  std::wcout << r64ms[i]<< std::endl << std::endl;
                }

         }
    }
}

Book* bookout = xlCreateBook();
if(bookout)
{
    Sheet* mssheet_out = bookout->addSheet(L"Sheet1");
    if(mssheet_out)
    {   
        for (int i=0; i<=64; i++)
        {
                mssheet_out->writeStr(i, 1, r64ms[i]);
                //even if i only have 10 inputs, I guess it doesn't matter//
                //and will only send out 10 //

         }
    }
}
return 0;
}

What I have when I run the program is an error "Unhandled exception at 0x0fb147af (msvcr100d.dll) in ExtractTS.exe: 0xC0000005: Access violation reading location 0x00000000." and the Pointer sends me to this line (see comment line 'this line' below):

size_t __cdecl wcslen (
    const wchar_t * wcs
    )
{
    const wchar_t *eos = wcs;

    while( *eos++ ) ; // <<<< This line//

    return( (size_t)(eos - wcs - 1) );
 }

Regardless I try to print out using 'std::wcout' or I try to writeStr to the excel file, the program stops at that moment and gives me this error.

Can someone please be so kind as to help me? I suspect it is the mistake on the way I created the array on const wchar_t* or something. I have tried to search online and couldn't find similar Question/solution.

Thank you very much for your patience with me!

  • There are some parts of the codes (processing part and invoking XL document part) where I didn't show to shorten the length of my question, in case you wonder. – Prietess Nov 02 '16 at 09:23
  • 1
    Change `r64ms`from `wchar_t*` to `std::wstring` type. You're not copying what you get back from `readStr` – Martin Ba Nov 02 '16 at 09:39
  • @MartinBa thanks for your kind answer! But can you be more specific please? I am totally new to programming, please excuse me. The type from readStr function is wchar_t* and I cannot store it as other types. Thanks for your further explanation. – Prietess Nov 03 '16 at 03:26
  • @MartinBa these are the lines defining the function readStr and writeStr. virtual const TCHAR* XLAPIENTRY readStr(int row, int col, IFormatT** format = 0) = 0; and for writeStr: -------------- virtual bool XLAPIENTRY writeStr(int row, int col, const TCHAR* value, IFormatT* format = 0, CellType type = CELLTYPE_STRING) = 0; – Prietess Nov 03 '16 at 03:29
  • I recommend reading a C++ tutorial, especially the section(s) on std::string and how strings are treated in C++ in general. – Martin Ba Nov 03 '16 at 10:32

0 Answers0