C++ File Reading Question

  • Comp Sci
  • Thread starter lee_sarah76
  • Start date
  • #1

Homework Statement



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.


Homework Equations



N/A

The Attempt at a Solution


Code:
#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!
 

Answers and Replies

Related Threads on C++ File Reading Question

  • Last Post
Replies
14
Views
1K
Replies
2
Views
2K
  • Last Post
Replies
2
Views
1K
Replies
12
Views
24K
  • Last Post
Replies
5
Views
6K
Replies
19
Views
2K
Replies
6
Views
19K
  • Last Post
Replies
3
Views
826
Replies
10
Views
7K
Top