Now I need to write the decryption part, and read each line of the file that is written by the first program. I want the program to decrypt each integer and then print them to another file.
How do I read just one character on a line so that I can put it into an array? Also, how do you make sure you get all the characters for each line, but only one line at a time? I know there are other (probably better?) ways of doing this, such as read the whole line and then break up the integer into parts in order to work with each part. But I prefer using an array.
This is the function that I think needs changing.
//read nums from file into array
void read(int nums[], int SIZE, ifstream& inData) {
int nextNum;
//read first line from the file //how do I read one char at a time from file?
inData >> nextNum;
//show error if file not found
if(!inData)
{
cout<< "Can't open input file.\n";
getchar();
}
//while there are more lines to read from file
int i = 0; //counter
while(inData){
nums[i] = nextNum;
//read another line...this will put each line into an element in the array
//so the whole file is read into one array instead of each line in one array
//with each char in the line put into one element of the array
inData >> nextNum;
i++;
}
}
This program does work in that it will read the file and swap the values that I told it to swap. They're just not the right values.
Here is the entire 2nd program (unfinished, of course)
#include<iostream>
#include<fstream> //for file reading & writing
using namespace std;
//read nums from file into array
void read(int nums[], int SIZE, ifstream& inData) {
int nextNum;
//read first line from the file //how do I read one char at a time from file?
inData >> nextNum;
//show error if file not found
if(!inData)
{
cout<< "Can't open input file.\n";
getchar();
}
//while there are more lines to read from file
int i = 0; //counter
while(inData){
nums[i] = nextNum;
//read another line...this will put each line into an element in the array
//so the whole file is read into one array instead of each line in one array
//with each char in the line put into one element of the array
inData >> nextNum;
i++;
}
}
//swap the first digit with the second, swap the thrid digit with
//the fourth
void swap(int nums[]){
int temp = nums[0];
//swap first two numbers
nums[0] = nums[1];
nums[1] = temp;
//swap second two numbers
temp = nums[2];
nums[2] = nums[3];
nums[3] = temp;
}
//print the decrypted integer to the screen and to a file
void print(int nums[], int SIZE, ofstream& outData){
//print to screen and write to file.
for(int i = 0; i<SIZE; i++){
cout<<nums[i];
outData << nums[i];
//start next number on a new line
cout << endl;
outData << endl;
}
}
int main() {
const int SIZE = 7;
int nums[SIZE];
//open file to read
ifstream inData;
inData.open("EncFile.txt");
//open file to write
ofstream outData;
outData.open("DecFile.txt");
read(nums, SIZE, inData);
//replace(nums, SIZE);
swap(nums);
print(nums, SIZE, outData);
inData.close();
outData.close();
}
//Write a separate program(a second program, gosh) that inputs an encrypted
//four-digit integer and decrypts it to form the original number.

New Topic/Question
Reply




MultiQuote





|