0

私はファイルにデータを書き込むためのC ++の関数を持っています:

static int SaveFile(lua_State *L)
{
  string file = lua_tostring(L,1);
  string data = lua_tostring(L,2);

  while(true)
  {
      string::size_type position = data.find ("\\");
      if (position == string::npos) break;
      else
          data.replace(position, 1, "/");
  }
  cout << data << endl;
  //myfile.clear();
  myfile.open (file.c_str(), ios::out);
  if (myfile.is_open())
  {
      myfile << data << endl;
      myfile.close();
  }
  else
    cout << "Error occured. #126 > Failed to read data from file: " << file << endl;

  cout << data << " should be saved to " << file.c_str() << endl;
  string data2;
  myfile2.open(file.c_str(), ios::in);
  string line;
  while (getline(myfile2, line))
  {
      data2 = data2 + line;
  }
  cout << data2 << " was read from the same file.." << endl;
  myfile2.close();
  myfile2.clear();
  return 0;
}

次に、その関数を登録します

lua_register(L, "SaveFile", SaveFile);

そして私が電話するとき

luaL_dostring(L, "SaveFile('settings.lua', 'settings = {location = ''}')");

プログラムディレクトリに空のファイルを正常に作成し、データを書き込みます。問題は、WinApi を使用してフレームを作成している (lua 関数としても登録されている) ことですが、次を使用する場合:

luaL_dostring(L, "SaveFile('settings.lua', 'settings = {location = ''}')");

中身

LRESULT CALLBACK WinProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)

関数 SaveFile がトリガーされ、「cout」は、どのデータがどのファイルに挿入されたかを正しく示し、ファイルを閉じて再度開き、ファイル内のデータを再度読み取ります。これはすべて完全に機能しているように見えますが、プログラムのディレクトリにファイルが作成されません..

プログラムはデスクトップにあります。ルアコード:

SaveFile('settings.lua', 'settings = '..TableToString(settings)) -- no problem to write to a file
locateButton = CreateFrame("BUTTON")
locateButton:SetScript(function()
 settings.location=LocateFile("*.*");
 print(settings.location) -- prints correct address of selected file (c:/users/jan/desktop/settings.lua
 SaveFile('settings.lua', 'settings = '..TableToString(settings)) -- only prints data, but file is not created or overwritten
end)
4

0 に答える 0