Thread: Last program!!

  1. #1
    Pumkin King
    Join Date
    Sep 2005
    Location
    Michigan, USA
    Posts
    12

    Last program!!

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

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    you are suppose to post this code in C board not here

    ssharish2005

  3. #3
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by ssharish2005
    you are suppose to post this code in C board not here

    ssharish2005
    Quote Originally Posted by buckwheat88
    You muse use // for comments and cout, cin for screen output and keyboard input.
    which part of C are these?

    to the OP, deleting the file-write prompt means delete the part that relates to asking the user for the file name. I'll leave it to you to find that.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  4. #4
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    ........are you in Harry Brown's CIS 185 class?....
    you are. you have to be. i took his CIS 185 class last winter and we had to write this program, and the chario.c file you posted is exactly the same as the one i have (still, yes) and you just have to be. thats crazy.
    i work in the pc lab mon & wed, 7:30 - 10 & 4 - 7:30 (respectively).

    DUDE thats messed up.
    Registered Linux User #380033. Be counted: http://counter.li.org

  5. #5
    Pumkin King
    Join Date
    Sep 2005
    Location
    Michigan, USA
    Posts
    12
    Haha, yes I am in his class. Someone actually mentioned the fact that one of my questions that I was asking about was similar to http://www.angelfire.com/linux/willc...s/basicinput.c . Your name sounded familiar and I noticed "H. Brown" wrote part of your program so I knew you took the class at one point

  6. #6
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    Quote Originally Posted by buckwheat88
    Haha, yes I am in his class. Someone actually mentioned the fact that one of my questions that I was asking about was similar to http://www.angelfire.com/linux/willc...s/basicinput.c . Your name sounded familiar and I noticed "H. Brown" wrote part of your program so I knew you took the class at one point
    hahah nice..i have a friend in your class right now. his names chris
    Registered Linux User #380033. Be counted: http://counter.li.org

  7. #7
    Pumkin King
    Join Date
    Sep 2005
    Location
    Michigan, USA
    Posts
    12
    Chris... do you know if he sits in the back row by chance? I think the person I sit next to is named Chris, but I'm not sure

  8. #8
    Pumkin King
    Join Date
    Sep 2005
    Location
    Michigan, USA
    Posts
    12
    Ok, I've hacked through it a little bit, have this for encode.cpp:
    Code:
    // 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

  9. #9
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    lol well, then these questions must be posed: do you know how to declare an integer variable named counter? do you know how to initialize the variable to 0? lol b/c thats what its saying


    he sits in the 2nd row, he thinks 2nd from the right
    he thinks
    lol
    Last edited by willc0de4food; 12-12-2005 at 12:48 AM.
    Registered Linux User #380033. Be counted: http://counter.li.org

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > gets(write_filename);
    You've got to be worried when "teacher" puts crap like this in their example programs.

    Whilst their use of feof() is by and large OK, they sure make a meal of it.
    while ( (input_value = fgetc(read_file_pointer)) != EOF )
    works just as well, and the test is in only one postition in the file

  11. #11
    Pumkin King
    Join Date
    Sep 2005
    Location
    Michigan, USA
    Posts
    12
    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:
    Code:
    // 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?


    And yeah, I think I may know who he is, I sit straight back from him in the last row
    Last edited by buckwheat88; 12-12-2005 at 01:49 PM.

  12. #12
    Pumkin King
    Join Date
    Sep 2005
    Location
    Michigan, USA
    Posts
    12
    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:
    Code:
    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:
    Code:
    // 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;
      }

  13. #13
    Pumkin King
    Join Date
    Sep 2005
    Location
    Michigan, USA
    Posts
    12
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM