Wednesday, October 15th 2008, 9:54pm UTC+1

You are not logged in.

  • Login
  • Register

adikiekrz

Beginner

Posts: 16

Location: poland

Occupation: informatyk

1

Monday, May 5th 2008, 5:39pm

convert str to wchar

i need convert string to const wchar but i dont know how do this
i want do same like this:
QString a="C:\\folder" + currentitem();
DeleteFile(a);


but i have this error :
error: cannot convert `std::string' to `const WCHAR*' for argument `1' to `BOOL DeleteFileW(const WCHAR*)'
  • Go to the top of the page

2

Monday, May 5th 2008, 5:52pm

RE: convert str to wchar

Try QString::toWCharArray( wchar_t * array )
Nicolas
  • Go to the top of the page

adikiekrz

Beginner

Posts: 16

Location: poland

Occupation: informatyk

3

Monday, May 5th 2008, 6:06pm

i use like this:

Source code

1
2
3
4
5
6
QString e="C:\\a"+s;
wchar_t *we;
e.toWCharArray(we);
DeleteFile(we);
t=ui.listWidget_1->currentItem();
s=t->text();


but when i use this code program is hang

This post has been edited 1 times, last edit by "adikiekrz" (May 5th 2008, 7:08pm)

  • Go to the top of the page

Stian

Beginner

Posts: 8

Location: Norway

Occupation: Senior Research Engineer

4

Monday, May 5th 2008, 8:47pm

It looks like you have forgotten to allocate memory for your we. Try:

wchart_t *we = new wchar_t[e.size()+1];
e.toWCharArray(we);
DeleteFile(we);
delete[] we;

...or if you want to get rid of the delete[] operator, allocate enough memory on the stack. Assuming 256 wchar_t's are enough:

wchart_t we[256];
e.toWCharArray(we);
DeleteFile(we);
Stian André Olsen

This post has been edited 3 times, last edit by "Stian" (May 6th 2008, 1:28pm)

  • Go to the top of the page

Stian

Beginner

Posts: 8

Location: Norway

Occupation: Senior Research Engineer

5

Monday, May 5th 2008, 8:54pm

I am not sure, but after reading the documentation for QString::toWCharArray it looks like you have to zero-terminate the wchar_t array yourself. If that's the case, try:

wchart_t *we = new wchar_t[e.size()+1];
e.toWCharArray(we);
we[e.size()] = 0;
DeleteFile(we);
delete[] we;
Stian André Olsen

This post has been edited 2 times, last edit by "Stian" (May 5th 2008, 9:39pm)

  • Go to the top of the page

6

Tuesday, May 6th 2008, 9:02am

I apologize, it was stupid
Nicolas

This post has been edited 2 times, last edit by "Nicolas SOUCHON" (May 6th 2008, 9:20am)

  • Go to the top of the page

adikiekrz

Beginner

Posts: 16

Location: poland

Occupation: informatyk

7

Saturday, May 10th 2008, 8:37pm

i made this:

Source code

1
2
3
4
5
6
QString e="C:\\a\\" + s;
    wchar_t we;
    e.toWCharArray(we);
    DeleteFile(we);
    t=ui.listWidget_1->currentItem();
    s=t->text();


but only if file name is in one letter(example a.txt) that is delete file but if is more then one letter(example abc.txt) not delete
  • Go to the top of the page

8

Saturday, May 10th 2008, 10:25pm

read the doc
the wchar_t array has to be allocated

but this seems more simple:

Source code

1
QFile( "C:\\a\\" + s ).remove();
Nicolas
  • Go to the top of the page

adikiekrz

Beginner

Posts: 16

Location: poland

Occupation: informatyk

9

Sunday, May 11th 2008, 11:21am

thank you
i is working

This post has been edited 1 times, last edit by "adikiekrz" (May 11th 2008, 11:34am)

  • Go to the top of the page

Rate this thread