CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 5 of 6 FirstFirst ... 23456 LastLast
Results 61 to 75 of 84
  1. #61
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Can someone please run this in XP

    Quote Originally Posted by jimJohnson123 View Post
    looks like after debugging it shows that it cannot be opened at

    int main(int argc, char *argv[]){
    What?
    Could you be more specific?
    What exactly did you do? Where did you set a break point? Did you debug it step-by-step with F10?
    Victor Nijegorodov

  2. #62
    Join Date
    May 2010
    Posts
    76

    Re: Can someone please run this in XP

    Ok sorry about that....this is what I am doing and i showed where i put my breakpoint in caps so it stands out ....

    Code:
    //MAIN FUNCTION
    //THIS IS WHERE MY BREAK POINT IS...I HIT F5 AND THEN KEEP HITTING F10 UNTIL I AM AT THE END OF MY PROGRAM
    int main(int argc, char *argv[]){
    
    	LPCTSTR *myStr1 = (LPCTSTR *)argv[1];
    	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
    	
    	if(argc=2)
    	{
    		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;
    			std::cout << "value of GetLastError() is " <<  GetLastError() << "\n";
    
    			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;
    }

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

    Re: Can someone please run this in XP

    Well...
    Now try to comment out this line
    Code:
    	if(argc=2)
    and "replay" with debugging.
    Victor Nijegorodov

  4. #64
    Join Date
    May 2010
    Posts
    76

    Re: Can someone please run this in XP

    Ok I think I am making progress on this now. I try to debug and it goes through and tries to open but crashes which I expect as I have set this up for memory mapping but when I open with command prompt I get the message "too many arguments given" Just want to see if I am getting close...

    Code:
    #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";
    
        //VERIFYING ARGUMENTS
        if(argc == 1 )
        {
            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\\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;
    }

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

    Re: Can someone please run this in XP

    Why didn't you do what I asked you? (about commenting out a line in your previous code snippet)
    Why did you show us a new code snippet until you have solved the problem with a previous one?

    Why in the world do you ask for some help but then ignore the answers?
    Victor Nijegorodov

  6. #66
    Join Date
    May 2010
    Posts
    76

    Re: Can someone please run this in XP

    Sorry I commented out the line but did not let me run it
    Its the same program but wanted to try something else
    I did however get it to try opening but after doing so it crashes...Here is what I changed everything to...

    Code:
    #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:\\Palindrome\\Debug\\Input.txt";
    
        //VERIFYING ARGUMENTS
        if(argc == 1 )
        {
            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:\\Palindrome\\Debug\\Output.txt";
    		//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;
    }

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

    Re: Can someone please run this in XP

    Where did it crash? on what line of your code snippet? And how exactly did it crash?
    Victor Nijegorodov

  8. #68
    Join Date
    May 2010
    Posts
    76

    Re: Can someone please run this in XP

    this is what I did...

    Microsoft Windows [Version 6.1.7600]
    Copyright (c) 2009 Microsoft Corporation. All rights reserved.

    C:\Users\Bryan>cd C:\Palindrome\Debug

    C:\Palindrome\Debug>palindrome.exe
    opening for read:
    C:\Palindrome\Debug>

    When I do this it I get a message right away saying palindrome.exe has stopped working...is there a way I can do this by getting rid of the argc and argv as I went over this but just dont understand it that great I am sorry to keep asking pretty much same ???

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

    Re: Can someone please run this in XP

    I asked you to debug
    Can't you debug?
    Victor Nijegorodov

  10. #70
    Join Date
    Jun 2008
    Posts
    592

    Re: Can someone please run this in XP

    Code:
        
    //Test file     
    char inFilename[] = "C:\\Users\\Bryan\\Documents\\CSI 345\\Palindrome\\Palindrome\\Input.txt";
    //VERIFYING ARGUMENTS     
    if(argc == 1 ) 
    {
    cout << "opening for read: " << argv[1] << endl;
    ...
    }
    argc stands for argument count. if you have only one argument, that would be the path to your exe.

    if you write this in your console
    Palindorme.exe "Testing.txt"

    How many arguments will be passed to your exe? 1 or 2??
    I will tell you it is not 1
    argc will = 2
    arg0 will be the path to your exe. this is automatic. you will always receive this before any of your arguments.
    arg1 will be "Testing.txt"

    so if( argc == 1 ) would mean you have ZERO arguments that you passed...

    so if I typed this in the console
    Palindorme.exe "Testing.txt"

    arg[ 0 ] will equal the path to this exe file of yours
    arg[ 1 ] will equal the very first argument that you passed.
    arg[ 2 ] will be anything else you sent to your exe after "Testing.txt"

    here seems to be a good tutorial on this http://www.learncpp.com/cpp-tutorial...ine-arguments/
    Quote Originally Posted by jimJohnson123
    is there a way I can do this by getting rid of the argc and argv as I went over this but just dont understand it that great I am sorry to keep asking pretty much same ???
    If you want to start without the command line args try this,
    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()
    {
    
        HANDLE readFile, writeFile;
        HANDLE readFileMap, writeFileMap;
        PVOID pvreadFile, pvwriteFile;
        DWORD readFileSize;
        string word = "";
        list<string> words;
    
        char inFilename[] = "C:\\Palindrome\\Debug\\Input.txt";
        char outFilename[] = "C:\\Palindrome\\Debug\\Input.out.txt";
    
        cout << "opening for read: " << inFilename << 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: " << outFilename << endl;
        //writeFile = CreateFile(outFilename, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
        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 << "inFilename = " << inFilename << " outFilename = " << outFilename << 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 );
    
    
        return 0;
    }
    But you must learn on how to accept args still They can be important later on.., but I would get this running right first and then try to add args... No reason to try too much at once
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

  11. #71
    Join Date
    May 2010
    Posts
    76

    Re: Can someone please run this in XP

    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?

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

  12. #72
    Join Date
    Jun 2008
    Posts
    592

    Re: Can someone please run this in XP

    if(argc = 2 ) is wrong. you don't assign a value to argc use the other operator meant for comparison
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

  13. #73
    Join Date
    May 2010
    Posts
    76

    Re: Can someone please run this in XP

    thanks....changed to argc==2 and input still works fine but output has a blank file....could my palindrome not be correct?

  14. #74
    Join Date
    Jun 2008
    Posts
    592

    Re: Can someone please run this in XP

    if(argc == 2 )
    cout << "Opening for write: " << argv[2] << endl;

    Consider that a buffer overrun

    Again... debugging will tell you this... or it should. You are trying to modify code that was good when you simply need to learn how to pass args right....
    Last edited by Joeman; May 28th, 2011 at 01:13 PM.
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

  15. #75
    Join Date
    May 2010
    Posts
    76

    Re: Can someone please run this in XP

    No I dont see it anywhere when I put it == 2 or if I put argc > 1

Page 5 of 6 FirstFirst ... 23456 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