Need Help

This is a discussion on Need Help within the C++ Programming forums, part of the General Programming Boards category; <code> HANDLE hFile; // variables are listed at the begining // there is NO declaration like "var" HANDLE hTempFile; DWORD ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    26

    Need Help

    <code>
    HANDLE hFile; // variables are listed at the begining
    // there is NO declaration like "var"

    HANDLE hTempFile;

    DWORD dwBytesRead, dwBytesWritten;

    char szTempName[MAX_PATH];
    // szTempName would be an Array[0..MAX_PATH] of Char
    char buffer[4096];

    // Open the file.

    hFile = CreateFile("ORIGINAL.TXT", // filename
    // double quotes are use like single quote for Text
    GENERIC_READ, // open for reading
    0, // do not share
    NULL, // no security
    OPEN_EXISTING, // existing file only
    FILE_ATTRIBUTE_NORMAL, // normal file

    NULL); // no attr. template
    if (hFile == INVALID_HANDLE_VALUE) {
    ErrorHandler("Could not open file."); // process error
    }

    // Create a temporary file.

    GetTempFileName("\\TEMP", // dir. for temp. files
    "NEW", // temp. filename prefix
    0, // create unique name
    szTempName); // buffer for name

    hTempFile = CreateFile((LPTSTR) szTempName, // filename
    // the (LPTSTR) szTempName is roughly like Typecasting in Pascal
    // PChar(szTempName);
    GENERIC_READ | GENERIC_WRITE, // open for read-write

    0, // do not share
    NULL, // no security
    CREATE_ALWAYS, // overwrite existing file
    FILE_ATTRIBUTE_NORMAL, // normal file
    NULL); // no attr. template

    if (hTempFile == INVALID_HANDLE_VALUE) {
    ErrorHandler("Could not create temporary file.");
    }

    do
    {
    if (ReadFile(hFile, buffer, 4096,
    &dwBytesRead, NULL)) {
    // the & above is roughly like the @ in Pascal
    CharUpperBuff(buffer, dwBytesRead);

    WriteFile(hTempFile, buffer, dwBytesRead,
    &dwBytesWritten, NULL);
    }
    } while (dwBytesRead == 4096);

    CloseHandle(hFile);
    CloseHandle(hTempFile);

    // Move the temporary file to the new text file.

    if (!MoveFile(szTempName, "ALLCAPS.TXT")) {
    ErrorHandler("Could not move temp. file.");
    }

    </code>
    why doesnt this work?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >why doesnt this work?
    Because you don't include any headers and you can't have processing code at the global level. I would recommend including at least <windows.h> and wrapping all of your code in main like this:
    Code:
    #include <windows.h>
    
    int main()
    {
      // Your code here
    }
    My best code is written with the delete key.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    </code>
    why doesnt this work?

    Because you used <> instead of [ ]
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    26
    excuse me, i was in a hurry and i posted the wrong code :-/. I'm trying to write a program that moves a text file to another location. here is the real code ;-):

    Code:
    #include <iostream>
    #include <conio.h>
    #include <windows.h>
    
    using namespace std;
    
    int main()
    {
        if(!MovFile("C:\temp.txt", "C\temp\temp.txt"))
        {
            cout<<"MoveFile failed!";
            getch()
            return 0;
        }
    return 0;
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    > if(!MovFile("C:\temp.txt", "C\temp\temp.txt"))
    Use \\ for each \ in your pathnames
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    26
    after doing what you said here's the code:
    Code:
    #include <iostream>
    #include <conio.h>
    #include <windows.h>
    
    using namespace std;
    
    int main()
    {
        if(!MoveFile("C:\\temp.txt", "C\\temp\\temp.txt"))
        {
            cout<<"MoveFile failed!";
            getch();
            return 0;
        }
    return 0;
    }
    but MoveFile() still fails

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    26
    ahh! Never mind i just didnt put ":" after volume label C lol. Thank you alot.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21