Can't open text file, no matter what

Hello. I am currently trying to open a text file and being unable to do so until now. I already checked:

1)The File name. I did a copy and paste from the file's name displayed by the window manager to ensure that I did not a typing error. I also printed it on the screen and seems ok.
2)I created a new text file, b.txt and tried to open, also without success.
3)Checked if the file to be open is in the same dir as the source code. It is.
4)The file in question is indeed a text file.

So I believe I am doing a very basic error. Bellow relevant code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//function call that pass the name of files to be opened. 
loadShaders ("SimpleVertexShader.vertexshader","SimpleFragmentShader.fragmentshader");

//method definition, first line:
GLuint OpenGL::loadShaders (const char* vertex_file_path, const char* fragment_file_path){

//file
ifstream file_reader (vertex_file_path);
    cout << vertex_file_path << endl;
    if (file_reader.is_open()){
       cout << "opened" << endl;
       std::string Line = "";
       while (getline(file_reader, Line))
	  vs_instructions += "\n" + Line;
	  file_reader.close();
       }
    else {
	printf("Impossible to open %s. Are you in the right directory ? Don't forget to read the FAQ !\n", vertex_file_path);
	getchar();
	return 0;
    }


Remembering that if I try ifstream file_reader ("b.txt");
it also does not work

Any guesses?
3)Checked if the file to be open is in the same dir as the source code. It is.

More importantly, is it in the same directory as the current working directory? The source code directory might not be the same directory that your program is looking in.

If you can use C++17, maybe see what https://en.cppreference.com/w/cpp/filesystem/current_path#Example prints for your program.

-Albatross
visual studio specifically puts the executable in a sub-folder (\debug or \release usually) and can't see files in the same folder as your main.cpp file.
you can change the target to move it up a folder, or whatever else.
other compilers do other things. This is kind of 'hidden' because you run it from the IDE, without really seeing the folder structure.

try running the executable from the command line and checking that the file is there with it using a command line file list.
Last edited on
Hello colt,

I have bee looking at your code for awhile and have some questions.

In the line of code:
loadShaders ("SimpleVertexShader.vertexshader","SimpleFragmentShader.fragmentshader");
What do these variables contain?

When you get to the line: ifstream file_reader (vertex_file_path);
What does the variable contain?

The fact that the variable ends with the name "path" leads me to believe that the variable is just a path and missing the file name.

Trace the variables to see what they contain and you may find that this is the root of your problem.

Hope that helps,

Andy
Hello colt,

When I tried to make the program work I found one problem that I did not notice at first:

Your function call
loadShaders ("SimpleVertexShader.vertexshader","SimpleFragmentShader.fragmentshader"); As you can see each parameter is a quoted string and not the variable that you want. So when you get to the line
ifstream file_reader (vertex_file_path);
it actually looks like
ifstream file_reader ("SimpleVertexShader.vertexshader"); and that is not a file name or a path.

With out seeing how the class is defined I can only guess at what you could do. I can say the the function call needs to call a "get" function to return the pointer to the variable "vertexshader" of the class or you just need to send the function a quoted string containing the file name.

Other things I noticed in the program:

You are mixing C++ and C code. This may work, but maybe not for everyone as compiles and header files are different. And what works for you might not work for some one else.

The else statement can easily be changed to:
1
2
3
4
5
6
7
8
9
	else
	{
		std::cout<< "Impossible to open \""<< vertex_file_path<<"\". Are you in the right directory ? Don't forget to read the FAQ !\n";

		std::cout << "\n\n\n Press Enter to continue: ";
		std::cin.get();

		exit(1); // <--- "exit" leaves the program. "return" returns to where the function was called from.
	}

Note the use of exit(1); here. Any number greater than zero means that there is a problem causing the exit. Using different numbers greater than zero here can help track down where a problem has occurred.

Also the above code is a way to replace the C code that you have used.

Hope that helps,

Andy
Topic archived. No new replies allowed.