Recommended Posts

Don't really have a question yet, I'm just sure I will have one eventually. Writing out what I have to do so I can save myself that step when I actually have a question...

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

/* 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;
}

Link to post
Share on other sites

Ok, now that I'm working on this and not finding answers in my notes, I'm becoming confused. I'm writing 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)

// 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;
}

Link to post
Share on other sites

This will be easier with line numbers.

	// snip
12 int main()
13 {
14 char read_filename[65], write_filename[65];
15 FILE *read_file_pointer, *write_file_pointer;
16 int input_value;

17 /* Get filenames from user */
18 printf("Enter the read File Name ");
19 gets(read_filename);

20 printf("Enter the write File Name ");
21 gets(write_filename);


22 /* Open files in binary mode */
23 if(read_filename[0] !=0) read_file_pointer = fopen(read_filename, "rb");
24 else read_file_pointer = NULL;

25 if(read_file_pointer == NULL) exit(1);

26 if(write_filename[0] !=0) write_file_pointer = fopen(write_filename, "wb");
27 else write_file_pointer = NULL;

28 if(write_file_pointer == NULL) exit(1);
//snip

He's describing lines 20, 21, and 26-28. For decode.cpp it's lines 18, 19, and 23-25.

Link to post
Share on other sites

Ok, I've hacked through it a little bit, have this for encode.cpp:

// 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, C;
FILE *myfile;


// Get filenames from user
printf("Enter the read File Name ");
gets(read_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);

myfile = fopen("odd.txt", "wt");
myfile = fopen("even.txt", "wt");

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;
}

and now I don't think I know how to do step 4: "Declare an integer counter variable and set to 0". Not sure if it's the wording or if I just missed that part in class

Link to post
Share on other sites

Ok, figured out the counter shiznit, the way he put it confused me. "Integer counter variable" confused me, if he would have said an "int counter" I would have known what to do. Did that step, and also added in the date and time, and this is what I have up to now:

// stdlib required for the exit() function
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string.h>


int main()
{
long int sec_since_1970;
char date_time_string[26];


char read_filename[65], write_filename[65];
FILE *read_file_pointer, *write_file_pointer;
int input_value, C;
FILE *myfile;

// This function will produce the # of seconds since 1970
time(&sec_since_1970);

// Copy the date & time into a manageable array
strcpy(date_time_string, ctime(&sec_since_1970));

// Set last position to null.
// Some versions of this function incorporate a line feed at this point
date_time_string[24]=0;

cout << "\n\nDate time string = " << date_time_string;


// Get filenames from user
printf("Enter the read File Name ");
gets(read_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);

myfile = fopen("odd.txt", "wt");
myfile = fopen("even.txt", "wt");

int counter = 0;

do
{
input_value = fgetc(read_file_pointer);
++counter;
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;
}

For the last step,

(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).)

a conditional statement, would that be like an if-then statement? a do-while statement? something else?

And for fputc() I understand kinda how it works. Take "fputc('a', stdout) for example writes a character 'a' to the standard output, but for putting the odd counter to odd.txt and evens to even.txt, what would I put in for the written character? The place to put it would be "fputc('?', odd.txt) right?

Link to post
Share on other sites

I am now also working on decode.cpp. I believe I have everything right up to the last step. I have some pseudocode written out, but don't know how to code my pseudocode...

My pseudocode is for the do while statement at the end. This is what I have:

do
Read from the odd file
if the end of file has not been read for the odd file, write into the write file
Read from the even file
If the end of file has not been read for the even file, write into the write file
While(you have not read past the end of file for odd and even)

This is the code I have written out so far:

// stdlib required for the exit() function
#include <stdlib.h>
#include <stdio.h>
#include <iostream.h>
#include <time.h>
#include <string.h>


int main()
{
long int sec_since_1970;
char date_time_string[26];

char write_filename[65];
FILE *read_file_pointer, *write_file_pointer;
int input_value;
FILE *myfile;

// This function will produce the # of seconds since 1970
time(&sec_since_1970);

// Copy the date & time into a manageable array
strcpy(date_time_string, ctime(&sec_since_1970));

// Set last position to null.
// Some versions of this function incorporate a line feed at this point
date_time_string[24]=0;

cout << "\n\nDate time string = " << date_time_string;


// Get filenames from user
cout << "\n\nEnter the write File Name ";
cin >> write_filename;


// Open files in binary mode
myfile = fopen("odd.txt", "rt");
myfile = fopen("even.txt", "rt");

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;
}

Link to post
Share on other sites
a conditional statement, would that be like an if-then statement? a do-while statement? something else?

If-else. Or switch-case, ?:, && and ||....

Take "fputc('a', stdout) for example writes a character 'a' to the standard output, but for putting the odd counter to odd.txt and evens to even.txt, what would I put in for the written character? The place to put it would be "fputc('?', odd.txt) right?

The last character you read from the input file and the output filestream, respectively. You can reuse this

  do
{
input_value = fgetc(read_file_pointer);
if(!feof(read_file_pointer))
{
fputc(input_value, write_file_pointer);
}
} while(!feof(read_file_pointer));

but with the extra code to select between the two output streams.

Edited by jcl
Link to post
Share on other sites
  • 1 month later...

Sorry about not responding sooner, have either been busy or forgeting about this, but thank you all for the help! Thanks to everyone who helped with any of the questions that I had, it was much appreciated. Got the program working, don't know how I did though, as it was due on the last day of class. Ended up getting an A- overall in the grade though. Wouldn't have been able to do it (well without pulling out patches of hair out of frustration) without you guys.

Thanks again!

-Aron

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...