CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    May 2010
    Posts
    76

    Palindrome program

    I am just finishing this palindrome program but I keep getting the error..."file not given" and I believe the error is in this line but cannot be sure...

    Code:
    #include <windows.h>
    #include <iostream>
    #include <string>
    #include <list>
    using namespace std;
    
    //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;
    
    		//INPUT WORDS FROM FILE WITH FOR STATEMENT
    		for(int i=0; i < readFileSize; i++)
    		{
    			while(isalpha(readptr[i]))
    			{
    				word = word += tolower(readptr[i]);
    				i++;
    			};
    			words.push_front(word);
    			word = "";
    		}
    
    		//SORT OUT THE WORDS
    		words.sort();
    
    		//WRITE THE WORDS THAT ARE SORTED TO THE OUTPUT FILE AND USE A FOR STATEMENT
    		for(int i=0; i<readFileSize; i++)
    		{
    			word = words.front();
    			int j = 0;
    
    			//CREATE WHILE STATEMENT
    			while(j < word.size())
    			{
    				writeptr[i] = word[j];
    				j++;
    				i++;
    			};
    			writeptr[i] = ' ';
    			words.pop_front();
    		}
    
    		//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;
    }
    Last edited by jimJohnson123; April 24th, 2011 at 06:21 PM. Reason: code tags

  2. #2
    Join Date
    May 2010
    Posts
    76

    Re: Palindrome program

    and Sorry about the code tags will need someone to show how to put them in for this site as well

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Palindrome program

    Quote Originally Posted by jimJohnson123 View Post
    and Sorry about the code tags will need someone to show how to put them in for this site as well
    The ending code tag must be [/code].

    Second, for a palindrome program, you sure are doing a lot of non-palindrome related things, such as opening and closing files. As a matter of fact, where is the "palindrome" code in this program?

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Palindrome program

    Quote Originally Posted by jimJohnson123 View Post
    I am just finishing this palindrome program but I keep getting the error..."file not given" and I believe the error is in this line but cannot be sure...
    Use your debugger.

    You keep getting that error because the logic flow of your program goes to that line. If you want to figure out why the logic goes to that line, debug the program and single step through it.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    May 2010
    Posts
    76

    Re: Palindrome program

    I know I am but it is required to use memory mapping (why I dont have the slightest idea but it is what he wants to see)

    Here is the palindrome section which should work...

    Code:
    		//INPUT WORDS FROM FILE WITH FOR STATEMENT
    		for(int i=0; i < readFileSize; i++)
    		{
    			while(isalpha(readptr[i]))
    			{
    				word = word += tolower(readptr[i]);
    				i++;
    			};
    			words.push_front(word);
    			word = "";
    		}
    
    		//SORT OUT THE WORDS
    		words.sort();
    
    		//WRITE THE WORDS THAT ARE SORTED TO THE OUTPUT FILE AND USE A FOR STATEMENT
    		for(int i=0; i<readFileSize; i++)
    		{
    			word = words.front();
    			int j = 0;
    
    			//CREATE WHILE STATEMENT
    			while(j < word.size())
    			{
    				writeptr[i] = word[j];
    				j++;
    				i++;
    			};
    			writeptr[i] = ' ';
    			words.pop_front();
    		}
    
    		//CLEANUP THE FILE
    		UnmapViewOfFile(pvwriteFile);
    		UnmapViewOfFile(pvreadFile);
    		CloseHandle(writeFileMap);
    		CloseHandle(readFileMap);
    		CloseHandle(writeFile);
    		CloseHandle(readFile);
    	}

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Palindrome program

    Quote Originally Posted by jimJohnson123 View Post
    Here is the palindrome section which should work...
    Seriously, I see nothing in that code that indicates "palindrome". I see no code checking if a string forward is the same reversed.

    That's why functions are important in a program. No one would know what that block of code is supposed to do.
    Code:
    #include <string>
    #include <algorithm>
    
    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
    }
    
    int main()
    {
        bool ret = isPalindrome( "eve redivider" );
    }
    That program and function above is a very strict palindrome checker (it takes in consideration everything -- casing, white space, punctuation, etc.). If it's too strict, then you rewrite it to be less strict.

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Apr 2011
    Posts
    3

    Re: Palindrome program

    Paul McKenzie is right

  8. #8
    Join Date
    May 2010
    Posts
    76

    Re: Palindrome program

    How about this for my palindrome code...

    Code:
     // Strart of Palindrome Catcher.
    	  char compare;
    	  bool drome = false;
    	  char tmpArray [600];
    	  char tmpOutArray [600];
    	  int incount = 0;
    	  int outcount = dwOutFileSize;
    	  int t = 0;
    	  stack<char> stack;  //Useing #stack
    	  queue<char> queue;
    
    	  // read the values of the memory-mapped file
          // from start to finish placing values in
          //array
    //-----------------------------------------	  
    	 for (i = 0; i < dwInFileSize; i++) 
        {
    		InFileptr[i];
    	 }
    	  for (i = 0; i < dwInFileSize; i++) 
        {
    		stack.push(tmpArray[i]);
    	 }
    
    
    			//If a space is not found push all chars onto array & increase the input count	
    		if (InFileptr[i] != ' ') 
    		{
    			stack.push(tmpArray[i]);
    			queue.push(tmpOutArray[i]);
    		//test for output ----------------
    		//	stack.push(outFileMap[i]);
    		//----------------------------------
    			incount++;	
    		}
       //If a space is found compare chars one by one with the same word in reverse 
    		if (InFileptr[i] == ' ')
    		{
    			while (!stack.empty())
    			{
    					stack.top() = tmpArray[t];
    					t ++;
    					stack.pop() ;
    					cout << "top:" << stack.top()<< endl;
    
    					outcount --;
    					int i = 0;
    
    
    		//while (!stack.empty())
    		//	if (tmpArray[t] != stack.pop()) return false;
    		//	return true;
    					//if (compare == InFileptr[i]);
    					//{
    
    					//}
    
    				}
    				
    		}
    	  }
    
    	  //Cleanup
    	  UnmapViewOfFile(pvInFile);
          CloseHandle(inFileMap);
          CloseHandle(inFile);
          UnmapViewOfFile(outFile);
          CloseHandle(outFileMap);
          CloseHandle(outFile);
    }

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Palindrome program

    Quote Originally Posted by jimJohnson123 View Post
    How about this for my palindrome code...
    Compare that code with what I wrote. Which one looks easier and more organized? I wrote a palindrome checker in 3 lines of code.

    Regardless of where the input comes from, I would have expected that you create a string -- how you create that string doesn't matter. Then once that string is created, you check if that string is a palindrome.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; April 24th, 2011 at 07:28 PM.

  10. #10
    Join Date
    May 2010
    Posts
    76

    Re: Palindrome program

    Yours look much better and orgnized but I guess it just looks too simple unless Im really overthinking this

  11. #11
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Palindrome program

    Quote Originally Posted by jimJohnson123 View Post
    Yours look much better and orgnized but I guess it just looks too simple unless Im really overthinking this
    Code:
    1) Create the string.
    2) Check if string is a palindrome
    Code:
    int main()
    {
         std::string s = GetMyStringFromMemoryMappedFile( );
         if ( isPalindrome( s ) )
         {
            cout << s << " is a palindrome";
         }
    }
    Isn't this all it boils down to?

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured