从同一文件夹读取文件(c++程序)

Reading files from same folder (c++ program)

本文关键字:c++ 程序 文件 读取 文件夹      更新时间:2023-10-16

我有一个绘制地球的程序,它使用以下代码读取纹理文件:

Images::RGBImage surfaceImage;
surfaceImage=Images::readImageFile("",Vrui::openFile("/home/rodrtu/Desktop/SolarSystem/land_shallow_topo_2048.png"));`

我设置的方式只在我的桌面上工作,但我希望其他人可以访问我的程序文件和图片,并能够让程序在他们的计算机上工作。我应该用什么来代替"/home/rodrtu/Desktop/SolarSystem/land_shallow_topo_2048.png"

如果我将一个文件夹添加到与我的。cpp文件相同的位置,我应该更改我的makefile吗?这是我的makefile

VRUI_MAKEDIR := /opt/local/Vrui-2.6/share/make
ifdef DEBUG
  VRUI_MAKEDIR := $(VRUI_MAKEDIR)/debug
endif
INSTALLDIR := $(shell pwd)
# Set resource directory: I added this images folder to the same place as my 
# .cpp file, but it still doesn't work
RESOURCEDIR = images
########################################################################
########################################################################
# Include definitions for the system environment and system-provided
# packages
include $(VRUI_MAKEDIR)/SystemDefinitions
include $(VRUI_MAKEDIR)/Packages.System
include $(VRUI_MAKEDIR)/Configuration.Vrui
include $(VRUI_MAKEDIR)/Packages.Vrui
# Set installation directory structure:
BININSTALLDIR = $(INSTALLDIR)/$(EXEDIR)
RESOURCEINSTALLDIR = $(INSTALLDIR)/$(RESOURCEDIR)
########################################################################
########################################################################
PACKAGES = MYVRUI
########################################################################
########################################################################
ALL = $(EXEDIR)/NewPlanet   
.PHONY: all
all: $(ALL)
########################################################################
#'make clean'
########################################################################
.PHONY: extraclean
extraclean:
.PHONY: extrasqueakyclean
extrasqueakyclean:
# Include basic makefile
include $(VRUI_MAKEDIR)/BasicMakefile
########################################################################
########################################################################

$(EXEDIR)/NewPlanet: $(OBJDIR)/NewPlanet.o $(OBJDIR)/drawShape.o 

您应该使用Beta建议的相对路径。在与可执行文件相同的文件夹中放置一个"data"文件夹,并使用:Vrui::openFile("./data/land_shallow_topo_2048.png")

文件打开应该是相对于程序目录的,因此您可以在源目录中为图片创建一个子目录。但是,请务必让用户知道放置这些图片的位置,

g-dev@g$ mkdir dat
g-dev@g$ mv pic.jpg dat/pic.jpg

::readImageFile("", Vrui::openFile("pic.jpg")

添加CMake目录:

include_directories ("${PROJECT_SOURCE_DIR/dat}")

添加VS目录:

这里

(确保您的文件路径$(ProjectDir)$(SolutionDir)使用了提供的MSVS宏)