I am trying to finish this palindrome program and the only output I get is output cannot be found. Can anyone show me what I am doing wrong. I will put the instructions in if needed for a guide to show what it needs to do...

#include <windows.h>
#include <iostream>
#include <stack>
#include <queue> 
using namespace std;



//START THE MAIN FUNCTION
int main(int argc, char *argv[])
{
	//BEGIN THE INPUT FILE MAPPING
	HANDLE hFile;
    HANDLE hFileMap;
    PVOID pvFile;
    DWORD dwFileSize;
    int i = 0;
	
	//HAVE USER ENTER THE PATH FOLLOWED BY THE FILE NAME
	//cout<< "Please enter the path and filename: ";

	//cout << endl;

	//Test for validity
	char hFilename[] = "C:\\Users\\Bryan\\Documents\\CSI 345\\Palindrome\\Input.txt"; 
	argv[1] = "C:\\Users\\Bryan\\Documents\\CSI 345\\Palindrome\\Input.txt"; 
  
	//ALLOW THE HFILE TO RETURN A HANDLE TO THE OPEN FILE
	hFile = CreateFile(argv[1], GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

	//IF STATEMENT TO CHECK IF FILE CAN BE OPENED
    if (hFile == INVALID_HANDLE_VALUE)
      {
			//DISPLAY MESSAGE THAT FILE CANNOT BE OPEN
            cout << "File could not be opened. " << endl;
            return(FALSE);
      }
      
      //OPENS A FILE MAPPED OBJECT
      hFileMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);

	  //IF STATEMENT TO SEE IF HFILEMAP CAN BE OPENED
      if (hFileMap == NULL)
      {
			//DISPLAY MESSAGE THAT THIS FILE CANNOT BE OPENED
            cout << "File map could not be opened. " << endl;
            CloseHandle(hFile);
            return(FALSE);
      }
      
      //GIVES ADDRESS FOR DATA FILE
      pvFile = MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 0);

	  //IF STATEMENT TO SEE IF PV FILE CAN BE OPENED
      if (pvFile == NULL)
      {
			//DISPLAY MESSAGE THAT THIS FILE CANNOT BE OPENED
            cout << "Could not map view of File. " << endl;
            CloseHandle(hFileMap);
            CloseHandle(hFile);
            return(FALSE);
      }

 
      //GIVES THTE SIZE OF THE FILE    
      dwFileSize = GetFileSize(hFile, NULL);

    //BEGIN OUTPUT FILE MAPPING
	HANDLE oFile;
    HANDLE oFileMap;
    PVOID pvOFile;
    DWORD dwOFileSize;

	//TEST FOR VALIDITY
	char oFilename[] = "C:\\Users\\Bryan\\Documents\\CSI 345\\Palindrome\\Output.txt";  
    argv[2] = "C:\\Users\\Bryan\\Documents\\CSI 345\\Palindrome\\Output.txt";

   
	//HAVE USER ENTER THE PATH FOLLOWED BY THE FILE NAME
    cout << "Please enter path and filename: ";
	cout << endl;

	//ALLOW THE oFILE TO RETURN A HANDLE TO THE OPEN FILE
	oFile = CreateFile(oFilename, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

	//IF STATEMENT TO CHECK IF FILE CAN BE OPENED
	if (oFile == INVALID_HANDLE_VALUE)
	{  
		//DISPLAY A MESSAGE FILE CANNOT BE OPENED
		cout << "outFile could not be opened." << endl;
        return(FALSE);
     }

	//OPENS A FILE MAPPED OBJECT
	oFileMap = CreateFileMapping(oFile, NULL, PAGE_READWRITE, 0, dwFileSize, NULL);

	//OPENS A FILE MAPPED OBJECT
    if (oFileMap == NULL)
    {

		//DISPLAY A MESSAGE FILE CANNOT BE OPENED
        cout << "File map could not be opened." << endl;
        CloseHandle(oFile);
        return(FALSE);
    }

	//GIVES ADDRESS FOR DATA FILE
    pvOFile = MapViewOfFile(oFileMap, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
 
	//IF STATEMENT TO SEE IF PV FILE CAN BE OPENED
    if (pvOFile == NULL)
    {
		//DISPLAY A MESSAGE FILE CANNOT BE OPENED
		cout << "Could not map view of File." << endl;
        CloseHandle(hFileMap);
        CloseHandle(hFile);
        return(FALSE);
      }

      //INPUT FILE SIZE AND OUTPUT FILE SIZE MUST MATCH UP
	  dwOFileSize = dwFileSize;
	  
	  //TEST THE SIZE OF THE FILE
      cout << "The file size is: " << dwOFileSize << endl;


      // pointer to the beginning address
      PSTR hptr = (PSTR) pvFile;
      PSTR optr = (PSTR) pvOFile;


	  // CHECK FOR PALINDROMES
	  char check;
	  bool palindrome = false;
	  char testArray [600];
	  char testOutArray [600];
	  int icount = 0;
	  int ocount = dwOFileSize;
	  int g = 0;
	  stack<char> stack;  
	  queue<char> queue;

	 //Begin for statement 
	 for (i = 0; i < dwFileSize; i++) 
    {
		hptr[i];
	 }
	  for (i = 0; i < dwFileSize; i++) 
    {
		stack.push(testArray[i]);
	 }


		//CHECKS TO MAKE SURE A SPACE IS NOT FOUND
	    //IF SPACE IS NOT FOUND BEGIN TO PUSH ALL CHARACTERS TO ARRAYS
		if (hptr[i] != ' ') 
		{
			stack.push(testArray[i]);
			queue.push(testOutArray[i]);
		
			//TEST OUTPUT
			icount++;	
		}
		//IF THERE IS A SPACE COMPARE THE CHARACTERS IN REVERSE ORDER 
		if (hptr[i] == ' ')
		{
			//PERFORM WHILE STATEMENT FOR WHEN THTE STACK IS NOT EMPTY
			while (!stack.empty())
			{
					stack.top() = testArray[g];
					g ++;
					stack.pop() ;
					cout << "The top is:" << stack.top() << endl;

					ocount --;
					int i = 0;
			}
				
		}
	  }

Create a utility that transfers words that are palindromes in the input file into the output file. Place a space between each palindrome written to the output file.
The input path and file name and the output path and file name must be gathered at runtime from the user. This can be accomplished via command line inputs (see argv and argc parameters in the main() function).
The program must use memory-mapped file input/output.
The contents of the input file must not be changed by your program (read only).
The definition of a palindrome, for purposes of this programming assignment, is a word which reads the same backward or forward. Examples of palindromes are mom, pop, kayak, noon, radar, and racecar.
While there are a number of methods available to test to see if a word is a palindrome, the most common method is to use a stack. Character[0] of the original string can be compared with character[n-1]. The next comparison would address character[1] with character[n-2]. Comparison continues until all characters are compared (the stack is empty) or there is an inequality (the word is not a palindrome).
The output file should contain only those words that are palindromes from the input file. Each word in the output file should be separated by a space.
Since the length of the output file is determined at runtime, and there is no way to predict how many palindromes will be present in the input file (without actually performing the test), there is no means to accurately predict the output file size required. Set the output file size to the input file size.

DO NOT ASSIGN VALUES to the vector argv[] (lines 27, 77, etc). Those are set by the system when the program runs. You have probably munged program memory as a result.

I tried that and I get the "file cannot be opened" output when before it would pass that to file map could not be opened...

#include <windows.h>
#include <iostream>
#include <stack>
#include <queue> 
using namespace std;



//START THE MAIN FUNCTION
int main(int argc, char *argv[])
{
	//BEGIN THE INPUT FILE MAPPING
	HANDLE hFile;
    HANDLE hFileMap;
    PVOID pvFile;
    DWORD dwFileSize;
    int i = 0;
	
	//HAVE USER ENTER THE PATH FOLLOWED BY THE FILE NAME
	//cout<< "Please enter the path and filename: ";

	//cout << endl;

	//Test for validity
	char hFilename[] = "C:\\Users\\Bryan\\Documents\\CSI 345\\Palindrome\\Input.txt";
	argv[1];
  
	//ALLOW THE HFILE TO RETURN A HANDLE TO THE OPEN FILE
	hFile = CreateFile(argv[1], GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

	//IF STATEMENT TO CHECK IF FILE CAN BE OPENED
    if (hFile == INVALID_HANDLE_VALUE)
      {
			//DISPLAY MESSAGE THAT FILE CANNOT BE OPEN
            cout << "File could not be opened. " << endl;
            return(FALSE);
      }
      
      //OPENS A FILE MAPPED OBJECT
      hFileMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);

	  //IF STATEMENT TO SEE IF HFILEMAP CAN BE OPENED
      if (hFileMap == NULL)
      {
			//DISPLAY MESSAGE THAT THIS FILE CANNOT BE OPENED
            cout << "File map could not be opened. " << endl;
            CloseHandle(hFile);
            return(FALSE);
      }
      
      //GIVES ADDRESS FOR DATA FILE
      pvFile = MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 0);

	  //IF STATEMENT TO SEE IF PV FILE CAN BE OPENED
      if (pvFile == NULL)
      {
			//DISPLAY MESSAGE THAT THIS FILE CANNOT BE OPENED
            cout << "Could not map view of File. " << endl;
            CloseHandle(hFileMap);
            CloseHandle(hFile);
            return(FALSE);
      }

 
      //GIVES THTE SIZE OF THE FILE    
      dwFileSize = GetFileSize(hFile, NULL);

    //BEGIN OUTPUT FILE MAPPING
	HANDLE oFile;
    HANDLE oFileMap;
    PVOID pvOFile;
    DWORD dwOFileSize;

	//TEST FOR VALIDITY
	char oFilename[] = "C:\\Users\\Bryan\\Documents\\CSI 345\\Palindrome\\Output.txt";  
    argv[2];

   
	//HAVE USER ENTER THE PATH FOLLOWED BY THE FILE NAME
    cout << "Please enter path and filename: ";
	cout << endl;

	//ALLOW THE oFILE TO RETURN A HANDLE TO THE OPEN FILE
	oFile = CreateFile(oFilename, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

	//IF STATEMENT TO CHECK IF FILE CAN BE OPENED
	if (oFile == INVALID_HANDLE_VALUE)
	{  
		//DISPLAY A MESSAGE FILE CANNOT BE OPENED
		cout << "outFile could not be opened." << endl;
        return(FALSE);
     }

	//OPENS A FILE MAPPED OBJECT
	oFileMap = CreateFileMapping(oFile, NULL, PAGE_READWRITE, 0, dwFileSize, NULL);

	//OPENS A FILE MAPPED OBJECT
    if (oFileMap == NULL)
    {

		//DISPLAY A MESSAGE FILE CANNOT BE OPENED
        cout << "File map could not be opened." << endl;
        CloseHandle(oFile);
        return(FALSE);
    }

	//GIVES ADDRESS FOR DATA FILE
    pvOFile = MapViewOfFile(oFileMap, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
 
	//IF STATEMENT TO SEE IF PV FILE CAN BE OPENED
    if (pvOFile == NULL)
    {
		//DISPLAY A MESSAGE FILE CANNOT BE OPENED
		cout << "Could not map view of File." << endl;
        CloseHandle(hFileMap);
        CloseHandle(hFile);
        return(FALSE);
      }

      //INPUT FILE SIZE AND OUTPUT FILE SIZE MUST MATCH UP
	  dwOFileSize = dwFileSize;
	  
	  //TEST THE SIZE OF THE FILE
      cout << "The file size is: " << dwOFileSize << endl;


      // pointer to the beginning address
      PSTR hptr = (PSTR) pvFile;
      PSTR optr = (PSTR) pvOFile;


	  // CHECK FOR PALINDROMES
	  char check;
	  bool palindrome = false;
	  char testArray [600];
	  char testOutArray [600];
	  int icount = 0;
	  int ocount = dwOFileSize;
	  int g = 0;
	  stack<char> stack;  
	  queue<char> queue;

	 //Begin for statement 
	 for (i = 0; i < dwFileSize; i++) 
    {
		hptr[i];
	 }
	  for (i = 0; i < dwFileSize; i++) 
    {
		stack.push(testArray[i]);
	 }


		//CHECKS TO MAKE SURE A SPACE IS NOT FOUND
	    //IF SPACE IS NOT FOUND BEGIN TO PUSH ALL CHARACTERS TO ARRAYS
		if (hptr[i] != ' ') 
		{
			stack.push(testArray[i]);
			queue.push(testOutArray[i]);
		
			//TEST OUTPUT
			icount++;	
		}
		//IF THERE IS A SPACE COMPARE THE CHARACTERS IN REVERSE ORDER 
		if (hptr[i] == ' ')
		{
			//PERFORM WHILE STATEMENT FOR WHEN THTE STACK IS NOT EMPTY
			while (!stack.empty())
			{
					stack.top() = testArray[g];
					g ++;
					stack.pop() ;
					cout << "The top is:" << stack.top() << endl;

					ocount --;
					int i = 0;
			}
				
		}
	  }

First, we need to know exactly what error you're getting and where the error occurs. Don't leave it to us to read and understand all 181 lines of code -- pinpoint the problem for us.

And rubberman, argv is not a vector but an array of char* -- but you probably knew that...

I apologize for that WaltP so let me explain what it is doing(and I am going to use lines from my first post). Initially I was getting the message in a command prompt box for my message on line 47 "file map could not be opened". I removed everything after argv[1] in line 27 and now I get the message in a command prompt "file cannot be opened" from line 36 so it seems I am going backwards. I hope this helps out.

First, we need to know exactly what error you're getting and where the error occurs. Don't leave it to us to read and understand all 181 lines of code -- pinpoint the problem for us.

And rubberman, argv is not a vector but an array of char* -- but you probably knew that...

Arrays are analogous to the mathematical concepts of the vector, the matrix, and the tensor. Indeed, arrays with one or two indices are often called vectors or matrices, respectively. Arrays are often used to implement tables, especially lookup tables; the word table is sometimes used as a synonym of array.

I meant vector, which is actually what the 'v' part of argv is short for... :-)

Can you print the value of argv[1] ?

Also can you get rid of line 27 completely ?

Are you still trying to open the file in argv[1]? If so, why? Your file name is in hFilename[].

I meant vector, which is actually what the 'v' part of argv is short for... :-)

OK, I can see that. I personally prefer using C/C++ terms for programming concepts rather than math terms when talking about programming. Less confusion. But that's just me.

OK, I can see that. I personally prefer using C/C++ terms for programming concepts rather than math terms when talking about programming. Less confusion. But that's just me.

Yeah. I can relate. Even my wife, a particle physicist, calls me an uber geek!

I made quiet a few changes and I get output in command prompt that "no file is given" which is at the end of my code so I think I am real close but here is my code....

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

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

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

	//checks to make sure that arguments were givin
	if(argc>1){


		readFile = CreateFile(argv[1], GENERIC_READ, 0,
			NULL, OPEN_EXISTING, 
			FILE_ATTRIBUTE_NORMAL, NULL);
		if(readFile == INVALID_HANDLE_VALUE){
			std::cout << "Read file could not be opened." << std::endl;
			return(FALSE);
		}


		readFileMap = CreateFileMapping(readFile, NULL, PAGE_READONLY,
			0, 0, NULL);
		if(readFileMap == NULL){
			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(pvreadFile == NULL){
			std::cout << "Could not map view of read file." << std::endl;
			CloseHandle(readFileMap);
			CloseHandle(readFile);
			return(FALSE);
		}

		//gets the size of the input file
		readFileSize = GetFileSize(readFile, NULL);

		writeFile = CreateFile(argv[2], GENERIC_READ | GENERIC_WRITE, 
			0, NULL, OPEN_EXISTING, 
			FILE_ATTRIBUTE_NORMAL, NULL);
		if(writeFile == INVALID_HANDLE_VALUE){
			std::cout << "Write file could not be opened." << std::endl;
			UnmapViewOfFile(pvreadFile);
			CloseHandle(readFileMap);
			CloseHandle(readFile);
			return(FALSE);
		}

		writeFileMap = CreateFileMapping(writeFile, NULL, PAGE_READWRITE, 
			0, readFileSize, NULL);
		if(writeFileMap == NULL){
			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(pvwriteFile == NULL){
			std::cout << "Could not open map view of write file." << std::endl;
		}

		//creates pointers to the begining address of both the input and output files
		PSTR readptr = (PSTR) pvreadFile;
		PSTR writeptr = (PSTR) pvwriteFile;

		//inputs all the words from file
		for(int i=0; i < readFileSize; i++){
			while(isalpha(readptr[i])){
				word = word += tolower(readptr[i]);
				i++;
			};
			words.push_front(word);
			word = "";
		}

		//sorts all the words 
		words.sort();

		//writes sorted words to output file
		for(int i=0; i<readFileSize; i++){
			word = words.front();
			int j = 0;
			while(j < word.size()){
				writeptr[i] = word[j];
				j++;
				i++;
			};
			writeptr[i] = ' ';
			words.pop_front();
		}

		//cleanup
		UnmapViewOfFile(pvwriteFile);
		UnmapViewOfFile(pvreadFile);
		CloseHandle(writeFileMap);
		CloseHandle(readFileMap);
		CloseHandle(writeFile);
		CloseHandle(readFile);
	}else 
		cout << "No file given" << endl << endl;

	return 0;
}

How are you running the program?

I am running it through debug > start without debugging but I may have to run it through command prompt

On the assumption you're using Visual C++, you can set the command line arguments under the debugging options of the project properties.

The very first if condition if(argc > 1) is failing.

Please post the exact commands used for compiling an running the code

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.