I am trying to get the file size of a memory mapped I/O from the input file, and set that size to the output file. Right now the code below is stuck on the kernel mapping for the output. The program is suppose to map an input and an output and transfer all the writing from the input to the output with changes made to a specific ASCII character.
The two problems I'm having trouble understanding are how to get the file size of the input to be the same in the output, and how to actually write data to the output file. If there are any resources out there that I can read up on that would be helpful at least, but msdn is so cryptic that I cannot find what I'm looking for.
The two problems I'm having trouble understanding are how to get the file size of the input to be the same in the output, and how to actually write data to the output file. If there are any resources out there that I can read up on that would be helpful at least, but msdn is so cryptic that I cannot find what I'm looking for.
Code:
#include <windows.h> #include <iostream> using namespace std; int main(int argc, char *argv[]) { HANDLE InFile; HANDLE OutFile; HANDLE InFileMap; HANDLE OutFileMap; PVOID InPVFile; PVOID OutPVFile; DWORD InFS; DWORD OutFS; int i; if (argv[4] == NULL) { cout << "ERROR:: Not enough parameters." << endl; return (0); } //Opening an input file. InFile = CreateFile(argv[1], GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (InFile == INVALID_HANDLE_VALUE) { cout << "File could not be opened." << endl; return(false); } //Creating a file-mapping kernel object for input. InFileMap = CreateFileMapping(InFile, NULL, PAGE_READONLY, 0, 0, NULL); if (InFileMap == NULL) { cout << "File map could not be opened. IN" << endl; CloseHandle(InFile); return(false); } //Mapping a view of the input file. InPVFile = MapViewOfFile(InFileMap, FILE_MAP_READ, 0, 0, 0); if (InPVFile == NULL) { cout << "Could not map view of file." << endl; CloseHandle(InFileMap); CloseHandle(InFile); return(false); } InFS = GetFileSize(InFile, NULL); //Create an output file. OutFile = CreateFile(argv[2], GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (OutFile == INVALID_HANDLE_VALUE) { cout << "Could not create output file." << endl; return(false); } //Creating a file-mapping kernel object for output. OutFileMap = CreateFileMapping(OutFile, NULL, PAGE_READWRITE, 0, 0, NULL); if (OutFileMap == NULL) { cout << "File map could not be opened. OUT" << endl << InFS; CloseHandle(OutFile); return(false); } //Mapping a view of the output file. OutPVFile = MapViewOfFile(OutFileMap, FILE_MAP_WRITE, InFS, InFS, 0); if (OutPVFile == NULL) { cout << "Could not map view of file." << endl; CloseHandle(OutFileMap); CloseHandle(OutFile); return(false); } //Switching the ASCII characters. PSTR pchANSI = (PSTR) InPVFile; PSTR pchANSO = (PSTR) OutPVFile; for (i=0; i < InFS; i++) { if (&pchANSI[i] == argv[3]) { pchANSO[i] = *argv[4]; } else pchANSO[i] = pchANSI[i]; } if (i == InFS) { OutPVFile = pchANSO; } //Housekeeping UnmapViewOfFile(InPVFile); UnmapViewOfFile(OutPVFile); CloseHandle(InFileMap); CloseHandle(OutFileMap); CloseHandle(InFile); CloseHandle(OutFile); }
Comment