Solved

Using CFile with binary files

Posted on 1998-11-08
11
1,039 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
11 Comments
 
LVL 1

Expert Comment

by:fkurucz
Comment Utility
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
Comment Utility
This advice does not work (fkurucz), using either the multithreaded option or the single threaded option.


0
 
LVL 3

Expert Comment

by:plaroche
Comment Utility
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
 

Author Comment

by:mmacrobert
Comment Utility
No answer
0
 
LVL 2

Expert Comment

by:prasanth
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:mmacrobert
Comment Utility
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
Comment Utility
Have you tried getting rid of the /MT?
0
 
LVL 11

Expert Comment

by:mikeblas
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Remove the libc and libcd from the linking proccess by ignoring them in the linker tab of the project settings.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Need Help INsttalling wget on Mavericks OS X 3 81
Detect CR LF to each line 12 135
WinWaitActive parameters 12 31
fizzArray2 challenge 1 55
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…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now