Solved

Using CFile with binary files

Posted on 1998-11-08
11
1,060 Views
Last Modified: 2013-11-20
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
0
Comment
Question by:mmacrobert
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 1

Expert Comment

by:fkurucz
ID: 1324373
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
0
 

Author Comment

by:mmacrobert
ID: 1324374
This advice does not work (fkurucz), using either the multithreaded option or the single threaded option.


0
 
LVL 3

Expert Comment

by:plaroche
ID: 1324375
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).

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mmacrobert
ID: 1324376
No answer
0
 
LVL 2

Expert Comment

by:prasanth
ID: 1324377
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.
0
 

Author Comment

by:mmacrobert
ID: 1324378
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"

0
 
LVL 2

Expert Comment

by:prasanth
ID: 1324379
Have you tried getting rid of the /MT?
0
 
LVL 11

Expert Comment

by:mikeblas
ID: 1324380
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
0
 
LVL 2

Expert Comment

by:wyy_cq
ID: 1324381
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.

0
 

Author Comment

by:mmacrobert
ID: 1324382
It is obviously a link error. The file is already set to compile using the MFC static library.
0
 
LVL 3

Accepted Solution

by:
shaig earned 100 total points
ID: 1324383
Remove the libc and libcd from the linking proccess by ignoring them in the linker tab of the project settings.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question