Solved

Using CFile with binary files

Posted on 1998-11-08
11
1,049 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
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
deburging in oracle form 12 99
Expand to include initial dialog with two choices. 9 78
unable to delete all specified values regedit 38 408
Excel file not created as expected 7 72
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seveā€¦
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

770 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