CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 6 1234 ... LastLast
Results 1 to 15 of 84
  1. #1
    Join Date
    May 2010
    Posts
    76

    Can someone please run this in XP

    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"

    Code:
    #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;
    }

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

    Re: Can someone please run this in XP

    Quote Originally Posted by jimJohnson123 View Post
    If you see what is wrong with it though please let me know my output keeps getting "file not given"
    It took you two months to see that this is executed?
    Code:
    	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;
    So why does it fall into the else block? (and please fix your formatting).

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; May 19th, 2011 at 09:32 PM.

  3. #3
    Join Date
    May 2010
    Posts
    76

    Re: Can someone please run this in XP

    I can see that this is what is executing I just cant see what is wrong with it. I believe my issue is in the CreateFile but not fore sure

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

    Re: Can someone please run this in XP

    Quote Originally Posted by jimJohnson123 View Post
    I can see that this is what is executing I just cant see what is wrong with it. I believe my issue is in the CreateFile but not fore sure
    ???

    Are you looking at the code? It is clearly obvious that argc is <= 1 causing that line to be printed. I even simplified it for you in my previous post.

    Now why is argc <= 1? It has nothing to do with CreateFile() not working. It has everything to do with your command-line arguments not being correct.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    May 2010
    Posts
    76

    Re: Can someone please run this in XP

    Would anyone else be able to look at this. I mean no disrespect Paul but I am really not a good at the programming aspect for my degree(which is not what I am even going into) and dont understand exactly what I am supposed to do still but I do appreciate you trying and I will rate you top in the post ratings.

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Can someone please run this in XP

    Quote Originally Posted by jimJohnson123 View Post
    ..
    I ... dont understand exactly what I am supposed to do still ...
    Well, What else could I add to the Paul's answer?
    If you still don't understand the very trivial logic with if...else block then, please, set a break point at the beginning of your main(), start debugging (F5), and debug your code step-by-step (F10) to see the variable values and where and why you will "go" with your code.

    PS: and don't expect someone will run/test your code because the reason of your problem is very obvious.
    Victor Nijegorodov

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

    Re: Can someone please run this in XP

    Quote Originally Posted by jimJohnson123 View Post
    Would anyone else be able to look at this.
    I told you what was wrong.

    I don't understand -- did you write this code? If you did, then how can you not know what if/else is supposed to do? No one else can break it down for you any simpler than that.
    Code:
    if ( this statement is true )
    {
        // do this stuff
    }
    else  // statement is not true
    {
        // do this instead
    }
    So look at your code -- replace "this statement is true" with "argc>1". Now, the only way that line could have printed was if argc>1 is not true. So by using logic, argc must have been <= 1 for that line to be printed, since argc>1 was false.

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    May 2010
    Posts
    76

    Re: Can someone please run this in XP

    is this what you are talking about...

    Code:
    #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);
    		
    	}
    
    		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;
    }

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

    Re: Can someone please run this in XP

    Quote Originally Posted by jimJohnson123 View Post
    is this what you are talking about...
    That "stuff I don't care about" is what I wrote, because I really don't care about all of that code you posted. Again:
    Code:
    //VERIFYING ARGUMENTS
    if(argc>1)
    {
       // this is all of your code, and I don't care what it does, 
       // so please don't post it as it has absolutelty nothing to do with your problem!
    }
    else
    //    cout an error saying you can't open the file.
    That is a synopsis of the code you posted. So how does it get into the "else" portion? It doesn't matter what that code is that you posted does inside the if() block -- it has absolutely nothing to do with the issue, so it need not be posted over and over again. The only way it can get to that line is if argc <=1. So where does argc come from?
    Code:
    int main(int argc, char *argv[])
    So why is argc <= 1? That is what I'm asking you. How did argc turn out to be <= 1 instead of > 1? Where does argc get its value? Something gave argc a value, so what is it? (I know what it is -- I'm asking if you know what it is that gives argc its value).

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; May 21st, 2011 at 06:11 PM.

  10. #10
    Join Date
    Jul 2010
    Posts
    94

    Re: Can someone please run this in XP

    That is a mixed salad of C++ carrots and Win32 apis potato chips. Anyway it works on my XP after some minor corrections

  11. #11
    Join Date
    May 2010
    Posts
    76

    Re: Can someone please run this in XP

    aight thanks still tryin to figure it out but appreciate the help

  12. #12
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: Can someone please run this in XP

    If the error message is "file not given", then you are not supplying
    the file at the command prompt when you execute the program.

    What are you typing to execute the code ?

    Why bother with argc/argv ? Get the code working with a hard-wired
    filename.

  13. #13
    Join Date
    May 2010
    Posts
    76

    Re: Can someone please run this in XP

    I really am just going in with start without debugging in visual studio

  14. #14
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Can someone please run this in XP

    You must start debugging!
    Victor Nijegorodov

  15. #15
    Join Date
    May 2010
    Posts
    76

    Re: Can someone please run this in XP

    this is what it shows...

    'Palindrome.exe': Loaded 'C:\Users\Bryan\Documents\CSI 345\Palindrome\Debug\Palindrome.exe', Symbols loaded.
    'Palindrome.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll', Cannot find or open the PDB file
    'Palindrome.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll', Cannot find or open the PDB file
    'Palindrome.exe': Loaded 'C:\Windows\SysWOW64\KernelBase.dll', Cannot find or open the PDB file
    'Palindrome.exe': Loaded 'C:\Windows\SysWOW64\msvcp100d.dll', Symbols loaded.
    'Palindrome.exe': Loaded 'C:\Windows\SysWOW64\msvcr100d.dll', Symbols loaded.
    The program '[6548] Palindrome.exe: Native' has exited with code 0 (0x0).

Page 1 of 6 1234 ... LastLast

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