views:

107

answers:

5

Hi all! I'm new here and my english is not really good. Apologize any inconvenience!

I'm programming an application for windows mobile with native code (MFC). I'm trying to open a file and this is driving me crazy. I've tried to open it in a thousand diferent ways... And I really achieve it, but when I try to read (fread or getline) the program crashes without any explanation:

The program 'x' finalize with code 0 (0x0)

The GetLastError() method, in some cases, returns me a 183.

Then, I put the code I've used to open the file:

std::wifstream file(L"\\Archivos de programa\\Prog\\properties.ini");  
wchar_t lol[100];
if (file) {  
    if(!file.eof()) {       
        file.getline(lol,99);
    }  
}

It enters on all the if's, but the getline crashes.


FILE * lol = NULL;  
lol = _wfope n(ruta, L"rb");  
DWORD a = GetLastError();  
if ( lol != NULL )  
    return 1;  
else  
    return -1;

It returns 1 (correct), and after, in a later getline, it stores trash on the string. However, it doesn't crash!!


fp.open (ruta, ifstream::in);  
if ( fp.is_open() ) {      
    return 1;  
}else{     
    return -1;  
}

It enters on the return 1, but when executing the later getline() crashes.

I've debugged the getline() method and it crashes on the library fstream, right there:

if ((_Meta = fget c (_File)) == EOF)  
    return (false);

In the if. The fgetc(), I supose.

I'm going completely crazy!! I need some clue, please!!
The path of the file is correct. First, because, in theory, the methods open the file, and second, I obtain the path dinamically and it matches.

Emphasize that the fread method also crashes.

Thanks in advance!

P.S.:
Say that when I do any fopen, the method fp.good() returns me FALSE, and the GetLastError returns me 183. By the other hand, if I use fp.fopen(path, ifstream::in); or std::wifstream fp(path); the fp.good(); returns me TRUE, and the GetLastError() doesn't throw any error (0).

A: 

A hint: use the Process Monitor tool to see what goes wrong in the file system calls.

The path accepted by wifstream is lacking a drive ("C:" or the like) (I don't know what the ruta variable points to)

Apart from the streams problem itself, you can save yourself a lot of trouble by using the GetProfileString and related functions, when using a windows .ini file.

xtofl
The thing is that my application is programed for Windows Mobile. In windows mobiles there is no drives, and there is no idea of current directory. The other point, is that i can't use GetProfileString because it is not supported. I'm programming for Pocket PC 2003 SE, to make my application compatible with all windows mobiles. I can't use Process Monitor, because I don't think that it runs for windows mobile applications, isn't it? `ruta` is a synonim of path. And in it is stored the path in where the .ini file is. Thank you, xtofl. And thanks to gf, for editing my post.
Newlog
There is no Process Monitor for Windows Mobile... or am I wrong? Could you please post a link if there is; I always wanted such a tool (though not hard enough to write one myself ;) )
atzz
A: 

I'm shooting in the dark here, but your description sounds like a runtime mismatch story. Check that MFC and your project use the same runtime link model (static/dynamic). If you link to MFC dynamically, then the restriction is stricter: both MFC and your project have to use dynamic runtime.

atzz
A: 

I don't know why, but with the CFile class... it works...

Programming mysteries!

Newlog
My guess is that your text file is encoded using UTF-8 but the functions which crashed are of Unicode UTF-16 variants. And CFile probably automagically convert them.. did u open the file in text mode/binary mode?
afriza
A: 

Shooting in the dark too. Unexplained random crash in MFC often comes from a mismatch message handler prototype. For example the following code is wrong but it won't generate any warning during compilation and it may work most of the time :

ON_MESSAGE(WM_LBUTTONDOWN, onClick)
...
void onClick(void)  //wrong prototype given the macro used (ON_MESSAGE)
{

//do some stuff 

}

Here the prototype should be :

LRESULT onClick(WPARAM, LPARAM)
{
}

It often happens when people get confident enough to start modifying manually the message maps.

FenchKiss Dev
A: 

lol, I dont think that it was my problem, but the thing is that I commit this mistake!

Thanks for your (late) answer!

Newlog