Thread: memory mapping

  1. #16
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Don't type anything. Type the name of the program, then take your hands off the keyboard. Sneak one finger back on to press the enter key, but resist the urge to type a bunch of things before pressing enter.

  2. #17
    Registered User
    Join Date
    May 2011
    Posts
    15
    Ok thanks I tried that but it does try to open but then crashes

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, I suppose:
    Code:
    cout << "opening for read: " << argv[1] << endl;
    If you don't provide arguments, argv[1] doesn't exist.

  4. #19
    Registered User
    Join Date
    May 2011
    Posts
    15
    so I would just replace all argv with inFilename and outFilename?

  5. #20
    Registered User
    Join Date
    May 2011
    Posts
    15
    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;
    }

  6. #21
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Do you ever even attempt to write something to your output file?

  7. #22
    Registered User
    Join Date
    May 2011
    Posts
    15
    This is what I did for any letter in the input file I want to cout to the output file. I am sure it is something very simple on what I am doing wrong

    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);
    
    		//declare string variable
    		string txt;
    		for (int j=0; j<int(txt.size()); ++j)	
    		{
    			 (txt[j]=='a' || txt[j]=='b' || txt[j]=='c' || txt[j]=='d' || txt[j]=='e' || txt[j]=='f'
    				|| txt[j]=='g' || txt[j]=='h' || txt[j]=='i' || txt[j]=='j' || txt[j]=='k' || txt[j]=='l'
    				|| txt[j]=='m' || txt[j]=='n' || txt[j]=='o' || txt[j]=='p' || txt[j]=='q' || txt[j]=='r'
    				|| txt[j]=='s' || txt[j]=='t' || txt[j]=='u' || txt[j]=='v' || txt[j]=='w' || txt[j]=='x' 
    				|| txt[j]=='y' || txt[j]=='z');
    		}
    
            //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);
    		
    		
    		
    		//show output
    		//cout<<txt[j];
    		cout << txt;
    
            //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;
    }

  8. #23
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    for (int j=0; j<int(txt.size()); ++j)	
    		{
    			 (txt[j]=='a' || txt[j]=='b' || txt[j]=='c' || txt[j]=='d' || txt[j]=='e' || txt[j]=='f'
    				|| txt[j]=='g' || txt[j]=='h' || txt[j]=='i' || txt[j]=='j' || txt[j]=='k' || txt[j]=='l'
    				|| txt[j]=='m' || txt[j]=='n' || txt[j]=='o' || txt[j]=='p' || txt[j]=='q' || txt[j]=='r'
    				|| txt[j]=='s' || txt[j]=='t' || txt[j]=='u' || txt[j]=='v' || txt[j]=='w' || txt[j]=='x' 
    				|| txt[j]=='y' || txt[j]=='z');
    		}
    Stop. Sit down, and ask yourself how you could come up with such a thing as this. I think you need a nap, or maybe a walk around the park.

    EDIT: And I don't know why we're having to have this discussion, but: argv[argc] is ALWAYS an invalid thing. If you're checking to make sure that argc is 2, you can't later try and use argv[2] because it quite simply won't exist.
    Last edited by tabstop; 05-28-2011 at 01:11 PM.

  9. #24
    Registered User
    Join Date
    May 2011
    Posts
    15
    sorry man i am just trying to figure out why this is doing what it is doin and lost

  10. #25
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It is not doing anything, because you have literally not written a useful line of code anywhere in this program. You open two files. You close them. That is all. You never attempt to read from the files, you never attempt to process the input in any way, nor do you make any attempt at writing to a file. Your program is not sentient, and will not just decide to do things all on its own. You need to decide what you want to have the program do, and then make it do that.

  11. #26
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    Quote Originally Posted by tabstop View Post
    It is not doing anything, because you have literally not written a useful line of code anywhere in this program.
    I think he should just go to his teacher and say I can't do it. Looking at Google he's had this assignment for at least three months and the code is hardly any further along to its goal now than it was then, and that includes 3 forums worth of people essentially writing it for him. I don't know where it is, but somewhere there's got to be a line between continued perseverance and just plain time wasting.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. mapping in C++
    By jack_carver in forum C++ Programming
    Replies: 5
    Last Post: 04-17-2009, 10:00 AM
  2. Height Mapping
    By IdioticCreation in forum Game Programming
    Replies: 17
    Last Post: 10-15-2007, 06:50 PM
  3. Palindrome Program using Memory mapping
    By rrsanch in forum C++ Programming
    Replies: 1
    Last Post: 09-24-2004, 11:49 PM
  4. Texture mapping?
    By rpearson in forum Game Programming
    Replies: 7
    Last Post: 03-04-2004, 10:35 PM
  5. Mapping
    By Scorpion-ice in forum Game Programming
    Replies: 0
    Last Post: 07-02-2002, 01:32 PM