How to Ensure a C++ Program Reads Exactly 52 Mappings from a File?

In summary, to ensure that the file contains 52 by 2 elements, you can use the "eof" function in a while loop to keep reading from the file until the end is reached. You can also use a counter variable to keep track of the number of elements read and exit the program if it is not equal to 52.
  • #1
lee_sarah76
18
0

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!
 
Physics news on Phys.org
  • #2
Thank you!

Hello,

It seems like you have made good progress on your program so far. To address your specific problem, you can use the "eof" function to check if the file has reached the end. This function returns true if the end of the file has been reached and false if it has not. You can use this in a while loop to continue reading from the file until the end is reached.

For example:

while(!keyFile.eof()) {
//read from file
}

This way, your loop will continue until the end of the file is reached. You can also use this in conjunction with a counter variable to keep track of the number of elements read from the file and exit the program if it is not equal to 52.

I hope this helps and good luck with your program!
 

Related to How to Ensure a C++ Program Reads Exactly 52 Mappings from a File?

1. How do I read a file in C++?

In C++, the standard way to read a file is by using the ifstream class from the fstream library. First, you need to open the file using the open() function, then use the getline() function to read the contents of the file line by line.

2. How do I check if a file exists in C++?

You can check if a file exists in C++ by using the ifstream class' is_open() function. This function returns a boolean value of true if the file exists, and false if it does not.

3. How do I read a specific line from a file in C++?

To read a specific line from a file in C++, you can use the getline() function and specify the line number as the second parameter. For example, getline(myFile, line, 5) would read the 5th line of the file into the line variable.

4. How do I read a file character by character in C++?

C++ provides the get() function to read a single character from a file. This function returns an integer value, so you need to cast it to a character to use it. You can also use the eof() function to check if the end of the file has been reached.

5. How do I read a binary file in C++?

To read a binary file in C++, you can use the read() function from the ifstream class. This function takes in a buffer and the number of bytes to be read from the file. It is important to note that binary files should be opened in binary mode by specifying the ios::binary flag in the open() function.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
15
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
2K
  • Programming and Computer Science
3
Replies
75
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Programming and Computer Science
Replies
8
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
Back
Top