Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I believe I need to cast argv[1] and argv[2] to a LPCTSTR data type to remove th

ID: 3626274 • Letter: I

Question

I believe I need to cast argv[1] and argv[2] to a LPCTSTR data type to remove the error.

Here is my code...Can someone please show me how to do this?

#include <windows.h>
#include <iostream>
#include <string>
#include <algorithm>
#include <list>
using namespace std;

bool isPalindrome(const std::string& s)
{
std::string sReverse = s;
std::reverse(sReverse.begin(), sReverse.end());
return s == sReverse; // return true if the reverse is the same as non-reverse
}


//MAIN FUNCTION

int main(int argc, char *argv[])
{

HANDLE readFile, writeFile;
HANDLE readFileMap, writeFileMap;
PVOID pvreadFile, pvwriteFile;
DWORD readFileSize;
string word = "";
list<string> words;





//VERIFYING ARGUMENTS
if(argc>1)
{
//char inFilename[] = "C:\Users\Bryan\Documents\CSI 345\Palindrome\Palindrome\Input.txt";
//argv[1] = "C:\Users\Bryan\Documents\CSI 345\Palindrome\Palindrome\Input.txt";
readFile = CreateFile(argv[1], GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

//IF STATEMENT TO CHECK IF THE READ FILE IS NOT VALID
if(readFile == INVALID_HANDLE_VALUE)
{
//DISPLAY ERROR MESSAGE
std::cout << "Read file could not be opened." << std::endl;
return(FALSE);
}

readFileMap = CreateFileMapping(readFile, NULL, PAGE_READONLY, 0, 0, NULL);

//IF STATEMENT TO SEE IF THE READFILEMAP IS NULL
if(readFileMap == NULL)
{
//DISPLAY ERROR MESSAGE
std::cout << "Read file map could not be opened." << std::endl;
CloseHandle(readFile);
return(FALSE);
}

pvreadFile = MapViewOfFile(readFileMap, FILE_MAP_READ, 0, 0, 0);

//IF STATEMENT TO DETERMINE IF PVREADFILE IS NULL
if(pvreadFile == NULL)
{
//DISPLAY ERROR MESSAGE
std::cout << "Could not map view of read file." << std::endl;
CloseHandle(readFileMap);
CloseHandle(readFile);
return(FALSE);
}

//DETERMINE SIZE LIMIT OF INPUT FILE
readFileSize = GetFileSize(readFile, NULL);

//writeFile = CreateFile(argv[2], GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
writeFile = CreateFile(argv[2], GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

//IF STATEMENT TO DETERMINE IF WRITE FILE IS VALID OR NOT
if(writeFile == INVALID_HANDLE_VALUE)
{
//DISPLAY ERROR MESSSAGE IF FILE CAN'T BE OPENED
std::cout << "Write file could not be opened." << std::endl;
//std::cout << "argv[1] = " << argv[1] << " argv[2] = " << argv[2] << std::endl;
UnmapViewOfFile(pvreadFile);
CloseHandle(readFileMap);
CloseHandle(readFile);
return(FALSE);
}

writeFileMap = CreateFileMapping(writeFile, NULL, PAGE_READWRITE, 0, readFileSize, NULL);

//IF STATEMENT TO DETERMINE IF WRITE FILE MAP IS NULL
if(writeFileMap == NULL)
{
//DISPLAY ERROR MESSAGE THAT THE WRITE FILE CANNOT BE MAPPED
std::cout << "Write File could not be mapped." << std::endl;
CloseHandle(writeFile);
UnmapViewOfFile(pvreadFile);
CloseHandle(readFileMap);
CloseHandle(readFile);
return(FALSE);
}

pvwriteFile = MapViewOfFile(writeFileMap, FILE_MAP_WRITE, 0,0,0);

//IF STATEMENT IF THE PVWRITEFILE IS NULL
if(pvwriteFile == NULL)
{
//DISPLAY ERROR MESSAGE THAT I COULD NOT OPEN MAP VIEW OF WRITE FILE
std::cout << "Could not open map view of write file." << std::endl;
}

//POINTERS NEED TO BE CREATED
PSTR readptr = (PSTR) pvreadFile;
PSTR writeptr = (PSTR) pvwriteFile;

{
bool ret = isPalindrome( "eve redivider" );
}

//CLEANUP THE FILE
UnmapViewOfFile(pvwriteFile);
UnmapViewOfFile(pvreadFile);
CloseHandle(writeFileMap);
CloseHandle(readFileMap);
CloseHandle(writeFile);
CloseHandle(readFile);

}

//if(argc>1)
//{
// stuff I don't care about
//}



//ELSE STATEMENT IF CANNOT FIND FILE
else
//DISPLAY ERROR MESSAGE THAT NO FILE IS Given
cout << "No file given" << endl << endl;

//RETURN A VALUE
return 0;
}

Explanation / Answer

use A2CT function or W2A

replace follwing line

readFile = CreateFile(argv[1], GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

with

readFile = CreateFile(A2CT(argv[1]), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);



Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote