Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. troubles in making a filecopy program in visual c++

troubles in making a filecopy program in visual c++

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
25 Posts 10 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Aamir Butt

    It is CopyFile(...) :) Einstein: "Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe." My Articles

    C Offline
    C Offline
    Christian Graus
    wrote on last edited by
    #21

    Thanks - I thought it was, but I did a google on CopyFile C++ and got no helpful links. Usually, MSDN is the first hit in a case like that. Christian Graus - Microsoft MVP - C++

    1 Reply Last reply
    0
    • J Joe Woodbury

      This code is not tested, I don't have the time, but here's a quick pass with comments. #include #include int main() { // Initialize MFC if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) { _tprintf(_T("Fatal Error: MFC initialization failed\n")); return 1; } // m_ is a C++ convention to indicate a variabe is a data member // of a class. Also by convention, the first letter after the // underscore is lower case. For example, it would be m_logFile. // These are local variables, thus the m_ has been removed. CFile logFile, dataFile; // the char *a has been moved below, closer to where it is actually // being used. // The share mode should probably be deny write to prevent someone // from using while attempting other operations. dataFile.Open("E:/data.txt", CFile::modeRead|CFile::shareDenyWrite); // the try block is not needed since CFile::Open does not throw // an exception. (The CFile constructor throws an exception.) // Instead, we pass CFileException as a parameter and check the BOOL // return. // I also added ShareDenyWrite to prevent any other program from writing // to the file at the same time. CFileException err; if (!logFile.Open("E:/data1.txt", CFile::modeReadWrite|CFile::modeCreate|CFile::modeNoTruncate|CFile::shareDenyWrite, &err)) { // If I remember, this pops up a message box. For console apps // it's generally better to use _tprintf() or _tputs() err.ReportError(); return 1; } logFile.SeekToEnd(); // since we aren't using smart pointers for pBuffer, // this reduces having to add a delete before an // explicit error return. int rval = 0; // Allocate the buffer here. Note I used the convention of a p for // pointer. This is the extent of my general notation rules. int bufferLen = dataFile.GetLength(); char* pBuffer = new char[bufferLen]; if (dataFile.Read(pBuffer, bufferLen) == dataFile.GetLength()) { if (logFile.Write(pBuffer, bufferLen) != bufferLen) { _tprintf("Error writing file\n"); rval = 1; } } delete [] pBuffer; return rval; } Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

      F Offline
      F Offline
      firebolt77
      wrote on last edited by
      #22

      I have tried the code(Mr. Joe Woodbury's code), but when I run it, it generate the same error with my code, although there is no error in the compile process.When I run the program, it generate a typical windows error dialog box(the one that has debug,send error report, and don't send button). I also try to debug it, but when I debug it, it opens other source code(winmain.cpp). I'm so confused. Pls help.. thx.

      J 1 Reply Last reply
      0
      • F firebolt77

        I have tried the code(Mr. Joe Woodbury's code), but when I run it, it generate the same error with my code, although there is no error in the compile process.When I run the program, it generate a typical windows error dialog box(the one that has debug,send error report, and don't send button). I also try to debug it, but when I debug it, it opens other source code(winmain.cpp). I'm so confused. Pls help.. thx.

        J Offline
        J Offline
        Joe Woodbury
        wrote on last edited by
        #23

        Interesting, since it doesn't compile, you couldn't have tried my code. For one, you didn't include the right headers so the call to AfxWinInit would fail when compiling. Second you would need to declare an instance of CWinApp. Third CFile::Write() is a void function. The problem actually likely lies with the failure to check for failure with the dataFile.Open call if e:/data.txt doesn't exist or can't be otherwise opened. I simplified it and compiled the file below. An additional possibility is that your project is not set up correctly. So, before continuing, close the project and create a new one with Visual Studio. Assuming you are using VS 2003, On the File menu, click New and then Project. In the left pane, open Visual C++ Projects and the Win32 folder. In the right pane Select Win32 Console Project and enter a project name in the Name edit control, and then click OK. In the wizard, select Application Settings and then click the MFC checkbox in the right, and then "Finish." When the project opens, select everything in Main between the two outer braces, then paste the code below into it. Compile and run. Here is the code as I compiled in a console project:

        if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
        {
        	\_tprintf(\_T("Fatal Error: MFC initialization failed\\n"));
        	return 1;
        }
        
        CFile logFile, dataFile;
        
        CFileException err;
        
        if (!dataFile.Open("e:/data.txt", CFile::modeRead | CFile::shareDenyWrite, &err))
        {
        	err.ReportError();
        	return 1;
        }
        
        if (!logFile.Open("e:/data1.txt", CFile::modeReadWrite | CFile::modeCreate | CFile::modeNoTruncate | CFile::shareDenyWrite, &err))
        {
        	err.ReportError();
        	return 1;
        }
        logFile.SeekToEnd();
        
        int rval = 0;
        
        int bufferLen = (int) dataFile.GetLength();
        char\* pBuffer = new char\[bufferLen\];
        if (dataFile.Read(pBuffer, bufferLen) == dataFile.GetLength())
        {
        	logFile.Write(pBuffer, bufferLen);
        }
        delete \[\] pBuffer;
        return rval;
        

        Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

        F 1 Reply Last reply
        0
        • D David Crow

          If that was the case, the linker would be complaining about MFC-related code instead of __endthreadex and __beginthreadex. See here for more.


          "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

          B Offline
          B Offline
          Bob Stanneveld
          wrote on last edited by
          #24

          I sometimes used MFC in an application that was not linking to the MFC libraries. The only unresolved external symbols that I had were the __beginthreadex and __endthreadex. I thought that that was the problem, since he is using the CFile class from MFC. There are numerous ways to generate the same error, however. Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

          1 Reply Last reply
          0
          • J Joe Woodbury

            Interesting, since it doesn't compile, you couldn't have tried my code. For one, you didn't include the right headers so the call to AfxWinInit would fail when compiling. Second you would need to declare an instance of CWinApp. Third CFile::Write() is a void function. The problem actually likely lies with the failure to check for failure with the dataFile.Open call if e:/data.txt doesn't exist or can't be otherwise opened. I simplified it and compiled the file below. An additional possibility is that your project is not set up correctly. So, before continuing, close the project and create a new one with Visual Studio. Assuming you are using VS 2003, On the File menu, click New and then Project. In the left pane, open Visual C++ Projects and the Win32 folder. In the right pane Select Win32 Console Project and enter a project name in the Name edit control, and then click OK. In the wizard, select Application Settings and then click the MFC checkbox in the right, and then "Finish." When the project opens, select everything in Main between the two outer braces, then paste the code below into it. Compile and run. Here is the code as I compiled in a console project:

            if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
            {
            	\_tprintf(\_T("Fatal Error: MFC initialization failed\\n"));
            	return 1;
            }
            
            CFile logFile, dataFile;
            
            CFileException err;
            
            if (!dataFile.Open("e:/data.txt", CFile::modeRead | CFile::shareDenyWrite, &err))
            {
            	err.ReportError();
            	return 1;
            }
            
            if (!logFile.Open("e:/data1.txt", CFile::modeReadWrite | CFile::modeCreate | CFile::modeNoTruncate | CFile::shareDenyWrite, &err))
            {
            	err.ReportError();
            	return 1;
            }
            logFile.SeekToEnd();
            
            int rval = 0;
            
            int bufferLen = (int) dataFile.GetLength();
            char\* pBuffer = new char\[bufferLen\];
            if (dataFile.Read(pBuffer, bufferLen) == dataFile.GetLength())
            {
            	logFile.Write(pBuffer, bufferLen);
            }
            delete \[\] pBuffer;
            return rval;
            

            Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

            F Offline
            F Offline
            firebolt77
            wrote on last edited by
            #25

            I will try it. thx...

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups