I have school project and i need work with files. First of all i need open csv file which is in directory "vstupnidata" in root of my program directory.
My solution which doesn't work:
string directory= "..\\vstupnidata\\";
string file= "ucty2015.csv";
ifstream VstupniSoubor((directory+file).c_str());
if (!(VstupniSoubor.is_open())){ //I am always here
VypisChybyV1();
return 1;
}
When i change it to this and dont use directory "vstupnidata", everything is OK but i need open it from directory:
string directory= "";
string file= "ucty2015.csv";
ifstream VstupniSoubor((directory+file).c_str());
if (!(VstupniSoubor.is_open())){
VypisChybyV1();
return 1;
}
Related
I have a config.txt file which is in my source folder. I use it to store my baseURL. When i build the dll the config file gets capsulated into one dll.
I am trying to add a config file which will reside outside the dll, so that i can change the base url when needed.
using namespace std;
struct Config {
string baseurl;
};
void loadConfig(Config& config) {
ifstream fin("config.txt");
string line;
while (getline(fin, line)) {
istringstream sin(line.substr(line.find("=") + 1));
if (line.find("baseurl") != -1)
sin >> config.baseurl;
}
}
#and to access the values
Config config;
loadConfig(config);
Also can the dll file access an external config file when placed in the same folder.?
any solutions for this?
I'm trying to retrieve all files from the C:/ directory using Boost library.
I can retrieve all files when the input is a file path with a directory (e.g : C:\Windows), but I get an error when the specified path is only C:\. I also tried with C: but Boost search file from my project directory and not from the root.
I have also added an exclusion to C:\Windows and this part works great.
So how to launch recursive_directory_iterator from C:\ ?
Here is my code :
//string rootPath = boost::filesystem::current_path().root_directory().string();
string rootPath = "C:";
string exclusionPath = rootPath+"\\"+"Windows";
void myClass::getFile()
{
for (boost::filesystem::recursive_directory_iterator end, dir(rootPath); dir != end; ++dir)
{
string filePath = dir->path().string();
if (boost::filesystem::is_regular_file(*dir) && filePath.find(exclusionPath) == string::npos)
{
cout << filePath << endl;
}
}
}
If you are using the c++17 standard library, you can take advantage of the standard filesystem library. It works just like the boost filesystem and the two libraries have a very similar API.
You have to include the filesystem header via
#include <filesystem>
You can recursively iterate through each file in a directory by calling:
for (std::filesystem::directory_entry entry : std::filesystem::recursive_directory_iterator(rootPath))
That will give you a directory entry, which, just like the boost directory entry, contains a path. I was able to replicate your example code with the standard library and got a working example like so:
#include <filesystem>
#include <string>
#include <iostream>
std::filesystem::path rootPath = "C:";
std::filesystem::path exclusionPath = rootPath / "Windows";
int main()
{
for (std::filesystem::directory_entry entry : std::filesystem::recursive_directory_iterator(rootPath))
{
std::string filePath = entry.path().string();
if (std::filesystem::is_regular_file(entry.path()) && filePath.find(exclusionPath.string()) == std::string::npos)
{
std::cout << filePath << std::endl;
}
}
}
As you can see, I converted the strings you use for paths above to paths. This is not necessary but it is better to construct a path up-front, because else every function you call will construct a new path with the string you put into it. Paths weirdly use the / operator to append two paths together, so on Windows
std::filesystem::path exclusionPath = rootPath / "Windows";
will give you C:\Windows.
I'm working under windows 7 with mingw. I have encountered some weird behaviour with unicode filenames. My program needs to be portable, and I'm using boost::filesystem (v 1.53) to handle the file paths.
This has all been going well, until I needed to open files with unicode filenames.
This is not about the content of the file, but the file's name.
I tried the following: For testing I made a folder named C:\UnicodeTest\вячеслав and I tried creating a file inside of it, by appending the file name test.txt to the boost wpath.
For some reason the creation of the file fails. I'm using boost's fstreams and when I try to open the file, the failbit of the stream is set.
Now the funny thing is, that when I append a foldername to the path instead, a call to create_directories() succeeds and creates the correct directory C:\UnicodeTest\вячеслав\folder.
I really don't understand why it won't work with a file. This is the code I use:
boost::filesystem::wpath path;
// find the folder to test
boost::filesystem::wpath dirPath = "C:\\UnicodeTest";
vector<boost::filesystem::wpath> files;
copy(boost::filesystem::directory_iterator(dirPath), boost::filesystem::directory_iterator(), back_inserter(files));
for(boost::filesystem::wpath &file : files)
{
if(boost::filesystem::is_directory(file))
{
path = file;
break;
}
}
// create a path for the folder
boost::filesystem::wpath folderPath = path / "folder";
// this works just fine
boost::filesystem::create_directories(folderPath);
// create a path for the file
boost::filesystem::wpath filePath = path / "test.txt";
boost::filesystem::ofstream stream;
// this fails
stream.open(filePath);
if(!stream)
{
cout << "failed to open file " << path << endl;
}
else
{
cout << "success" << endl;
}
If I understand the issue correctly, the issue of being unable to create a file directly within C:\UnicodeTest\вячеслав occurs when you do not create the folder directory, as illustrated below.
// create a path for the folder
//boost::filesystem::wpath folderPath = path / "folder";
// this works just fine
//boost::filesystem::create_directories(folderPath);
// create a path for the file
boost::filesystem::wpath filePath = path / "test.txt";
I was able to get this to work by making the filename a wchar_t string:
// create a path for the file
boost::filesystem::wpath filePath = path / L"test.txt";
I am working on a mini-shell, and am trying to set the execution path dynamically.
I am using setvar() to set the PATH and when I check it with getvar() it reads the new PATH.
Here are the two things I tried to execute:
1. I change the PATH to /bin which I know contains most system functions, but no programs can work. I can also verify that the path has been set in the environment. I execute the commands using execvp()
2. I then used extern char** environ to get the environment and pass it into execvpe() but it still doesn't work.
I solved this by using istringstream to manually search the PATH and then use that to execute the process:
string dir;
string path = get_var("PATH");
istringstream search(path);
while(search.good()) {
getline(search, dir, ':');
if(dir != "") {
struct stat st;
if(dir[dir.length()] != '/') dir.append("/");
string file = dir + cmdArg[0];
//is file in dir?
if(stat(file.c_str(), &st) == 0) {
execvp(file.c_str(), cmdArg);
}
}
}
I am a newbie in c++ and boost. I am attempting to read (later write) to INI file using boost::program_options. I even tried using boost::property_tree.
Both(program_options & property_tree) work perfectly when std::stringstream s("[test]\n""a=2\n""b=3\n") is used, BUT NOT when std::ifstream s("dimension.ini"). I have put files: dimension.ini, Rcasdim.hpp/cpp in the same folder, and also have relevant boost lib files in search directory.
INI File
[Section]
a=2
b=3
Purpose:
I need to dynamically set the "Value" (at the start ONLY) for a Particular "Key" in INI file & Later USE that Previously set "Value" for that "Key" by other project files (more, as a toggle)
#include boost/program_options/detail/config_file.hpp
#include boost/program_options/parsers.hpp
namespace pod = boost::program_options::detail;
class CRcasdim
{
public:
CRcasdim(){};
~CRcasdim(){};
std::string getrcasdim(float);
private:
std::string sd;
};
std::string CRcasdim::getrcasdim(float d)
{
//std::stringstream s("[Section]\n""a=2\n""b=3\n"); WORKS
std::ifstream s("dimension.ini"); DOESNT WORK
if(!s)
{
std::cerr<<"error"<<std::endl;
}
std::set<std::string> options;
std::map<std::string, std::string> parameters;
options.insert("Section.a");
options.insert("Section.b");
try
{
for (pod::config_file_iterator i(s, options), e ; i != e; ++i)
parameters[i->string_key] = i->value[0];
}
catch(std::exception& e)
{
std::cerr<<"Exception: ";
}
if (d==2)
sd = parameters["Section.a"];
else if (d==3)
sd = parameters["Section.b"];
return sd;
}
You don't need to put the ini file and the hpp/cpp files in the same folder.
The dimension.ini file has be in the same folder like your binary(executable on linux .exe on windows).
The location depends on your build system and your platform and most probably some things I forgot.