File Open and write to; and error error C2440
HI I am very new to programming and I was just assigned to do this programming job for work and I am having a lot of trouble with it.
The code here has many different '.cpp' files all calling functions within each other.. One of the functions/ codes has to call to another one 'FileWriter' which should ask the user for a filename to enter, open that file, and write to it. Since this function will be called many times throughout the application, the next time it writes to the file, it should append to it (not overwrite) and in the end save and close the file.
So far this is what I got but it has errors (one which comes up now is '........caltest\filewriter.cpp(72) : error C2440: 'initializing' : cannot convert from 'errno_t' to 'FILE *')
and there probably is a neater way of doint it. Please post your advice and help on the error.
using namespace std;
FILE* stream;r. Here is the code for FileWriter:
void WriteFile(const char* Log_BPCalTest)
char s = "This file contains the test resullts.\n";
char question = "Please enter a name for the Log file containing test results: ";
char filename ;
cout << question;
cin >> filename;
FILE *stream = fopen_s( &stream, filename, "a+" );
if( stream == NULL )
printf ("File could not be opened, enter different name.\n");
fprintf_s( stream, "%s", s );
fprintf(stream,"CalPrimaryTransducer() returned %s",CalPrimaryTransducer());
fprintf(stream,"CalBattery() returned %s",CalBattery());
fclose( stream );
Thank you in advance!!
You need to look at the declaration of fopen_s and see what its return type is. Hint: it's not FILE*.
A better idea: get rid of fopen_s() and use the standard fopen() function instead.
I replaced fopen with fopen_s since Visual studio 2005 has fopen "DEPRACATED".
Anyway, I was looking at some examples, and seemed like I dint really need the "FILE* string = " part. So I replaced
"FILE *stream = fopen_s( &stream, filename, "a+" );"
with just "fopen(filename, "a+");"
and I have a whole bunch of Linking errors now! :
...caltest\filewriter.cpp(74) : warning C4996: 'fopen' was declared deprecated
c:\program files\microsoft visual studio 8\vc\include\stdio.h(234) : see declaration of 'fopen'
Message: 'This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.'
InflationConCheck.obj : error LNK2005: "struct _iobuf * stream" (?stream@@3PAU_iobuf@@A) already defined in FileWriter.obj
UI.obj : error LNK2005: "char * resultString" (?resultString@@3PADA) already defined in LogicInterface.obj
FileWriter.obj : error LNK2019: unresolved external symbol "char * __cdecl CheckSafetyTransducerCal(void)" (?CheckSafetyTransducerCal@@YAPADXZ) referenced in function "void __cdecl WriteFile(char const *)" (?WriteFile@@YAXPBD@Z)
LogicInterface.obj : error LNK2001: unresolved external symbol "char * __cdecl CheckSafetyTransducerCal(void)" (?CheckSafetyTransducerCal@@YAPADXZ)
FileWriter.obj : error LNK2019: unresolved external symbol "char * __cdecl CheckPrimaryTransducerCal(void)" (?CheckPrimaryTransducerCal@@YAPADXZ) referenced in function "void __cdecl WriteFile(char const *)" (?WriteFile@@YAXPBD@Z)
LogicInterface.obj : error LNK2001: unresolved external symbol "char * __cdecl CheckPrimaryTransducerCal(void)" (?CheckPrimaryTransducerCal@@YAPADXZ)
C:\Documents and Settings\GauthamJ\Desktop\BPCalTest\BPCalTest_prog\BPCalTest\Debug\BPCalTest.exe : fatal error LNK1120: 2 unresolved externals
Build log was saved at "file://c:\Documents and Settings\GauthamJ\Desktop\BPCalTest\BPCalTest_prog\BPCalTest\BPCalTest\Debug\BuildLog.htm"
BPCalTest - 7 error(s), 1 warning(s)
Sorry, I read about the return type as you suggested and I think I realize what the mistake was, so I replaced the fopen section with this:
if( (err = fopen_s( &stream, filename, "a+" )) != 0 )
printf( "The file 'data2' was not opened\n" );
printf( "The file 'data2' was opened\n" );
fprintf_s( stream, "%s", s );
Yet, I am getting all those linking errors I posted above.
You need to:
1) get rid of the stafx.h header (which is #included twice, mind you), and disable precompiled headers
2) make sure that FileWriter.cpp
have been compiled successfully, that they contain definitions of their class's functions, and that the linker knows where to find their respective .obj files.
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL