Created
December 28, 2012 12:52
-
-
Save fpersson/4397477 to your computer and use it in GitHub Desktop.
a quick sample code to answare a beginers question about file paths in C++, this code is free to use for anyone.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
/** | |
* @brief a quick sample code to answare a beginers question about file paths in C++, this code is free to use for anyone. | |
*/ | |
class Path{ | |
public: | |
Path(std::string path, std::string file); | |
std::string getAbsulutePath(); | |
void touch(); | |
private: | |
std::string m_path; | |
std::string m_filename; | |
}; | |
Path::Path(std::string path, std::string file): m_path(path), m_filename(file){} | |
std::string Path::getAbsulutePath(){ | |
std::string ret=""; | |
ret.append(m_path); | |
ret.append(m_filename); | |
return ret; | |
} | |
/** | |
* This will only create the file if you want to write something checkout http://www.cplusplus.com/reference/fstream/ofstream/ | |
*/ | |
void Path::touch(){ | |
std::ofstream fout(getAbsulutePath().c_str()); | |
fout.close(); | |
} | |
int main(int argc, char **argv) { | |
std::string path; | |
std::string file; | |
std::cout << "Enter a path: "; | |
std::cin >> path; | |
std::cout << "Enter filename: "; | |
std::cin >> file; | |
Path filepath(path, file); | |
std::cout << "Your file is: " << filepath.getAbsulutePath() << std::endl; | |
filepath.touch(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment