1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ File Reading Question

  1. Nov 3, 2013 #1
    1. The problem statement, all variables and given/known data

    This program must read two input files, one is a key that specifies the conversion of characters
    the other is a text message that will be encoded based on the mapping specified in the key
    files. The program must use the key mappings to both encode and decode the message file.
    The user can choose between two options, encoding and decoding.
    When encoding the encoded file name must bethe same as the input message plus a ".enc" extension.
    When decoding the decoded file name must be the same as the input message plus a ".dec" extension.

    Above is the whole Problem Statement. However, my specific problem is that after the file that is being read is inputting values into two given vectors, I need to make sure that the file contained 52 by 2 elements or else the program must exit.


    2. Relevant equations

    N/A

    3. The attempt at a solution
    Code (Text):
    #include <iostream>
    #include <fstream>
    #include <stdlib.h>

    int main()
    {

        /*
         Three file names are needed.
         One will contain the key that specifies the character mappings.
         One will hold the name for the message.
         One will hold the name for the encoded message.
         The string 'extension'.
        */
       
        std::string keyFileName, messageFileName, encodedMessageFileName, extension;
       
        std::ifstream keyFile;
        std::ifstream messageFile;
        std::ofstream encodedMessageFile;
       
        char choice = 'E';
        do
        {
            std::cout << "\nWhat would you like to do? Encode/Decode?";
            std::cout << "\nPlease key in 'E' or 'D': ";
            std::cin >> choice;
        } while( choice != 'E' && choice != 'D');
       
        do
        {
            std::cout << "\nKey in a valid key file name: ";
            std::cin >> keyFileName;
           
            keyFile.open(keyFileName.c_str());
            if( !keyFile.is_open())
            {
                std::cout <<"\nFailed to open." << keyFileName;
            }
        }while (! keyFile.good() );
       
       
        do
        {
            std::cout << "\nKey in a message name: ";
            std::cin >> messageFileName;
           
            messageFile.open(messageFileName.c_str());
            if( !messageFile.is_open())
            {
                std::cout <<"\nFailed to open." << messageFileName;
            }
        }while (! messageFile.good() );
       
        if(choice == 'E')
        {
            extension = ".enc";
        }
        else
        {
            extension = ".dec";
        }
        //The two strings are now concatenated.
        encodedMessageFileName = messageFileName + extension;
       
       
        char from[52];
        char to[52];
       
       
        if(choice == 'E')
        {
            for(int i = 0; i < 52; i++)
            {
                keyFile >> from[i];
                keyFile >> to[i];
               
                {
                    //[B]In here is where the loop should go, and I'm also fairly positive it should be the eof //function that I am calling but I'm not sure how to go about that exactly. [/B]
                   
                   
                    exit(1);
                }
            }
        }
        else
       {
            for(int i = 0; i < 52; i++)
            {
                keyFile >> to[i];
                keyFile >> from[i];
               
               {
                    //[B]In here is where the loop should go, and I'm also fairly positive it should be the eof //function that I am calling but I'm not sure how to go about that exactly. [/B]
                   
                   
                    exit(1);
                }
           }
       }

    keyFile.close();
    [B]And here is where the 1-1 mapping goes I believe and I think it should be a for loop but I'm not exactly sure how to do that either. [/B]
    }


     
    I'm not looking for someone to give me the answers, but if someone could point me in the right direction that would be splendid!
     
  2. jcsd
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Can you offer guidance or do you also need help?
Draft saved Draft deleted