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

    finishing palindrome program

    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...

    [CODE]
    #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;
    }

    }
    }
    [CODE]

    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.

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

    Re: finishing palindrome program

    Quote Originally Posted by jimJohnson123 View Post
    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...
    1) Please correct your code tags.

    2) Have you debugged your program using the debugger that comes with your compiler?

    3) Why is the program one gigantic main() function? Why isn't the program broken down into several functions? Having one huge function makes the problem more difficult to debug. The first thing you should have written, regardless of all those instructions is this:
    Code:
    bool isPalindrome(const char *word)            // or
    bool isPalindrome(const std::string& word);
    Then you write the function to determine if the passed in string is a palindrome. All of that stuff with file reading is a separate issue all designed to waste your time, instead of completing the most important part of the assignment.

    4) What is with all of this file mapping code? What does that have to do with palindromes? (see comment 3).

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; April 6th, 2011 at 10:13 PM.

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