Advertisement Jump to content
Sign in to follow this  
noatom

ifstream not working

This topic is 1880 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

in.open("test.txt", ios::in | ios::binary | ios::ate);

if (in.is_open())
cout << "File opened" << endl;

the test.txt exists and it has text in it,yet,i see no file opened string. What could be the problem?

Share this post


Link to post
Share on other sites
Advertisement

But what about the working directory? If, for example, you run the program from Visual Studio, the working directory is by default the project directory, so that's where you have to put the file.

Share this post


Link to post
Share on other sites
Try something like this, so you get a meaningful error message:
#include <iostream>
#include <fstream>
#include <cerrno>

int main() {
  std::ifstream in;
  in.open("test.txt", std::ios::in | std::ios::binary | std::ios::ate);

  if (in.is_open())
    std::cout << "File opened\n";
  else
    std::cout << std::strerror(errno) << '\n';
}

Share this post


Link to post
Share on other sites
std::string ExePath()
{
	char buffer[_MAX_PATH];
	GetModuleFileName(NULL,buffer, MAX_PATH);
	string::size_type pos = string(buffer).find_last_of("\\/");
	return string(buffer).substr(0, pos);
}


int main(){

try{
		string path = ExePath() + "\\A.txt";
		ifstream          in;
		in.exceptions(std::ios::failbit);
		cout << path.c_str() << endl;
		in.open(path);
	}
	catch (const std::exception & ex) {

		cerr << strerror(errno);
	}

}

output: c:\users\enjoydrama\documents\visual studio 2013\Projects\Raisa\Release\A.txt
No such file or directory

 

I went to the path,and A.txt exists!

Share this post


Link to post
Share on other sites

GetModuleFilename returns the path to the executable. You need the working directory, look up GetCurrentDirectory. The path to the executable and the working directory are not necessarily the same.

 

std::string ExePath()
{
    char buffer[_MAX_PATH];
    GetCurrentDirectoryA(MAX_PATH,buffer);
    string::size_type pos = string(buffer).find_last_of("\\/");
    return string(buffer).substr(0, pos);
}

Getting the path to the executable fails as expected, and getting the working directory works as expected, when I run it from Visual Studio.

Share this post


Link to post
Share on other sites

brotherbob...the release folder had a in it too. Anyway,i tried with getcurdir too,the same happens.

 

 

UPDATE:

 

this works: ifstream binary_file("c:\'a.txt", ios::in | ios::binary | ios::ate);

this doesn't: fstream binary_file("c:\'a.txt", ios::out | ios::binary | ios::app);

 

when i say it doesn't i mean it can't find the file.

Edited by noatom

Share this post


Link to post
Share on other sites

With the program, try creating a dummy file by opening an ofstream, and creating a new empty file that didn't exist before. If it doesn't exist where you thought it did, then that solves one problem. If it doesn't exist at all, then that's another problem. If it puts it exactly where you thought, then carry on with your debugging.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!