File Open and write to; and error error C2440


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: File Open and write to; and error error C2440

  1. #1
    Join Date
    Aug 2008
    Posts
    3

    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.

    #include "stdafx.h"
    #include <stdio.h>
    #include <process.h>
    #include "stdlib.h"
    #include <iostream>
    #include <fstream>
    #include "stdafx.h"
    #include "FileWriter.h"
    #include "CalDevice.h"
    #include "LogicInterface.h"
    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 [80];
    cout << question;
    cin >> filename;
    FILE *stream = fopen_s( &stream, filename, "a+" );


    if( stream == NULL )
    {
    printf ("File could not be opened, enter different name.\n");
    }
    else
    {
    fprintf_s( stream, "%s", s );
    fprintf(stream,"CalPrimaryTransducer() returned %s",CalPrimaryTransducer());
    fprintf(stream,"CalBattery() returned %s",CalBattery());
    fclose( stream );
    }
    }


    Thank you in advance!!

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    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.
    Danny Kalev

  3. #3
    Join Date
    Aug 2008
    Posts
    3
    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! :
    Compiling...
    FileWriter.cpp
    ...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.'
    Linking...
    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)

  4. #4
    Join Date
    Aug 2008
    Posts
    3
    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" );
    else
    printf( "The file 'data2' was opened\n" );


    fprintf_s( stream, "%s", s );


    Yet, I am getting all those linking errors I posted above.
    Please Help!!

  5. #5
    Join Date
    Nov 2003
    Posts
    4,118
    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
    CalDevice.cpp
    and LogicInterface.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.
    Danny Kalev

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center