0
votes

I need help with opening a file in a folder called "Edit Data" that is in the same folder as the .exe I am making. I am not sure of how the opening of files from folders is handled in c++. Example of what I want.

....\Program\Edit Data <- I want to open a file from this folder and edit it

....\Program\program.exe <- Location of .exe

I need to do this because I have multiple files inside this folder that the user can edit. I first tried to use a folder for Mod.dat called Mod but I was unsuccessful.

Half of my current code:

#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;
}

I don't know if this is important but I use Visual Studios and my program opens "infile" and edits it. I also want to open about 1000 files from the Edit Data folder and edit them with the mod.dat. ( Edit: As of now, I mainly just need to know how to read a single file from a folder.) Also is there a method where I can make a text document with the file name and location that the ofstream reads when I add it to the inside of a loop?

What the files would look like: ....\Program\Edit Data\12a.dat ....\Program\Edit Data\m9i3.dat ....\Program\Edit Data\2020.dat

1

1 Answers

1
votes

You can use dirent.h to obtain an array of all the files inside the Edit Data folder, afterwards you just open the *.dat ones.

Or you could open a file in X place with a list of the files you want to edit and their filepath, keep in mind that the path either is complete ("C:\Program Files\Edit Data") or variable such as ("..\Edit Data").