C++ 从目录中的指定文件夹打开文件。.exe在哪里
我需要帮助打开名为“编辑数据”的文件夹中的文件,该文件夹与我正在制作的.exe文件位于同一文件夹中。我不知道C++中如何打开文件夹中的文件。 我想要的例子C++ 从目录中的指定文件夹打开文件。.exe在哪里,c++,file,location,directory,C++,File,Location,Directory,我需要帮助打开名为“编辑数据”的文件夹中的文件,该文件夹与我正在制作的.exe文件位于同一文件夹中。我不知道C++中如何打开文件夹中的文件。 我想要的例子 ....\Program\Edit Data您可以使用dirent.h获取编辑数据文件夹中所有文件的数组,然后只需打开*.dat文件即可 或者您可以在X位置打开一个文件,其中包含要编辑的文件及其文件路径的列表,请记住该路径要么是完整的(“C:\Program files\edit Data”),要么是变量,例如(“…\edit Data”)。
....\Program\Edit Data您可以使用dirent.h获取编辑数据文件夹中所有文件的数组,然后只需打开*.dat文件即可
或者您可以在X位置打开一个文件,其中包含要编辑的文件及其文件路径的列表,请记住该路径要么是完整的(“C:\Program files\edit Data”),要么是变量,例如(“…\edit Data”)。如果我要写的是:“C:\Program files\edit Data”,那么这个“C:\Program files\edit Data”是否允许我访问该文件(要编辑的文件).dat”?问题是您事先知道文件名,或者通过用户提供的参数(cin或getline)给出文件名。如果不这样做,您必须使用文件系统库搜索文件,这就是dirent.h的用途(或者您可以使用fseek),您也可以使用boost文件系统。对文件的访问取决于您程序的权限,如果您以管理员身份运行它,您应该不会有问题。好的,我刚得到dirent.h,我将尝试一下。现在我非常关注打开一个文件,我甚至可以这样做。它的接缝我无法让dirent.h正常工作,我不确定它是如何工作的。
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cmath>
#include <cstring>
using namespace std;
int main ()
{
string filename;//file that will be changed
//string path = "Mod/Mod.dat";//Path of Mod.dat that I could not get to work
//string file = path + "Mod.dat";//this was for opinging Mod.dat from folder call Mod, didnt work
long size;
char* memblock;
cout << " Enter a file to be modded by Mod.dat ";
cin >> filename;
ofstream infile ( filename ,std::ofstream::binary | ofstream::in);//the file that will be opened and changed)
//"filename: I want to open it from a folder called Edit Data
ifstream modFile ( "Mod.dat" , ifstream::binary);// (mod.dat is the file that i get the first 1840 hex values from)
if (!infile){
cout << " Couldn't open " << filename << endl;
}
if (!modFile){
cout << " Couldn't open " << modFile << endl;
}