Could someone please run this code in XP and let me know if it works. I am on Windows 7 and have looked at this code for 2 months now and not able to find a solution to save my life. If you see what is wrong with it though please let me know my output keeps getting "file not given"

#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)
	{
		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);
	}
	//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 9 Replies

How do you run this program?

I just do the start without debugging...is that not right?

I just do the start without debugging...is that not right?

The problem here is that the input and output file paths are obtained from command line parameters. And as you're running the program via the IDE no parameters are currently being passed to your program.

But you can set up the command line parameters for your program in the VS IDE by doing the following:
1. Open up the properties page for your main project
2. In 'configuration properties->Debugging' you can enter the filenames/parameters in the 'command arguments' field.
3. Hit OK on the properties page to accept the changes.

With the command line parameters set, you can then run the program using F5 or Ctrl+F5. The parameters you set in the project properties will now be passed to the program each time it is ran from the IDE.

The other way of testing the program would be to open up a command line and then cd into whichever directory your program is in and run it from the command line passing the filenames.
e.g.
myprogram.exe file1 file2

so the only two file names are the input and output file correct?

I ran the debugger and the issue is in this line which is line 28...

if (argc>1)

This mean argc <= 1 but I dont know what I need to do to change this

You need to add command-line arguments. Jason just told you how to do that, either through VisualStudio or by running the program from a command-prompt.

I think I read on how to fix this but not sure how I am to do this....


If I wanted to could someone show me how would I cast argv[1] and argv[2] to a LPCTSTR data type?

searching for LPCTSTR on msdn.microsoft.com:

"LPCTSTR: An LPCWSTR if UNICODE is defined, an LPCSTR otherwise."

"LPCWSTR: typedef CONST WCHAR *LPCWSTR;"
"LPCSTR: typedef __nullterminated CONST CHAR *LPCSTR;"

Since your argv array is already defined as type char*[] (an array of char* elements), I'm going to assume you don't need Unicode support yet, and suggest that they're so close to one-and-the-same, that you can simply cast directly:

LPCTSTR *myStr1 = (LPCTSTR *)argv[1];
LPCTSTR *myStr2 = (LPCTSTR *)argv[2];

I am guessing I put the following before if (argc>1)...


LPCTSTR *myStr1 = (LPCTSTR *)argv[1];
LPCTSTR *myStr2 = (LPCTSTR *)argv[2];

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.