I am working on a project in which I need to read/open an .stl from any arbitrary directory. This is what I have for opening a file:
int ReadStl( std::string fname, std::vector<ReadFacet>& mesh)
{
std::ifstream ifs;
ifs.open(fname, std::ios::binary);
... stl reading and processing...
return 0;
}
As I understand it, I can pass the complete file path and name like so:
fname = "C:/Users/Luke/folder1/example.stl"
This works for the most part, but fails once I go more than 6 folders deep...
Any ideas on why this happens or what is wrong with my solution?
I am still relatively new to C++ so any input/corrections are welcomed.
Related
I am trying to read about 2 lines from a file of text into a std::string in c plus plus. I have looked through several answers and found none that work on my device. Can anyone tell me what I am doing wrong? The method is currently returning a null string, and doesn't correctly open the file or read it at all.
std::string readFile(std::string filename) {
std::ifstream infile;
infile.open(filename);
std::string output;
if (infile.is_open()) {
while(infile.good()) {
infile >> output;
}
}
infile.close();
return output;
}
Not sure what file you are trying to open but that's a completely separate problem. The code you've written will open a file if you give it a path to a file that it can open. Check your current working directory and confirm the path is correct.
Even after you solve that problem, you're going to have more problems though.
I expect that you are confused because you are repeatedly overwriting output with this line:
infile >> output;
perhaps you meant to declare output as a std::stringstream
And for me it doesn't return an empty string, it returns the last word of the file. I guess it depends what's in your file.
I want the line below to write a new file using the content given in the array
but into a new folder named logs:
char log_file_name[100]; /* this array contains the name of a new file */
ofstream write_to_log;
write_to_log.open (relative path, log_file_name , fstream::app);
How do I get it working ?
You can use CreateDirectory for creating folders with VC++ in Windows.
#include <windows.h>
#include <string>
#include <fstream>
using namespace std;
int main() {
string path = "C:\\users\\folder";
CreateDirectory(path.c_str(), NULL);
char log_file_name[100] = "log.txt";
path += '\\';
path += log_file_name;
ofstream write_to_log(path.c_str(), fstream::app);
return 0;
}
The NULL refers to a security attributes structure that you may have to create. More details at MSDN here and in this answer.
You can save your self a lot of potential trouble and replace char log_file_name[100]; with std::string log_file_name; The benefits of string are many, the most important here are they resize and they make appending really easy. The string does everything a char array does and a whole lot of extras. In virtually all cases, you should chose a string over a char array.
string path;
string log_file_name;
With the path and the file name as strings
path += "\\" + log_file_name
ofstream write_to_log(path, fstream::app);
if (write_to_log)
{ // file is open and looks writable (have to start writing to be sure)
// do stuff. Or not. It's a free country.
}
else
{ // file didn't open
// Handle error
}
All done and the file, if it exists and is writable, is open and ready to go. Always check the state of a stream when you use it. SO is littered with questions from people who didn't and got confused by the result.
On older compilers you may have to change the create and open line slightly:
ofstream write_to_log(path.c_str(), fstream::app);
I am using Visual Studio 2013
So I have to open a .ppm image file and do some work on it, but the ifstream I'm trying to use to read the data fails to open the image file. I am pretty sure the image file is in the working directory (I have created and read some simple .txt files to make sure). And even after excessive research I can't figure out what is going on.
Here's the relevant code
EDIT: I added some more code to get an idea of what I'm trying to do
Image * PPMImageReader::read(std::string filename){
std::string line;
int width, height, max_val;
std::ifstream src(filename, std::ios_base::binary);
if (src.fail()) { //failbit is always set but not badbit
perror("Logical error on i/o operation. failbit was set\n");
if (src.bad())
perror("Read/writing error on i/o operation. badbit was set");
}
if (!src.is_open()) { //and of course this return true
printf("File was not opened\n");
exit(1);
}
//Edited
getline(src, line, '\n');
if (line.empty())
getline(src, line);
if (line.find("P6") == std::string::npos) {
printf("wrong format\n");
exit(1);
}
As from discussed came to know the problem is relative path.
fstream support relative paths as below..
Consider the following case where your input file is one level up than exe file.
E:\MyProgramBin\YourExe.exe
E:\YourInputFile.ppm
In this case, you can create your filename as below.
filename1 = "..\YourInputFile.ppm"
and use that filename1 in ifstream
std::ifstream src(filename1, std::ios_base::binary);
I'm trying to read in some GLSL source from simple plain text files using the following.
I provide the sources to the constructor like so:
Shader *shader = new Shader("res/default.vert", "res/default.frag");
The constructor passes those paths on directly to my little OSUtils file that reads in plain text and returns std::string:
Shader::Shader(const std::string& vertPath, const std::string& fragPath)
{
addVertexShader(vertPath);
addFragmentShader(fragPath);
}
void Shader::addVertexShader(const std::string& path)
{
try {
const std::string shaderSrc = OSUtils::fileToBuffer(path);
compileVertexShader(shaderSrc);
} catch (int e) {
std::cout << "Error no: " << e << std::endl;
exit(1);
}
}
The addFragmentShader function is nearly identical.
The fileToBuffer function is as such:
const std::string OSUtils::fileToBuffer(const std::string& path)
{
std::cout << "Trying to load: " << path << std::endl;
std::ifstream in(path, std::ios::in | std::ios::binary);
if (in)
{
std::string contents;
in.seekg(0, std::ios::end);
contents.resize(in.tellg());
in.seekg(0, std::ios::beg);
in.read(&contents[0], contents.size());
in.close();
return(contents);
}
throw(errno);
}
This throws the error with Errno 2 (No file found) in the addVertexShader function. And here's the kicker. I have this EXACT code running in a different project. Most of this code was copypasta from that earlier project that compiles just fine.
I'm running both projects in Xcode 5. The files themselves (res/default.vert and res/default.frag) have been added to the target and are listed in the Compile Sources listing under the target's Build Phases tab. In other words, everything seems in order, but I can't figure out why it won't load the files in this project. Is there some mysterious Xcode setting I'm missing? I'd suspect the code if it wasn't 100% identical to the code in the working project. The other project also mimics the file structure with both the .vert and .frag files living in a res/ folder. The only difference is that the source files that attempt to read the files live in a src/ subdirectory. My understanding though is that I should be able to reference the files relative to the project, not the actual source files themselves. Just in case, I tried "../res/default.vert" to no avail.
Any help is appreciated. Cheers!
The answer was in fact related to the working directory. There's a custom setting under the schemes that lets you set a custom working dir. Obviously not really suitable for distribution, but that explains the discrepancy. Thanks for the help!
I am trying to open a file in C++ but it seems to be giving me a bit of hassle, here is the code that deals with opening the file so far:
void CreateHistogram(string str_file, vector<HistogramWord> &result) {
string line;
long location;
HistogramWord newWord;
const char * filename = str_file.c_str();
//ifstream myfile (str_file.c_str());
ifstream myfile (filename);
//myfile.open(filename);
if (myfile.is_open()) {
while (myfile.good()) {
getline(myfile, line);
line = clarifyWord(line);
Okay, just for a bit of explanation, HistogramWord is a struct that is defined in the header and from what I have read in the online documentation, the filename has to be of type const char *, so that is what I have done. Converted str_file to be a const char *.
Now, I have tried a few different things which is why some of the code is commented out. When it gets to the line if (myfile.is_open()), it always evaluates to false. Anyone seem to know why?
Thanks,
Brandon
OK IO 101
If you don't give the complete filepath but only the filename then the current working directory will be appended to the filename.
So if your .exe is in C:\temp and you call your program from this directory and your filename is test.txt then the complete filename in this case will be C:\temp\test.txt
This will only work if the .exe and the test.txt are both under C:\temp.
In all other cases it will fail. You could create the absolute path by using win API or the linux equivalent - I don't know what platform you are on.
Now in order to read a succsfully opened file this will suffice :
void CreateHistogram(string str_file, vector<HistogramWord> &result) {
string line;
long location;
HistogramWord newWord;
ifstream myfile (str_file.c_str());
if (myfile.is_open()) {
while (getline(myfile, line)) {
line = clarifyWord(line);
}
else{
//throw exception, print error message etc
throw std::exception(std::string("Couldn't open file : " + str_file).c_str());
}
}
edit : Thanks # Shahbaz
My best guess is that Windows is "hiding extensions for known file types" so the name of the file is actually different than what you have put in windows. For example if it's a .txt file, and you name it test.txt, the actual name would be test.txt.txt which is quite a stupid thing windows does.
To change this, go to My Computer -> Toold -> Folder Options -> And uncheck the box that says "Hide extensions for Known File Types". This is for XP. If you have another windows it should be more or less the same path. If you don't see the toolbar, try ALT+t (tools) or ALT+f (file) to make it appear.
This problem give quite many of us a trouble in the first semester of college.
What fixed it for me was using forward slashes instead of double backslashes in my filepath.
e.g.
inFile.open("path/to/file.txt")
instead of
inFile.open("path\\to\\file.txt")