# How to determine if app uses MFC?

## Recommended Posts

Hi. So I'm writing an "ultimate" ;) wrapper for files. So there will be CFile for VC++ and FILE* for linux and others. I can make some predefines so the compiler knew what OS it is compiling my headers for. But I cannot be using CFile without using MFC, which I tried and failed to manually include for win-compiled project. So I could use CFile only for win apps, which explicite declared using MFC. It could look somewhat like that:
#if (defined USING_MSVCPP) && (defined "we declared using MFC" )

typedef CFile CMyFile;

#else

#include <stdio.h>
class CMyFile
{
protected:
FILE *m_f;
public:
// wrap FILE* operations into CFile-like methods.
};

#endif


I'm searching formula for "we declared using MFC". Any ideas? /def

##### Share on other sites
You'd be better off to completely building CFile yourself than relying on MFC as you can use FILE* the same as in linux. Or put CFile inside a namespace.

I'm not sure about the define, you might check for __AFX_H__ as that gets set when including stdafx.h.

##### Share on other sites
Since you can use FILE functions on both Win32 and Linux, why not dispense with the conditional preprocessor commands?

##### Share on other sites
I knew I should have told that earlier...

I sustained serious injuries... ekhmmm.. I mean I had some problems with FILE* under win. That absolutely was not happening when using CFile. The symptoms:
FILE* f = fopen("data.smth", "some_options");int len = 100200;char* buf = allooooc_and_fill(len);int lewWr = fwrite(f, buf, len);// ...

And so lenWr was stopping after something like 700 bytes or so.
I really don't know what was happening, and stopped trying to find out after a few days.

Anyway, I switched to CFile and everything was ok.
So that's the deal. I you can cure that, then, by all means, do it.
/def

##### Share on other sites
That's weird, as in Windows the fopen/fwrite functions and CFile function both literally call the same functions beneath: CreateFile/WriteFile.

##### Share on other sites
It only happens whenI try to write a LARGE portion of data.
Or when I try to write SOME chunks of SMALL portions of data ;)

##### Share on other sites
it's because fwrite may not write everything, check the return value and retry in a loop

##### Share on other sites
Unfortunately, I tried and was getting zero all the time...

##### Share on other sites
And what error code did you get after fwrite failed?
Use GetLastError and look the error code up in the Error Lookup Tool.

##### Share on other sites
fwrite's syntax is:
size_t fwrite( const void *buffer, size_t size, size_t count, FILE *stream );

So you should write

int lenWr = fwrite(buf, sizeof(char), len, f);

int lenWr = fwrite(f, buf, len);

##### Share on other sites
@Endurion:
fwrite() sets errno on failure. I'm afraid GetLastError() is not supposed to give you any information which is relative to a failure of fwrite.

Funny thing: in MS fwrite.c - the one which is shipped with VS7, lines 75 and 76:
  /* do the read */  retval = _fwrite_lk(buffer, size, count, stream);

Regards,

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627676
• Total Posts
2978582

• 11
• 12
• 10
• 12
• 22