Hi, I'm working on a program, and I'm lost already. Been working on this for a while surprisingly, just have been looking through my notes and can't find stuff on this. This is what I have to do:
Encode.cpp
Your company is concerned about the security of data shipped through the mail or the Internet. It is your duty to devise a program that will read a file and creat two encoded files. Read the file to be encrypted and alternately place the character read into one of two files opened, one called odd.txt and the other called even.txt.
Decode.cpp
You are also requested to write a program that will recreate the original file. Once again, use Chario.c as an example. Alternately read from the odd and even files, writing the character read into the write file.
You muse use // for comments and cout, cin for screen output and keyboard input. Also, display the current date and time in the upper left corner of the screens.
General Hints:
1. Find Chario.c from the C FILE INPUT/OUTPUT OPERATIONS handout (I'll supply for the forum).
2. Key it in as Chario.cpp.
3. Remove the line that changes the case.
4. Test the program to see if it copies a file.
5. Change comments from /* to //
6. Test again.
7. Copy Chario.cpp to Encode.cpp and Decode.cpp
Encode.cpp hints
1. Leave the read-file prompt, filename input, and fopen() as is.
2. Delete the write-file prompt, filename input, and fopen().
3. Open the files odd.txt and even.txt for writing.
4. Declare an integer counter variable and set to 0.
5. After the fgetc() line, increment the counter.
6. Remove the fputc() line and replace it with a conditional statement that fputc()s to the odd.txt file when counter is odd and to the even.txt file otherwise. (Remember modulus).
Decode.cpp hints
1. Delete the read file user prompt and file open and replace the opening of the files odd.txt and even.txt
2. Leave the write file prompt and open as is. Allow the user to determine the name of the reconstruced file.
3. Modify the do-loop as follows:
a. Perform the loop as long as the end of file has not been reached in both files.
b. Input a character from the odd file and write it if eof has not been reached.
c. Input a character from the even file and write it eof has not been reached.
Here's chario.c
Code:
/* chario.c by Teacher 11/26/2002
*
* This program demonstrates the use of single character
* file input/output functions to read a file, change lower
* case characters to upper and write them back. Be careful,
* the integrity of most files will be destroyed by this operation.
* File names are input from the keyboard.
*/
/* stdlib required for the exit() function */
#include <stdlib.h>
#include <stdio.h>
int main()
{
char read_filename[65], write_filename[65];
FILE *read_file_pointer, *write_file_pointer;
int input_value;
/* Get filenames from user */
printf("Enter the read File Name ");
gets(read_filename);
printf("Enter the write File Name ");
gets(write_filename);
/* Open files in binary mode */
if(read_filename[0] !=0) read_file_pointer = fopen(read_filename, "rb");
else read_file_pointer = NULL;
if(read_file_pointer == NULL) exit(1);
if(write_filename[0] !=0) write_file_pointer = fopen(write_filename, "wb");
else write_file_pointer = NULL;
if(write_file_pointer == NULL) exit(1);
do
{
input_value = fgetc(read_file_pointer);
if(!feof(read_file_pointer))
{
if(input_value >= 'a' && input_value <= 'z') input_value -=32;
fputc(input_value, write_file_pointer);
}
}
while(!feof(read_file_pointer));
/* while(feof(read_file_pointer)==0) could also be used */
fclose(read_file_pointer);
fclose(write_file_pointer);
return 0;
}
Right now I'm working on encode.cpp, under his hints he has "Delete the write-file prompt, filename input, and fopen()," I'm assuming it's these lines,
"if(write_filename[0] !=0) write_file_pointer = fopen(write_filename, "wb");
else write_file_pointer = NULL;"
but I'm not sure if it's exactly these two lines, more or less.
(Already removed the line that changes case, this is what I have written so far, not very far as you can see)
Code:
// Encode.cpp by Aron 12/05/2002
// This program demonstrates the use of single character
// file input/output functions to read a file, change lower
// case characters to upper and write them back. Be careful,
// the integrity of most files will be destroyed by this operation.
// File names are input from the keyboard.
// stdlib required for the exit() function
#include <stdlib.h>
#include <stdio.h>
int main()
{
char read_filename[65], write_filename[65];
FILE *read_file_pointer, *write_file_pointer;
int input_value;
// Get filenames from user
printf("Enter the read File Name ");
gets(read_filename);
printf("Enter the write File Name ");
gets(write_filename);
// Open files in binary mode
if(read_filename[0] !=0) read_file_pointer = fopen(read_filename, "rb");
else read_file_pointer = NULL;
if(read_file_pointer == NULL) exit(1);
if(write_filename[0] !=0) write_file_pointer = fopen(write_filename, "wb");
else write_file_pointer = NULL;
if(write_file_pointer == NULL) exit(1);
do
{
input_value = fgetc(read_file_pointer);
if(!feof(read_file_pointer))
{
fputc(input_value, write_file_pointer);
}
}
while(!feof(read_file_pointer));
// while(feof(read_file_pointer)==0) could also be used
fclose(read_file_pointer);
fclose(write_file_pointer);
return 0;
}