Finishing this memory mapping palindrome and keep getting file not found. If anyone has the time please take a look at this I debugged to if (argc >1) and that is where I ran into problems. Also could someone also just verify my palindrome is set up correctly?

#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;

	//Test Files -------------------------------------------------------------
			 char inFilename[] = "C:\\Users\\Bryan\\Documents\\CSI 345\\Palindrome\\Palindrome\\Input.txt";
			 argv[1] = "C:\\Users\\Bryan\\Documents\\CSI 345\\Palindrome\\Palindrome\\Input.txt";
	 //--------------------------------------------------------------------------

	//VERIFYING ARGUMENTS
	LPCTSTR *myStr1 = (LPCTSTR *)argv[1];
	if(argc>1)
	{
		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);
		LPCTSTR *myStr2 = (LPCTSTR *)argv[2]; 

		//Test Files -------------------------------------------------------------
			 char outFilename[] = "C:\\Users\\Bryan\\Documents\\CSI 345\\Palindrome\\Palindrome\\Output.txt";
			 argv[2] = "C:\\Users\\Bryan\\Documents\\CSI 345\\Palindrome\\Palindrome\\Output.txt";
		//--------------------------------------------------------------------------


		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);
	}
	//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;
}

Recommended Answers

All 7 Replies

Never Never load a value into argv[] variables! Never! You will probably crash the program by overwriting memory you don't want written. And if it doesn't crash, you have no idea what will happen to your program. Like maybe a file not found.

Is this at least improvement...

#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;

	//Test Files -------------------------------------------------------------
			 char inFilename[] = "C:\\Users\\Bryan\\Documents\\CSI 345\\Palindrome\\Palindrome\\Input.txt";
			 argv[1] = "C:\\Users\\Bryan\\Documents\\CSI 345\\Palindrome\\Palindrome\\Input.txt";
	 //--------------------------------------------------------------------------

	//VERIFYING ARGUMENTS
	//LPCTSTR *myStr1 = (LPCTSTR *)argv[1];
	if(argc>1)
	{
		readFile = CreateFile(inFilename, 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);
		//LPCTSTR *myStr2 = (LPCTSTR *)argv[2]; 

		//Test Files -------------------------------------------------------------
			 char outFilename[] = "C:\\Users\\Bryan\\Documents\\CSI 345\\Palindrome\\Palindrome\\Output.txt";
			 argv[2] = "C:\\Users\\Bryan\\Documents\\CSI 345\\Palindrome\\Palindrome\\Output.txt";
		//--------------------------------------------------------------------------


		writeFile = CreateFile(outFilename, 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);
	}
	//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;
}

Simplify this:

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
}

to this :

bool isPalindrome(const std::string& s){
    return s == string(s.rbegin(),s.rend());
}

When I debug this it looks like it shows that it cannot open at

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

can someone let me know if this is the issue and if it is then what I need to do

OK I got my input file to work and it now I do a copy con input.txt and it loads into my input file successfully....

Now when I open my output file it is not doing as it should...I also checked the requirements and I do need argc and argv...can anyone see why my output file is blank as it should be storing palindromes?

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

bool isPalindrome(const std::string& s)
{
    return s == string(s.rbegin(),s.rend());
}

//MAIN FUNCTION

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

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

	//Test file
	//char inFilename[] = "C:\\Users\\Bryan\\Documents\\CSI 345\\Palindrome\\Palindrome\\Input.txt";
	char inFilename[] = "C:\\Users\\Bryan\\Documents\\CSI 345\\Palindrome\\Palindrome\\Input.txt";

    //VERIFYING ARGUMENTS
    if(argc = 2 )
    {
        cout << "opening for read: " << argv[1] << endl;
        readFile = CreateFile(inFilename, 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
            cout << "Read file could not be opened. Error code: " << GetLastError() << 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
            cout << "Read file map could not be opened. Error code: " << GetLastError() << 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
            cout << "Could not map view of read file. Error code: " << GetLastError() << endl;
            CloseHandle(readFileMap);
            CloseHandle(readFile);
            return(FALSE);
        }

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

        cout << "Opening for write: " << argv[2] << endl;
        //writeFile = CreateFile(argv[2], GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);

		//test file
		char outFilename[] = "C:\\Users\\Bryan\\Documents\\CSI 345\\Palindrome\\Palindrome";
		//char outFilename[] = "C:\\Users\\Bryan\\Documents\\CSI 345\\Palindrome\\Palindrome\\Output.txt";

        writeFile = CreateFile(outFilename, 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
            cout << "Write file could not be opened. Error code: " << GetLastError() << endl;
            //cout << "argv[1] = " << argv[1] << " argv[2] = " << argv[2] << 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
            cout << "Write File could not be mapped. Error code: " << GetLastError() << 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
            cout << "Could not open map view of write file. Error code: " << GetLastError() << endl;
        }

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

    }
    //ELSE STATEMENT IF CANNOT FIND FILE
    else
    {
        //DISPLAY ERROR MESSAGE THAT NO FILE IS Given
        switch( argc )
        {
            case 0:
                cout << "No arguments given" << endl;
                break;
            case 1:
                cout << "No read file or write file given" << endl;
                break;
            case 2:
                cout << "No write file given" << endl;
                break;
            default:
                cout << "too many arguments given." << endl;
        }
    }

    //cout << "Arg 0 = file to read" << endl;
    //cout << "Arg 1 = file to write to" << endl;

    //RETURN A VALUE
    return 0;
}

Now that you've created a mapping of your input file and output file, what do you intend to do with each? You don't call isPalindrome() anywhere, nor does it look like you even iterate over your input mapping or set anything in your output mapping.

Has anyone asked why you're memory-mapping the files in the first place, instead of simply reading out of an input file and writing into an output file?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.