using variable insert to directory

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <fstream>
#include <memory>
#include <direct.h>
#include <windows.h>
#include <time.h>
#include <windows.h>
#include <Lmcons.h>


int main()

}
using namespace std;




TCHAR name [ UNLEN + 1 ];
DWORD size = UNLEN + 1;

(GetUserName( (TCHAR*)name, &size ));

copy_file("C:\\Users\\" + name + "\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\file.lnk" );



     return 0;
  
}

I'm getting error: invalid operands of types 'const char [10]' and 'TCHAR [257] {aka char [257]}' to binary 'operator+'

What's wrong?
Last edited on
closed account (48bpfSEw)
"text" is not equal to L"text" or TCHAR("text")!

You need a conversion from string to TCHAR

look at the answer of helios or ask mrs. google ^^

http://www.cplusplus.com/forum/general/12245/

One way to get your filename is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#define _UNICODE
#define UNICODE

#include <windows.h>
#include <tchar.h>
#include <string>
#include <iostream>

using namespace std;

int main ()
{
  const int NAME_LEN = 256;

  wstring UserName(NAME_LEN, ' ');
  
  DWORD size = NAME_LEN;

  GetUserName (&UserName[0], &size);
  UserName.resize (size);
  wstring FileName = wstring(L"C:\\Users\\") + UserName + wstring(L"\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\file.lnk");

  wcout << FileName << "\n\n";

  system ("pause");
  return 0;

}
One way to get your filename is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#define _UNICODE
#define UNICODE

#include <windows.h>
#include <tchar.h>
#include <string>
#include <iostream>

using namespace std;

int main ()
{
  const int NAME_LEN = 256;

  wstring UserName(NAME_LEN, ' ');
  
  DWORD size = NAME_LEN;

  GetUserName (&UserName[0], &size);
  UserName.resize (size);
  wstring FileName = wstring(L"C:\\Users\\") + UserName + wstring(L"\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\file.lnk");

  wcout << FileName << "\n\n";

  system ("pause");
  return 0;

}

Thank you Thomas1965, but I need to copy file like that
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <fstream>
#include <memory>

//C++98 implementation, this function returns true if the copy was successful, false otherwise.

bool copy_file(const char* From, const char* To, std::size_t MaxBufferSize = 1048576)
{
    std::ifstream is(From, std::ios_base::binary);
    std::ofstream os(To, std::ios_base::binary);

    std::pair<char*,std::ptrdiff_t> buffer;
    buffer = std::get_temporary_buffer<char>(MaxBufferSize);

    //Note that exception() == 0 in both file streams,
    //so you will not have a memory leak in case of fail.
    while(is.good() and os)
    {
       is.read(buffer.first, buffer.second);
       os.write(buffer.first, is.gcount());
    }

    std::return_temporary_buffer(buffer.first);

    if(os.fail()) return false;
    if(is.eof()) return true;
    return false;
}

#include <iostream>

int main()
{
   bool CopyResult = copy_file("C:\\Untitled.cpp","C:\Users\<my username>\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\Untitled.cpp");

   std::boolalpha(std::cout);
   std::cout << "Could it copy the file? " << CopyResult << '\n';
}
Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <fstream>
#include <memory>
#include <Windows.h>
#include <iostream>
#include <string>

using namespace std;

//C++98 implementation, this function returns true if the copy was successful, false otherwise.

bool copy_file (const char* From, const char* To, std::size_t MaxBufferSize = 1048576)
{
  std::ifstream is (From, std::ios_base::binary);
  std::ofstream os (To, std::ios_base::binary);

  std::pair<char*, std::ptrdiff_t> buffer;
  buffer = std::get_temporary_buffer<char> (MaxBufferSize);

  //Note that exception() == 0 in both file streams,
  //so you will not have a memory leak in case of fail.
  while (is.good () && os)
  {
    is.read (buffer.first, buffer.second);
    os.write (buffer.first, is.gcount ());
  }

  std::return_temporary_buffer (buffer.first);

  if (os.fail ()) return false;
  if (is.eof ()) return true;
  return false;
}

int main ()
{
  const int NAME_LEN = 256;

  string UserName (NAME_LEN, ' ');

  DWORD size = NAME_LEN;

  GetUserNameA (&UserName[0], &size);
  UserName.resize (size);
  string FileName = string ("C:\\Users\\") + UserName + string ("\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\Untitled.cpp");


  bool CopyResult = copy_file ("C:\\Untitled.cpp", FileName.c_str());

  std::boolalpha (std::cout);
  std::cout << "Could it copy the file? " << CopyResult << '\n';
}
Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <fstream>
#include <memory>
#include <Windows.h>
#include <iostream>
#include <string>

using namespace std;

//C++98 implementation, this function returns true if the copy was successful, false otherwise.

bool copy_file (const char* From, const char* To, std::size_t MaxBufferSize = 1048576)
{
  std::ifstream is (From, std::ios_base::binary);
  std::ofstream os (To, std::ios_base::binary);

  std::pair<char*, std::ptrdiff_t> buffer;
  buffer = std::get_temporary_buffer<char> (MaxBufferSize);

  //Note that exception() == 0 in both file streams,
  //so you will not have a memory leak in case of fail.
  while (is.good () && os)
  {
    is.read (buffer.first, buffer.second);
    os.write (buffer.first, is.gcount ());
  }

  std::return_temporary_buffer (buffer.first);

  if (os.fail ()) return false;
  if (is.eof ()) return true;
  return false;
}

int main ()
{
  const int NAME_LEN = 256;

  string UserName (NAME_LEN, ' ');

  DWORD size = NAME_LEN;

  GetUserNameA (&UserName[0], &size);
  UserName.resize (size);
  string FileName = string ("C:\\Users\\") + UserName + string ("\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\Untitled.cpp");


  bool CopyResult = copy_file ("C:\\Untitled.cpp", FileName.c_str());

  std::boolalpha (std::cout);
  std::cout << "Could it copy the file? " << CopyResult << '\n';
}

Nah, it doesn't work.
What's the problem ?
What's the problem ?

I compiled this code, but it doesn't copy the file and refers: Could it copy the file? false.
Where did you get this copy_file function from ?

Try using the normal Windows function CopyFile:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <fstream>
#include <memory>
#include <Windows.h>
#include <iostream>
#include <string>
#include <stdio.h>

using namespace std;

int main ()
{
  const int NAME_LEN = 256;
  char szUserName[NAME_LEN];
  DWORD size = NAME_LEN;

  GetUserNameA (szUserName, &size);
  char szFormat[] = "C:\\Users\\%s\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\Untitled.cpp";
  char szNewFileName[MAX_PATH] = {0};
  char szOrgFileName[] = "C:\\Temp\\Untitled.cpp";

  sprintf_s (szNewFileName, MAX_PATH, szFormat, szUserName);
  BOOL CopyResult = CopyFileA (szOrgFileName, szNewFileName, FALSE);

  if (CopyResult)
  {
    cout << "File copied successfully. ";
  }
  else
  {
    cout << "File error: " << GetLastError ();
  }
  system ("pause");
  return 0;
}
Last edited on
Sorry, but I can't remember.
OK, it's not really important. Try the code above. I edited it after you posted so you probably didn't see it.
OK, it's not really important. Try the code above. I edited it after you posted so you probably didn't see it.

error: 'sprintf_s' was not declared in this scope
Topic archived. No new replies allowed.