mkdir() isnt working with absolute filepath - c++

I am using a raspberry pi to store data collected from a rocket launch. I am trying to create a directory which has the date and time built in, to hold multiple text and .csv files which hold interesting data. My code looks like:
string date = getDateTime(); //Returns a string like "Launch_2017_04_28_23:31:03"
string dated_directory = "~/Launch_System_Cpp/Source_Code/Launch_Data/" + date;
cout << dated_directory << endl;
if (mkdir(dated_directory.c_str(), ACCESSPERMS )) {
cout << "ERROR creating dated directory" << endl;
}
The executable itself is located in ~/Launch_Code_Cpp/.
I have been able to get this working with relative references, but I want this code to work no matter the directory it was run from. I haven't been able to get it working with absolute references; it always enters the if statement, and the directory doesn't exist when the program exits.
Can you tell me what I may be doing wrong when I try to make this directory? Is there a better way to make this directory?

The ~ character is converted to the home directory by the shell. Since you're not using a shell, you need to expand that yourself.

Related

What would cause ifstream code to fail on OS X?

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.

Xcode c++ cant access file through code

I have added an image "padimage.png" to my resources folder and set add to target and make copy if needed checked. Then in my c++ code I have the following code to check if it can reach the file
std::ifstream my_file("padimage.png");
if (my_file.good())
{
std::cout << "could read file \n";
} else {
std::cout << "could not read file \n";
}
This fails meaning I can't reach the file. I have checked in the debug build folder and the image is there under the resources folder, I have also tried alternative paths to the file like "resources/padimage.png" || Resources/padimage.png || ../Resources/padimage.png etc. etc.
I am fairly new to c++ still so I don't quite understand how it is suppose to find files or what path it searches relative to. Also I am sure this is quite an easy problem but I somehow can't solve it.
All help is much appreciated.
Just for your own sanity, do the following before anything else.
char wd[1024];
std::cout << getcwd(wd, sizeof(wd)) << std::endl;
You may be surprised at where you are, and thus why you can't open your file. When running from the IDE you can specify the location of your working directory under the Product/Edit Schemes... area of Xcode (among other places).
Thanks to a suggestion from WhozCraig I have managed to get it working by using the root of the project and then creating a standalone file next to the application like so:
./padimage.png
however this is not ideal. This means I would have resources outside of the project.
But after some trial and error I managed to navigate into the programs package contents by using .app to the package name;
./ProjectName.app/Contents/Resources/padimage.png

Use of `ofstream` appears not to create nor write to file

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.

C++: boost/filesystem: some questions

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()?

Program that modifes string inside its exe

I looking for example of program, that modifies a string inside its exe.
I work with C++, Visual Studio under Windows.
I searched working examples in Windows, but I can't find any working code.
I need simple code, that will ask user for string:
string strTest = "";
(if strTest != "")
{
cout << "Modified: " << strTest << endl;
}
cin >> strText;
And code should rewrite:
string strTest = "";
To string that typed user:
string strTest = "SomeStringFromUser";
How, in C++, do you modify a string (from string strTest = ""), to string, what a user typed? (for example to strTest = "foo")?
When an EXE is running on a Windows machine, the exe file is held open as a CreateFileMapping object with pages marked either as READONLY or COPY_ON_WRITE.
So when the exe writes to itself, the file is not modified. It just creates a new page backed by the swap file. But since the file is kept open, no-one else can open the EXE file and write to it either.
Other than hacking the page protection to turn off COPY_ON_WRITE - Which I'm not sure is even possible. The only way I can think to do this would be to write a little program that runs after your exe finishes and opens the .exe file and writes to it.
I've gotta believe that whatever you are trying to do, there is a better way to go about it.
--- Later ----
Ok, I get it now. you are looking to watermark your exe. Here's the thing, this is pretty easy to do in your installer before the exe starts. But once the .exe is running it's MUCH harder to do.
Here's what I would do.
declare a global string variable of the necessary size, say const char g_szWatermark[100] = "";
Build my exe, then look in the map file to find the address of the variable within its segment (remember about C++ name decoration)
parse the EXE header to find the where the segment begins in the exe.
add these two numbers to get the location of the string within the exe file, give this number to my installer
have the installer run a little program that asks the user for information and then writes it into the .exe. Note: you have do do this before the exe runs or it won't work!
A licensing scheme based on some open, established cryptographic mechanism is going to be your most robust solution. Something using standard PKI should be much simpler and more secure than attempting to implement self-modifying code.
To be honest though, there are a lot of companies that spend a lot of money on R&D creating copy protection and those systems are cracked within days of release. So if you're trying to thwart crackers then you have a long, hard road ahead.
If you just want to keep the honest people honest, a simple online activation using a GUID as the "license key" would be quite effective.
How about this:
#include <string>
#include <iostream>
int main()
{
std::string strTest = "";
std::getline(std::cin,strTest);
if (strTest != "")
{
std::cout << "Modified String: " << strTest << "\n";
}
else
{
std::cout << "Not modified\n";
}
}