Trouble with opening a file.

Hello,

The code snippet below is part of a homework problem I'm doing for class. I've re-worked code and searched the internet for close to an hour hoping to come across some decrypting documentation that might shed some light on my problem.

I expect the file to open, but for some reason it's not opening.

Any help would be greatly appreciated.

(Note of Disclaimer: I'm not expecting anyone to do my homework, I simply would like a fresh pair of eyes to look at my code.)

Much obliged.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  std::string sExecutable = EXEFILEPATH;
	std::string sReadingExecutable;
	std::ifstream input3(sExecutable, std::ios::in | std::ios::binary);
	std::vector<char> cExe;
	char cBinary;
	input3.open(EXEFILEPATH);
	if (input3.is_open()){
		std::cout << "Yay! It's open!\n";
		/*while (input3 >> cBinary){
			cExe.push_back(cBinary);
			std::cout << cBinary;
		}*/
	}
	else std::cout << "Somethin' went wrong, boy!\n";
	input3.close();
1
2
std::ifstream input3(sExecutable, std::ios::in | std::ios::binary); //Opens file
input3.open(EXEFILEPATH); //And opens it AGAIN 

http://en.cppreference.com/w/cpp/io/basic_filebuf/open
If the associated file was already open, returns a null pointer right away.
Return value
this on success, a null pointer on failure.

http://en.cppreference.com/w/cpp/io/basic_filebuf/is_open
Returns true if the most recent call to open() succeeded and there has been no call to close() since then.


So your second open() breaks your code.
Topic archived. No new replies allowed.