Results 1 to 27 of 27

Thread: Why it no work to copy file?

  1. #1

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935

    Why it no work to copy file?

    Code:
    bool copyFile(const apstring &source, const apstring &dest)
    // Copies source to dest and return true on success.
    {
        long flag = CopyFile(source.c_str(), dest.c_str(), FALSE);
        return (flag == ERROR_SUCCESS);
    }
    If I pass it a source that doesn't exist it still returns true.

  2. #2
    Megatron
    Guest
    Make sure that the c_str() function for your apstring class is returning a valid string.

  3. #3
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    Why not first use "access" to see if the file exist and then copy one file into another one?
    Baaaaaaaaah

  4. #4

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935
    I'm pretty sure it does; it returns an array of chars. If you want to try it out, http://www.collegeboard.org/ap/compu...l/classes.html (just before the #endif, type #include "apstring.cpp" in the apstring.h file).

  5. #5

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935
    Originally posted by abdul
    Why not first use "access" to see if the file exist and then copy one file into another one?
    Considering that I'm completely ignorant to C++ outside what they taught in school , got an example?

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    ERROR_SUCCESS isn't used by CopyFile - did you read the quote I posted from MSDN (which you should've looked at before asking, but oh well...)
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935
    I ignorantly assumed that ERROR_SUCCESS was 0.

  8. #8
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    PHP Code:
    if (access(filename,0) == 0)
    {
    //The file exists

    Baaaaaaaaah

  9. #9
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    So it might be, but still:
    If the function succeeds, the return value is nonzero.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  10. #10

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935
    This looks better:
    Code:
    bool copyFile(const apstring &source, const apstring &dest)
    // Copies source to dest and return true on success.
    {
        long flag = CopyFile(source.c_str(), dest.c_str(), FALSE);
        return (flag != 0);
    }

  11. #11
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    A little shorter:
    Code:
    bool copyFile(const apstring &source, const apstring &dest) {
        return CopyFile(source.c_str(), dest.c_str(), FALSE);
    }
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  12. #12
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    PS: for speed, you can make it an inline function as well
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  13. #13

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935
    I haven't done inlines; is it like this?

    Code:
    inline bool copyFile(const apstring &source, const apstring &dest) { return (CopyFile(source.c_str(), dest.c_str(), FALSE) != 0); }

  14. #14
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    It doesn't have to be one line, and you don't need the != 0, because the return value will be implicitly converted to a bool (0 = false, anything else = true). Basically, just stick an inline before my one.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  15. #15

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935
    *homer's voice* DAMMIT! It no work.

    Code:
    // Visual Basic Installer Stub
    // Designed for Visual Basic 5.0 and 6.0
    // Copyright (c) 2001 Arien Talabac
    
    // ap*.h classes are from the College Board; download them at
    // http://www.collegeboard.org/ap/compu...l/classes.html
    
    #include <iostream.h>
    #include <fstream.h>
    #include <windows.h>
    #include <stdio.h>
    #include <ctype.h>
    #include "apstring.h"
    #include "apvector.h"
    
    #define _PROJECT_VERSION_ "1.0.0"
    #define _SCRIPT_FILE_ "script.iss"
    #define _WAIT_FOR_KEY_PRESS_TERMINATE_ true
    #define _COPYRIGHT_NOTICE_ "Copyright (c) 2001 Arien Talabac"
    
    // Uncomment below if bool is not a keyword in your compiler:
    /*
    #define bool int
    #define true 1
    #define false 0
    */
    
    apstring windowsSystemDirectory()
    // Returns a path to the Windows system directory or "" on error.
    {
        UINT i;
        char path[MAX_PATH];
        UINT sz = MAX_PATH;
        i = GetWindowsDirectory(path, sz);
        return apstring(path);
    }
    
    bool copyFile(const apstring &source, const apstring &dest)
    // Copies source to dest and return true on success.
    {
        long flag = CopyFile(source.c_str(), dest.c_str(), FALSE);
        return (flag != 0);
    }
    
    apstring lowercase(apstring s)
    // Returns a lowercase version of s
    {
        for (int i = 0; i < s.length(); i++)
        {
            s[i] = tolower(s[i]);
        }
        return s;
    }
    
    bool executeScriptCommand(const apstring &s)
    // Executes s and returns true on success.
    {
        if (s.find("copytowinsysdir") == 0)
        {
            int spaceLoc = s.find(' ');
            if (spaceLoc != npos)
            {
                apstring source = s.substr(spaceLoc + 1, s.length() - spaceLoc);
                apstring dest(windowsSystemDirectory() + "\\");
                cout << "Copying \"" << source << "\" to \"" << dest << "\"...";
                if (copyFile(source, dest + "\\"))
                {
                    cout << "Done" << endl;
                    return true;
                }
                else
                {
                    cout << "Failed" << endl;
                    return false;
                }
            }
            else
            {
                cout << "Bad syntax in \"" << s << "\"" << endl;
                return false;
            }
        }
        else if (s.find("copytowinsysdirandregister") == 0)
        {
        }
        else if (s.find("shellprocess") == 0)
        {
        }
        else
        {
            cout << "Unrecognized directive \"" << s << "\"" << endl;
            return false;
        }
        return true;
    }
    
    int main()
    {
    	cout << "Visual Basic Installer Stub" << endl;
        cout << "Version " << _PROJECT_VERSION_ << endl;
        cout << _COPYRIGHT_NOTICE_ << endl;
        cout << endl;
        cout << "Searching for stub script \"" << _SCRIPT_FILE_ << "\"...";
        ifstream in(_SCRIPT_FILE_, ios::nocreate);
        if (in.fail())
        {
            cout << "Failed" << endl;
            return 1;
        }
        cout << "Found" << endl;
        cout << "Loading script commands...";
        apstring currentLine("");
        apvector<apstring> lines;
        while (!in.eof())
        {
            getline(in, currentLine);
            lines.resize(lines.length() + 1);
            lines[lines.length() - 1] = currentLine;
        }
        in.close();
        cout << "Loaded " << lines.length() << " directive(s)" << endl;
        cout << "Executing directives..." << endl;
        long errorCount = 0;
        for (long i = 0; i < lines.length(); i++)
        {
            if (!executeScriptCommand(lines[i]))
            {
                errorCount++;
            }
        }
        cout << "Done executing " << lines.length() << " directive(s) with "
                << errorCount << " error(s)" << endl;
        if (_WAIT_FOR_KEY_PRESS_TERMINATE_)
        {
            getchar();
        }
        return 0;
    }

  16. #16
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Well...considering you just asked a load of questions and then ignored the answers...
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  17. #17

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935
    Originally posted by parksie
    Well...considering you just asked a load of questions and then ignored the answers...
    What answer did I ignore? I fixed the nonzero success return value thing (I like doing explicit casts )...

  18. #18
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Well, the return CopyFile(...); line is smaller, and allows inlining, resulting in smaller, faster code.

    Also, the FALSE parameter changes whether the function overwrites files that are already there, or just fails.

    Is the target file there? Are you trying to overwrite it? Are you letting it overwrite it? All this was given in MSDN.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  19. #19

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935
    It's not there, and I don't mind if it gets overwritten. At the moment I'm not concerned with execution speed (just look at all of those apstring declarations ).

  20. #20
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Perhaps you need to give a full path name? GetCurrentDirectory should help here
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  21. #21

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935
    Based on the MSDN info, does that mean I need a full path and name for both the source and the destination?

  22. #22
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Well, make a small test program that does CopyFile("script.iss", "script.iss.bak", FALSE) and see if it works or not.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  23. #23
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Originally posted by parksie
    Well, the return CopyFile(...); line is smaller, and allows inlining, resulting in smaller, faster code.
    Faster...yes
    Smaller...NO
    inlining means that each occurence of the function call is replaced by the function body - like a macro. This surely does NOT make smaller code...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  24. #24
    jim mcnamara
    Guest
    CB is right - that is exactly what inlining does - puts the function code inside the calling module. It's faster because you reduce stack overhead. It will be larger if you call the inline module from more than one function.

  25. #25
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Exactly, but he only calls it once...and since he'd call copyFile ANYWAY, there's no real difference in function sizes.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  26. #26
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    but then, if he calls it only once, the function overhead can be ignored too. Inlining is better here, but only a little bit.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  27. #27
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Originally posted by CornedBee
    but then, if he calls it only once, the function overhead can be ignored too. Inlining is better here, but only a little bit.
    Good enough for me

    Okay I'll shut up and stop being pedantic and talking about clock cycles now...
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width