Link to home
Start Free TrialLog in
Avatar of mmacrobert
mmacrobert

asked on

Using CFile with binary files

I'm trying to learn to use  the MFC CFile object to open binary files and I get the following error message on linking:
 Compiling...
 temp.cpp
 Linking...
 nafxcw.lib(afxmem.obj) : error LNK2005: "void __cdecl operator
 delete(void *)" (??3@YAXPAX@Z) already defined in libcp.lib(delop.obj)

 nafxcw.lib(thrdcore.obj) : error LNK2001: unresolved external symbol
 __endthreadex
 nafxcw.lib(thrdcore.obj) : error LNK2001: unresolved external symbol
 __beginthreadex
 Release/temp.exe : fatal error LNK1120: 2 unresolved externals
 Error executing link.exe.

I am at a complete loss as to how to fix it.

Here is the source code :

#include <iostream>
using namespace std;
#include <stdio.h>
#include <afx.h> // for CString, CFile

class DataFileHeader
{
public:
    int     m_nNoChannels;
    double  m_dUpdateRate;
    double  m_dMinPower;
    double  m_dMaxPower;
    int     m_nNoDataPts;
    int     m_nTempLoopedFlag;
};

int main()
{

DataFileHeader inputbuffer;

CString writebuffer;
char tempbuffer[50];
CString outputbuffer;
CString sInFilename = "red-11.dat"; // the binary input file
CString sOutFilename = "Output.txt";

CFile testin(sInFilename,CFile::modeRead|CFile::typeBinary);
CFile testout(sOutFilename,CFile::modeWrite|CFile::typeText);

UINT nBytesRead = testin.Read(&inputbuffer,sizeof( DataFileHeader ));

cout << "Number of Bytes Read " << nBytesRead << endl;

sprintf(tempbuffer, "Number of Channels -
%d\n",inputbuffer.m_nNoChannels);
outputbuffer = tempbuffer;

.... and so on ...

sprintf(tempbuffer,"Temperature Looped Flag -
%d\n",inputbuffer.m_nTempLoopedFlag);
outputbuffer += tempbuffer;

writebuffer = outputbuffer;

cout << (LPCTSTR)writebuffer;

testout.Write((LPCTSTR)writebuffer,sizeof(outputbuffer)/sizeof(outputbuffer[0]));

testout.Close();
testin.Close();

return 0;
}

There doesn't appear to be anything syntactically wrong with the code, but I don't know what to do about the Linker error.

Any help would be greatly appreciated.

Sincerely, Martin MacRobert
Avatar of fkurucz
fkurucz

Hi mmacrobert,

some parts of your code seem to use multithreading (the 2 missing functions).
So you have to use the multithreaded runtime.
look at the "Project-Settings" under the "C/C++"-Tab
at category "Code Generation".
In the Field "Use run-time library" you must select one of
the Multithread runtimes.
The compiler-directives must not contain the commands /ML or /MLd
(/MT, /MTd for statical linkage /MD, MDd for DLL)

Remember to change both settings (Releas and Debug)

Another posibillity to fix your problem may be to find out, which part of your code uses begin/endthread indirectly.
Perhaps some unneccessary library needs thees functions.

OK, hope it helps,
  feri
Avatar of mmacrobert

ASKER

This advice does not work (fkurucz), using either the multithreaded option or the single threaded option.


Try this, from article Q148652: (I suggest you go check this article).  It applies to the debug files but remove the "d" and try to fix it for your files. Ie change NAFXCWD.LIB in the text to NAFXCW.LIB.

Solution One - Force Linker to Link Libraries in Correct Order

Open the Project Settings dialog box by clicking Settings on the Build menu.

in the Settings For view, select (highlight) the project configuration that's getting the link errors.

Click the Link tab.

Select INPUT in the Category combo box.

In the Libraries to Ignore edit box, insert the library names (for example, Nafxcwd.lib Libcmtd.lib)
NOTE: The linker command line equivalent in /NOD:<library name>


In the Object/library Modules edit box, insert the library names. You must ensure that these are listed in order and as the first two libraries in the line (for example, Nafxcwd.lib Libcmtd.lib).

No answer
How are you compiling this? With a AppWizard generated makefile? a makefile you wrote? just calling cl on the source? The problem is not with your source code. It's a linking problem.
The program is being compiled an linked  from inside the DevStudio environment. I have tried the suggestions above, without success. I am not controlling cl directly, rather through the Project>Settings dialog.  The options used for cl are listed below:

/nologo /MT /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /Fp"Release/translate.pch" /YX /Fo"Release/" /Fd"Release/" /FD /c

In the link section the options set are:

/nologo /subsystem:console /incremental:no /pdb:"Release/translate.pdb" /machine:I386 /out:"Release/translate.exe"

Have you tried getting rid of the /MT?
You _need_ the /MT to link with MFC.  /MT or /MTd.

The easiset way to make sure you've got things set right is to alter the "Microsoft Foundation Classes" dropdown in the "General" settings in the project settings dialog. Make sure you change the setting both for your debug build and your retail build; they're independent.

You want either "use MFC in a shared DLL" or "use MFC from a static library".

B ekiM
Link error . =)
your source code is very correct.
but you need set the correct link prarmeter.
you set "use MFC in static Library" in the
" project/setting " dialog.

It is obviously a link error. The file is already set to compile using the MFC static library.
ASKER CERTIFIED SOLUTION
Avatar of shaig
shaig
Flag of Afghanistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial