0

Currently, this is the code that I have created but I can't seem to find the problem behind it. I am writing a program that encrypts

int countWords(FILE *f){
   int count = 0;
   char ch;
   while ((ch = fgetc(f)) != EOF){
       if (ch == '\n')
           count++;
   }
   return count;
}

int main ( int argc, char *argv[] ){
    //int min > 0; 
    //int max >= int min; 
    time_t current_time;
    char* c_time_string;

    current_time = time(NULL);

    /* Convert to local time format. */
    c_time_string = ctime(&current_time);

    printf("Program started %s\n", c_time_string);


    if ( argc != 2 ){ 
        printf( "usage: %s filename\n", argv[0] );
    }else {
        int wordCount = 0;
        FILE *file = fopen( argv[1], "r" );
        //wordCount += countWords(file);
        //printf("Total number words processed => %d\n", wordCount);

    if ( file == 0 ){
        printf( "Could not open file.\nProgram halted. Please verify the file path and try again.\n" );
    }else {
            int x;
            while  ( ( x = fgetc( file ) ) != EOF ){
                printf("%c", x );
            }
            fclose( file );
        }


                /// CRYPT ///

         char * plaintext = argv[1] ;
         char * hash_type_1 = "$1$";  // type 1 implies md5 (number of iteration is only 1000 rounds)
         char * hash_type_2 = "$6$";  // type 2 implies sha-512 (default value as in yr 2017, number of iteration is minimum 10,000 rounds )
         char * salt_1 ="$";           // a simplified 0 length salt.
         char * salt_2 ="ABCD1234$";   // a normal 8 character salt.
         char * result;
         char encyption_scheme[20]; // 20 is more than enough.

         // prepare the first call using md5 and empty salt

         strcpy(encyption_scheme,hash_type_1);
         strcat(encyption_scheme,salt_1);
         result = crypt(plaintext,encyption_scheme);
         printf("MD5: %s\n",result);
         // prepare the second call using sha-512 and a 8-char salt
         strcpy(encyption_scheme,hash_type_2);
         strcat(encyption_scheme,salt_2);
         result = crypt(plaintext,encyption_scheme);
         printf("Sha512: %s\n",result);
         printf("Program ended %s\n", c_time_string);

        //transfer the output to mytab2411.txt//
        result = freopen("mytab2411.txt", "w+", stdout);

    }
            return 0; 
}

I need some help with my coding. I am trying to code a program which is able to transfer the output of my program to an empty file. Please help me find my errors.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • 6
    I think you should call `freopen` before you print to stdout. – Pablo Dec 28 '17 at 05:40
  • Sorry but what do you mean by that? Can you give me an example of it? – JokerIsMyFriend Dec 28 '17 at 05:50
  • 1
    Not *the* error, but in countWords `char ch; ch = fgetc(f)` is the wrong way to do it: https://stackoverflow.com/questions/35356322/difference-between-int-and-char-in-getchar-fgetc-and-putchar-fputc. `fgetc()` returns an `int`, for a reason. – Ilja Everilä Dec 28 '17 at 06:15
  • 1
    When you call `freopen`, it arranges that later calls which print to `stdout` go to your file instead, but it doesn't magically redirect the `printf` calls that you had already made. We're suggesting you move the call to `freopen` earlier in your program, before the calls to `printf` that you want to affect. – Steve Summit Dec 28 '17 at 07:15
  • Ahh i see, thanks for the help and explanation! – JokerIsMyFriend Dec 28 '17 at 14:19
  • the posted code does not compile! This is because the posted code is missing the needed `#include` statements for the needed header files. This means the question is 'off topic' because when the question is about a 'runtime' problem, the posted code must conform to the requirements of: [mcve] – user3629249 Dec 28 '17 at 14:31
  • @Yunnosch there is already an answer, I don't think I need to post code for my obvious comment. – Pablo Dec 28 '17 at 18:09
  • @Pablo True, I got what I wanted. – Yunnosch Dec 28 '17 at 18:58

1 Answers1

0

regarding:

//transfer the output to mytab2411.txt//
result = freopen("mytab2411.txt", "w+", stdout);

The modification to the I/O only applies to output AFTER the call is made.

Suggest placing this statement at the beginning of the 'encrypt' section of the code.

user3629249
  • 16,402
  • 1
  • 16
  • 17