Visual c++ fstream无法打开Wix工具集3安装的文件

Visual c++ fstream无法打开Wix工具集3安装的文件,visual-c++,fstream,wix3,Visual C++,Fstream,Wix3,最近,我使用Wix工具集3创建了一个安装程序。它所做的是将png和json文件安装到用户的PC上,以及一个我一直在开发的游戏的exe文件,该文件要求所有png和json文件正常工作。该程序使用fstream读取json文件,并将其中的数字转换为地图系统的分幅。但是,当用户使用安装程序安装我的程序时,我的程序无法读取文件。我已经使用if语句验证了它,以测试文件是否可以打开,如果不能打开,甚至不尝试生成映射。此外,如果用户复制他们获得的json文件,将其粘贴到其他地方,删除安装程序放在其PC上的原始

最近,我使用Wix工具集3创建了一个安装程序。它所做的是将png和json文件安装到用户的PC上,以及一个我一直在开发的游戏的exe文件,该文件要求所有png和json文件正常工作。该程序使用fstream读取json文件,并将其中的数字转换为地图系统的分幅。但是,当用户使用安装程序安装我的程序时,我的程序无法读取文件。我已经使用
if
语句验证了它,以测试文件是否可以打开,如果不能打开,甚至不尝试生成映射。此外,如果用户复制他们获得的json文件,将其粘贴到其他地方,删除安装程序放在其PC上的原始文件,并将其替换为复制的文件,则程序可以打开该文件。以下是地图读取类(map.cpp和map.h)的代码和安装程序(Product.wxs)的代码:

(在地图h中):

(引自Igor Tandetnik):


“将
fstream
替换为
ifstream
。默认情况下,前者尝试打开文件进行读写。但出于安全原因,默认情况下,C:\Program Files下的目录不可由非管理员写入。这就是程序在其他地方复制时工作的原因。”

fstream
替换为
ifstream
。默认情况下,前者尝试打开文件进行读取和写入。但出于安全原因,
C:\Program Files
下的目录默认情况下不可由非管理员写入。这就是为什么你的程序在其他地方复制时可以工作。啊,这是有道理的。谢谢你的帮助!我会试试看,然后告诉你是否有效。好的,成功了。非常感谢。
#pragma once
#include <string>

class Map {
public:

    Map();
    ~Map();

    static void LoadMap(std::string path, int sizeX, int sizeY, int layers);
};
#include "Map.h"
#include "Game.h"
#include <fstream>

Map::Map() {
}

Map::~Map() {
}

void Map::LoadMap(std::string path, int sizeX, int sizeY, int layers) {
    char tile;
    std::fstream mapFile;
    mapFile.open(path);
    if (mapFile.is_open()) {
        for (int l = 0; l < layers; l++) {
            for (int y = 0; y < sizeY; y++) {
                for (int x = 0; x < sizeX; x++) {
                    mapFile.get(tile);
                    Game::AddTile(atoi(&tile), x * 64, y * 64);
                    mapFile.ignore();
                }
            }
        }
    }
    mapFile.close();
}
<?xm version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"
  xmlns:util="http://schemas.microsoft.com/wix/UtilExtension">
    <Product Id="fb88098c-8720-4f04-bb0d-38432fb62c1b" 
           Name="Ophicyte" 
           Language="1033" 
           Version="0.0.0.1" 
           Manufacturer="U-Bit Company" 
           UpgradeCode="49eaae97-93b9-4e34-ab73-d21c5e3b74d1">

    <Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine" />

        <MajorUpgrade DowngradeErrorMessage="A newer version of Ophicyte is already installed." />
        <MediaTemplate />

        <Feature Id="ProductFeature" Title="Ophicyte" Level="1">
            <ComponentGroupRef Id="ProductComponents" />
        </Feature>
    <Icon Id="mainicon.ico" SourceFile="..\UBitEngine\assets\mainicon.ico"/>
  <Feature Id="Uninstall">
    <ComponentRef Id="UninstallFolder" Primary="yes"/>
  </Feature>
    </Product>

    <Fragment>


    <Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="DesktopFolder" Name="Desktop" >
        <Component Id="OphicyteDesktopShortcut" Guid="95ba12b1-e9d7-467e-9008-e46630122a1a">

        <Shortcut Id="OphicyteShortcutDesktop"
                  Name="Ophicyte"
                  Target="$(var.UBitEngine.TargetFileName)"
                  WorkingDirectory="TARGETDIR"
                  Icon="mainicon.ico"
                  IconIndex="0"
                  Advertise="no"/>
          <RemoveFolder Id="DesktopFolder" On="uninstall"/>

           <RegistryValue
             Root="HKCU"
             Key="Software/Ophicyte"
             Name="installed"
             Type="integer"
             Value="1"
             KeyPath="yes"/>
          </Component>
      </Directory>

        <Directory Id="ProgramFilesFolder">
        <Directory Id="INSTALLFOLDER" Name="Ophicyte" >
          <Directory Id="ASSETSDIR" Name="assets">
            <Component Id="ASSETSFOLDER" Guid="c18af068-440c-4faf-8fb0-22da79ddfa53"
                         SharedDllRefCount="no" KeyPath="no" NeverOverwrite="no" Permanent="no" Transitive="no" Win64="no" Location="either">
              <RemoveFolder Id="ASSETSFOLDER" Directory="ASSETSDIR" On="uninstall"/>
              <CreateFolder/>
            </Component>
            <!--Icons:-->
            <Component Id='mainicon' Guid='a4788915-ff9a-4244-a330-d4084fa3aa59'>
              <File Id='mainicon' Name='mainicon.ico' Source='..\UBitEngine\assets\mainicon.ico' KeyPath='yes'/>
            <!--Sprites:-->
            </Component>
            <Component Id="CharacterFemale" Guid="f9e432c2-f74f-49ae-bb0c-e27d7184f578">
              <File Id='CharacterFemale' Name='Character(female).png' Source="..\UBitEngine\assets\Character(female).png" KeyPath='yes' />
            </Component>
            <Component Id='Character' Guid='52a62880-5e56-4128-94e9-38fd9b7908c9'>
              <File Id='Character' Name='Character.png' Source='..\UBitEngine\assets\Character.png' KeyPath='yes'/>
            </Component>
            <Component Id='GrassTufts1' Guid='24241c90-0af1-417c-984a-9e6d18d22315'>
              <File Id='GrassTufts1' Name='Grass Tufts1.png' Source='..\UBitEngine\assets\Grass Tufts1.png' KeyPath='yes'/>
            </Component>
            <Component Id='Grass1' Guid='aab9b2fa-4583-4d11-b355-09e09cd20f63'>
              <File Id='Grass1' Name='Grass1.png' Source='..\UBitEngine\assets\Grass1.png' KeyPath='yes'/>
            </Component>
            <Component Id='Ocean1' Guid='aae24e29-3eba-4880-9242-8d29fc2559e9'>
              <File Id='Ocean1' Name='Ocean1.png' Source='..\UBitEngine\assets\Ocean1.png' KeyPath='yes'/>
            </Component>
            <Component Id='Tree2night' Guid='c9493e18-2814-408d-b750-15127b218d44'>
              <File Id='Tree2night' Name='Tree2(night).png' Source='..\UBitEngine\assets\Tree2(night).png' KeyPath='yes'/>
            </Component>
            <Component Id='Tree2' Guid='75e4804c-d96f-4190-a411-06affe9e9ad9'>
              <File Id='Tree2' Name='Tree2.png' Source='..\UBitEngine\assets\Tree2.png' KeyPath='yes'/>
            </Component>
            <!--Sound effects:-->
            <Component Id='Slash3' Guid='03a64a3b-6234-4db8-b4e7-970a1d557979'>
              <File Id='Slash3' Name='Slash3.wav' Source='..\UBitEngine\assets\Slash3.wav' KeyPath='yes'/>
            </Component>
            <!--Maps:-->
            <Component Id='Overworld' Guid='cc8bb3a0-b0a3-48f2-8df6-2b7c5d96aa2d'>
              <File Id='Overworld' Name='Overworld.json' Source='..\UBitEngine\assets\Overworld.json' KeyPath='yes'/>
            </Component>
          </Directory>
        </Directory>
      </Directory>
    </Directory>
      <!--Features for icons:-->
      <Feature Id="CREATEMAINICON" Title="mainicon.ico" Level="1">
        <ComponentRef Id="mainicon"/>
      </Feature>
      <!--Features for shortcuts:-->
      <Feature Id='SHORTCUT' Title='Ophicyte' Level='1'>
        <ComponentRef Id='OphicyteDesktopShortcut'/>
      </Feature>
      <!--Features for folders:-->
    <Feature Id="CREATEASSETSFOLDER" Title="assets" Level="1">
      <ComponentRef Id="ASSETSFOLDER"/>
    </Feature>
      <!--Features for sprites:-->
    <Feature Id="CREATECHARACTERFEMALE" Title="Character(female).png" Level="1">
      <ComponentRef Id="CharacterFemale"/>
    </Feature>
      <Feature Id="CREATECHARACTER" Title="Character.png" Level="1">
        <ComponentRef Id="Character"/>
      </Feature>
      <Feature Id="CREATEGRASSTUFTS1" Title="Grass Tufts1.png" Level="1">
        <ComponentRef Id="GrassTufts1"/>
      </Feature>
      <Feature Id="CREATEGRASS1" Title="Grass1.png" Level="1">
        <ComponentRef Id="Grass1"/>
      </Feature>
      <Feature Id="CREATEOCEAN1" Title="Ocean1.png" Level="1">
        <ComponentRef Id="Ocean1"/>
      </Feature>
      <Feature Id="CREATETREE2NIGHT" Title="Tree2(night).png" Level="1">
        <ComponentRef Id="Tree2night"/>
      </Feature>
      <Feature Id="CREATETREE2" Title="Tree2.png" Level="1">
        <ComponentRef Id="Tree2"/>
      </Feature>
    <!--Features for sound effects:-->
    <Feature Id="CREATESLASH3" Title="Slash3.wav" Level="1">
      <ComponentRef Id="Slash3"/>
    </Feature>
    <!--Features for maps:-->
    <Feature Id="CREATEOVERWORLD" Title="Overworld.json" Level="1">
      <ComponentRef Id="Overworld"/>
    </Feature>
  </Fragment>

  <Fragment>
    <ComponentGroup Id="ProductComponents" Directory="INSTALLFOLDER">
      <Component Id="ProductComponent">
        <File Source="$(var.UBitEngine.TargetPath)" />
      </Component>
    <Component Id="libpng" Guid="60fb99cf-5a5f-4dc2-8928-9c0b5232d046">        
            <File Id='libpng' Name='libpng16-16.dll' Source="..\UBitEngine\libpng16-16.dll" KeyPath='yes' />
        </Component>
    <Component Id="SDL2" Guid="9ee9481a-d57d-499d-8732-574ac6338980">        
            <File Id='SDL2' Name='SDL2.dll' Source="..\UBitEngine\SDL2.dll" KeyPath='yes' />
        </Component>
    <Component Id="SDL2image" Guid="bc104401-71d7-4bbd-a2fc-00f7b31045c9">        
            <File Id='SDL2image' Name='SDL2_image.dll' Source="..\UBitEngine\SDL2_image.dll" KeyPath='yes' />
        </Component>
      <Component Id='zlib' Guid='a9732a61-c65f-4cfc-ac82-48da7d2f9808'>
        <File Id='zlib' Name='zlib1.dll' Source='..\UBitEngine\zlib1.dll' KeyPath='yes'/>
      </Component>
    </ComponentGroup>
  </Fragment>
  <Fragment>
    <ComponentGroup Id="Assets" Directory="ASSETSFOLDER"/>
  </Fragment>
  <Fragment>
    <Component Id="NEWCOMP" Guid="6b94d818-8cbc-45c7-8ded-37fc8048cc71" Directory="INSTALLFOLDER">
      <RemoveFile Id="RemoveInstallFolder2" Name="Ophicyte" On="uninstall" />
    </Component>
  </Fragment>
<Fragment Id="FolderUninstall">
  <?define RegDir="SYSTEM\ControlSet001\services\[UBitEngine]:[Ophicyte]"?>
  <?define RegValueName=InstallDir?>
<Property Id="INSTALLFOLDER">
  <RegistrySearch Root="HKLM" Key="$(var.RegDir)" Type="raw" Id="APPLICATIONFOLDER_REGSEARCH" Name="$(var.RegValueName)" />
</Property>
<DirectoryRef Id="INSTALLFOLDER">
  <Component Id="UninstallFolder" Guid="4e61c6b1-fe6f-4cbb-8000-b94cf5f333d8">
    <CreateFolder Directory="INSTALLFOLDER"/>
  <util:RemoveFolderEx Property="INSTALLFOLDER" On="uninstall"/>
    <RemoveFolder Id="INSTALLFOLDER" On="uninstall"/>
  <RegistryValue Root="HKLM" Key="$(var.RegDir)" Name="$(var.RegValueName)" Type="string" Value="[INSTALLFOLDER]" KeyPath="yes"/>
  </Component>
</DirectoryRef>
</Fragment>
</Wix>
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3
3,3,3,3,3,3,3,3,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3
3,3,3,3,3,3,3,1,3,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3
3,3,3,3,3,3,1,3,1,3,1,3,3,3,3,3,3,3,3,3,3,3,3,3
3,3,3,3,3,1,3,1,3,1,3,1,3,3,3,3,3,3,3,3,3,3,3,3
3,3,3,3,3,3,1,3,1,3,1,3,3,3,3,3,3,3,3,3,3,3,3,3
3,3,3,3,3,3,3,1,3,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3
3,3,3,3,3,3,3,3,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,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