-
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?
-
>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
}
-
</code>
why doesnt this work?
Because you used <> instead of [ ]
-
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;
}
-
> if(!MovFile("C:\temp.txt", "C\temp\temp.txt"))
Use \\ for each \ in your pathnames
-
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
-
ahh! Never mind i just didnt put ":" after volume label C lol. Thank you alot.