It seems like a simple problem, but I just did not manage to figure it out, even with the help of the Internet.
So basically, because my project became rather big on files, I wanted to clean up the working directory by creating subfolders. So I moved e.g. shader files to Ressources/Shaders/ .
Now I just wanted to load them in as usual with the new path, but I always get an error.
ifstream fin("Ressources/Shaders/texture.vs");
does not work. as well as
.\\Ressources\\Shaders\\textures.vs
./Ressources/Shaders/textures.vs
/Ressources/Shaders/textures.vs
\\Ressources\\Shaders\\textures.vs
Ressources/Shaders/textures.vs
Ressources\\Shaders\\textures.vs
I also tried without capital letters.
while
"texture.vs"
worked when it still was in the working directory.
I appreciate any tips.
In Visual Studio, you can check what your current working directory is (this path must match the path of the file you are trying to open):
char * dir = getcwd(NULL, 0);
cout << dir << endl;
Then you can check if your file exists by trying to open it (capitalization shouldn't matter):
ifstream fin("Ressources\\Shaders\\texture.vs");
if (fin)
cout << "File Exists" << endl;
else
cout << "File Doesn't Exist" << endl;
If your paths are correct, this should work.
If you want to see what path you're actually accessing you can do the following:
char * dir = getcwd(NULL, 0);
printf("%s\\Ressources\\Shaders\\texture.vs", dir);
Related
I was unable to access files in a program and drafted (mostly copied and pasted) a segment for testing doing so- just to troubleshoot.
fstream file;
file.open("thishere.txt");
if(file.is_open())
{
cout << "Managed to open file" << endl;
file << "Here's some info for muh file\n";
cout << "Attempted to give info to file, read: " << endl;
string line;
while ( getline (file,line) )
{
cout << line << '\n';
}
} else cout << "Failed to open file" << endl;
The program returns Managed to open file \n Attempted to give info to file, read: and stops there. There is text already in the file to read off of and it does not return anything. Also nothing new appears on the file.
So it manages to neither write down information on the file, nor read information from the file, even though it is supposedly open. When I delete the file from the directory my program says Failed to open file. I've heard of IDE's using the wrong directory so I manually used the EXE in the directory I wanted and got the same results. I have permissions to the file, and am running the EXE as administrator.
What am I missing?
I'm using Windows 8 if that's pertinent
EDIT:
I solved my own problem if anyone cares, it was only half the code. There was no file.close() so the file did not save. Kenny Orstroms answer also solved my problem of no text appearing. All good now!
I have the following code
string fileName = "assets/maps/main.json";
std::ifstream file(fileName);
std::string temp;
if(!file.good())
{
LOG(logERROR) << "Failed to open map file: " << fileName;
//return;
}
LOG(logDEBUG) << "file Char Count: " << file.gcount();
while(std::getline(file, temp))
{
mapString += temp;
}
file.close();
This code works superbly on Windows 8. When I take this program over to OS X, the file fails to open 100% of the time. Or to be more concise, file.good() never returns true. I intentionally commented out the return there to help debugging for later code.
Anyway, this has driven me insane. I cannot figure out why it's failing on OS X. I've tried different directories, re-created the file on OS X to make sure it wasn't an encoding or line-end issue, nothing at all.
What else can I do to debug, or what might I try as an alternative?
I've also checked the file permissions themselves and they are all fine. I have many other types of files in the same directory structure (images, music, fonts) and they all open fine, it's just this JSON file that fails, and any new derivatives of this file also fail.
When you start a program on Linux or MacOSX, the working directory will be wherever the user is. So, if your game needs to find files, you need to make use of the appropriate preference system. Mac has a concept of a 'bundle' that allows a program to come with data files and use find them, you'll have to learn how to make one. You can look inside all the '.app' directories in your /Applications directories for many examples.
I'm having trouble validating the existence of REGISTER.txt for input purposes in a function (see below). My understanding is that if the file doesn't exist, then the file won't be opened and the file stream variable (inData) will be false. Thus, I can use that variable in an if/else statement to verify whether or not it opened. But even though REGISTER.txt is in the same directory as my .cpp file, my code still says that it wasn't opened.
Here's the thing though. When I run the same exact code in Dev-C++ compiler, it works fine and the file is found. Now, I understand compilers are different, but I don't understand what is causing the discrepancy here. My preferred IDE is Xcode, so I'd like to learn how to do I/O with files in Xcode.
Thanks in advance for the help.
P.S. My Xcode project references the file, so it's not like the project isn't connected with the file.
void ReadVehicleRegInfo(char& vehicleType, string& licensePlate,
int& modelYear, float& origTaxValue, bool& error)
{
ifstream inData;
string inputFile = "REGISTER.txt";
inData.open(inputFile.c_str()); //File contains registration info
if (!inData) {
//File does not exist. Exit function
cout << inputFile << " does not exist. Program will now terminate"
<< endl << endl;
error = true;
return;
} else {
//File exists - continue with program
cout << inputFile << " found";
}
inData.close();
}
In my main() function, I have the following code to signal to the user that an error has occurred:
if (error) {
//Function encountered error. Exits program
system("PAUSE");
return 99;
}
EDIT
I spent 40 minutes trying to figure this out, 15 writing the question, and 5 minutes after I post it I make huge progress. Don't you love that?
I put in the full directory to the file and that did the trick.
However, this is not ideal. The next question is how do I avoid having to do that? What is the default directory for Xcode?
Normally it would be the directory where your program lives. If you want to make sure, use _getcwd to get the current directory or just include the parent directory.
char *_getcwd(
char *buffer,
int maxlen
);
However, you should try not to use the full path for the reason it might not be the same when you run your program on another computer.
REGISTER.txt is in the same directory as my .cpp file
REGISTER.txt needs to be in the same directory as the binary (build/Release or elsewhere depending on your build settings)
At the end of a simulation, I want to write some results as an appended row to a data file. The code I am using is the following, where you can assume that outFile was correctly allocated as an std::ofstream, that output_file is a std::string containing a valid path to a file that does not yet exist, and that the variables printed out to the file are just int types that get values during the simulation.
outFile.open(output_file.c_str(), std::ios::out | std::ios::app );
outFile << num_nodes << ", " << tot_s << ", " << tot_c << ", " << tot_d << std::endl;
outFile.close();
I've checked whether it correctly opens the file with the ofstream::is_open() function and it returns false. However, I can't figure out why. I've tried it with many different file names and directory paths, all of which I have checked and they are valid (no typos, etc.)
The file being written is just into a folder on the desktop where I create files all the time, so I don't see how it could be a permissions issue. If it was a permissions issue, how can I check that?
Otherwise, what else can be preventing it from writing to the file?
Added:
Following up on the comments, after adding a call to perror(), it is displaying the "No such file or directory" error. The file path in question is:
/home/ely/Desktop/Evolutionary_Dynamics/GamesOnCycle/data/test.data
I want this file to be created, and all the directories in that path exist, it's all spelled correctly, etc., and there are no weird permission issues with the GamesOnCycle folder or its data subfolder. Note that it is a linux system (Ubuntu 11.04) so the forward slashes are correct for the file path, unless I'm missing something that C++ has to have w.r.t. file paths.
This could be happening due to several reasons.
1) The file is already open.
2) All the directories in the file path are not created.
3) Lack of file permissions.
For an additional reference, please see When will ofstream::open fail?
This may sound bad, but are you on windows or linux? If windows, for your file path, do you have it defined with double "\" in your string, or just one? If just one, you aren't putting the characters in your path that you think you are. To be safe, use the "/" character.
So if you had this:
string pathname = "C:\Users\me\Desktop";
That is NOT a valid path. You are escaping "\U", "\m" and "\D" into your string. You'd need this:
string pathname = "C:\\Users\\me\\Desktop";
or
string pathname = "C:/Users/me/Desktop";
The "/" isn't an escape character.
It's what seems likely to me.
I am using Boost library in C++ running in Ubuntu enviroment. I have some questions that I am not clear about:
fs::is_directory
namespace fs = boost::filesystem;
fs::path full_path(fs::initial_path<fs::path>() );
full_path = fs::system_complete(fs::path( "temp/"));
if(fs::is_directory(full_path ))
{
cout << "the path is a directory" << endl;
}
else
{
cout << "the path is not a directory" << endl;
}
=> I am sure that the moment I am running the program, there is a directory temp at the same location with the executable file. But it is always returned: "the path is not a directory" ?
fs::last_write_time
Is this fs::last_write_time(path) be able to get the last date time of modifying for the given path for BOTH either a directory or a file?
If it is true also for a directory, is that true for only the directory when it was created or the last date time if I add a file to inside the folder as well?
fs::directory_iterator
fs::directory_iterator dir(full_path) => how can I check whether this 'dir' has any sub directories or not?
Is there any way in boost::fileSystem to check if a file is opening?
Thanks in advance and I hope you could help me to make my mind clear!
Seems like that should work. Why don't you put a cout << fullpath before the if to make sure the path really contains what you think it does?
I've never used last_write_time. Can't help you with that one.
You have to iterate over the directory's contents and use fs::is_directory(dir->status()) to determine whether a given directory entry is a directory or not. (assuming dir is your directory iterator)
I don't believe there is anything in boost::filesystem to tell you if a file is open or not.
re 4.: Can't you find out if a file is currently open by simply calling [boost::filesystem|std]::fstream::is_open()?