L
L
Lnkova2020-12-07 16:42:03
C++ / C#
Lnkova, 2020-12-07 16:42:03

Why does it give error C2664?

I'm learning WinApi, I was asked to write a file manager.

#include "pch.h"
#include <iostream>
#include <windows.h>
#include <string>
#include <fstream>

using namespace std;
int main()
{
  HANDLE hFindFile, hFile;
  WIN32_FIND_DATA fd, file_inf[100];
  int i, counter;
  counter = 0;
  string comand, directory, prev, file_name, user_path, addition[100];
  directory = "D:\\";
  prev = "D:\\";
  bool x = true;
  i = 0;
  while (true) {
    cout << "help to see all comands" << endl;
    cin >> comand;
    if (comand == "help") {
      cout << "dir - show all files in current directory" << endl;
      cout << "cd  - enter directory" << endl;
      cout << "kill - delete file" << endl;
      cout << "text - create file" << endl;
      cout << "copy - copy file" << endl;
    }
    if (comand == "dir") {
      cout << "_____________" << endl;
      hFindFile = FindFirstFile ((directory + "*.*").c_str(), &fd);
      file_inf[0] = fd;
      while (FindNextFile(hFindFile, &fd)) {
        i++;
        file_inf[i] = fd;
      }
      int n = i;
      for (i = 0; i <= n - 1; i++) {
        cout << file_inf[i].cFileName << endl;
      }
      cout << "_____________" << endl;
      i = 0;
    }
    if (comand == "cd") {
      cout << "directory name?" << endl;
      cin >> addition[counter];
      if (addition[counter] == "..") {
        counter--;
        directory = prev;
      }
      else {
        counter++;
      }
      for (int i = 0; i < counter; i++) {
        directory += "\\" + addition[i] + "\\";
      }
      cout << directory << endl;
    }
    if (comand == "kill") {
      cout << "file's name?" << endl;
      cin >> file_name;
      hFindFile = FindFirstFile((directory + file_name + "*.*").c_str(), &fd);
      file_inf[0] = fd;
      cout << directory + file_name << endl;
      DeleteFile((directory + file_inf[0].cFileName).c_str());
    }
    if (comand == "create") {
      cout << "file's name?" << endl;
      cin >> file_name;
      hFile = CreateFile((directory + file_name).c_str(),
        GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
      CloseHandle(hFile);
    }
    if (comand == "copy") {
      cout << "file's name?" << endl;
      cin >> file_name;
      cout << "new folder?" << endl;
      cin >> user_path;
      CopyFile((directory + file_name).c_str(), (user_path + file_name).c_str(), false);
    }

  }
  system("pause");
  return 0;
}


Throws 4 errors of type: C2664 "HANDLE FindFirstFileW(LPCWSTR,LPWIN32_FIND_DATAW)": Cannot convert argument 1 from "const _Elem *" to "LPCWSTR". Tell me how to be?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Adamos, 2020-12-07
@Lnkova

If the function has the last letter A, it expects char * as arguments.
If W - then she needs wchar_t *.
So instead of string you need to use wstring.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question