Sign in to follow this  
c0uchm0nster

Trouble opening a file (ifstream/wfopen)

Recommended Posts

c0uchm0nster    182
Hi, I'm trying to open a file in a unicode program. I've tried many things, such as assigning wfopen to an ifstream, using _wfopen_s to assign to a FILE and then pointing the ifstream at that FILE, using the .open function of an ifstream, etc etc... They always report 0 errors, but they always have a NULL pointer... .is_open() reports that the file is open as well... Any help would be greatly appreciated.

Share this post


Link to post
Share on other sites
KulSeran    3267
have you tried a wifstream?
it looks like it solves what you are trying to do.
It works like ifstream, but opens in unicode.

You should be able to

wifstream myfile;
myfile.open ( ... );

and have it work just fine.

(never played with it myself, but MSDN pulled this up after a quick search.
It should point you in a direction that might work.)

Share this post


Link to post
Share on other sites
deathwearer    367
Alright, i sugest you to use fstream. It's a somewhat easy library in my opinion.


To open a file you need first an object type ifstream.

ifstream m_oFichierGeneration;


#include ".\lecture.h"

CLecture::CLecture(void)
{
m_bPeutLire = false;
}

CLecture::~CLecture(void)
{
}

//Open the file
void CLecture::Ouvrir_Fichier(string sNomFichier) // the string is the name of the file being pass as parameter
{
m_oFichierGeneration.open(sNomFichier.c_str(), ios::in );
if (!m_oFichierGeneration.fail()) //if it doens't fail to open
{
SetPeutLire(true); // Set m_bPeutLire = true
}
else
{
m_oFichierGeneration.clear(); // Dont forget to clear if it's fail
SetPeutLire(false);
}
}

bool CLecture::PeutEncoreLire()
{
return !m_oFichierGeneration.eof(); // If not at end of file
}

int CLecture::Lire_Fichier(int iLecture)
{
m_oFichierGeneration >> iLecture; //Read an int

return iLecture;
}

char CLecture::Lire_Fichier(char cEtat)
{
m_oFichierGeneration >> cEtat; //Read a char

return cEtat;
}

void CLecture::SetPeutLire(bool bPeutLire)
{
m_bPeutLire = bPeutLire;
}

bool CLecture::GetPeutLire()
{
return m_bPeutLire;
}



this is my class that i use whenever i need to read a file and it's working pretty well. If you have any question feel free to ask.

Share this post


Link to post
Share on other sites
c0uchm0nster    182
kulseran - thanks, the files themselves aren't actually stored in unicode - they're normal ascii. I was just mentioning that my program was unicode because that seems to affect how you use every single other function under the sun ;)

deathwearer - that works great. I was actually pretty close but I guess I got confused since, even after the file loads correctly, the ifstream still points to null in the ms vc++ 2k5 debugger.

thanks again

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this