By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,934 Members | 1,677 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,934 IT Pros & Developers. It's quick & easy.

binary system input and output

P: 5
i have to read the binary data of a file, then encrypt them according to a supplied algorithm...and then the obtained output has to be written to an output file...everything works ok, and there are no errors...
but when i print out the binary data from the input file it seems to be the same whtever the file, and then after encrypting and wirting the data to the output file, the output file has a size of 0KB and nothing in it..pls help as soon as possible as i hav to submit assignment asap!!!!!
here is my code
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<ctype.h>
  3. #include<string.h>
  4. #include<stdlib.h>
  5.  
  6. FILE *finput, *foutput;
  7.  
  8. int main()
  9. {
  10.     char input_file[255],output_file[255],keyword[1000];
  11.     int seed_val=0,help=0;
  12.     int i = 0,input_char[] = {0},s = 0,rand_char = 0,output_char[] = {0},key_length,encrypt_char,n;
  13.  
  14.  
  15.     printf("Enter the input file:"); /*Prompt user for input file name and location*/
  16.     fgets(input_file,255,stdin);
  17.     input_file[strlen(input_file)-1] = '\0';
  18.  
  19.     for (; i < strlen(input_file); i++)
  20.         printf("%c", input_file[i]);
  21.  
  22.     finput = fopen(input_file,"rb");
  23.  
  24.     if (finput == NULL)     /*Check whether input file exists, if not keep on asking for input file, until user enters correct input file*/
  25.     {
  26.         printf("\nError opening input file,please enter another file:");
  27.         while (finput == NULL)
  28.         {
  29.             printf("\nError opening input file,please enter another file:");
  30.             printf("\nEnter the input file:");
  31.             fgets(input_file,255,stdin);
  32.             input_file[strlen(input_file)-1] = '\0';
  33.  
  34.             for (; i < strlen(input_file); i++)
  35.                 printf("%c", input_file[i]);
  36.  
  37.             finput = fopen(input_file,"rb");
  38.         }
  39.         printf("Input file opened successfully\n");
  40.     }
  41.     else printf("Input file opened successfully\n");
  42.  
  43.     printf("\nEnter the output file:");                                 /*Prompt user for output file name*/
  44.     fgets(output_file,30,stdin);
  45.     output_file[strlen(output_file)-1] = '\0';
  46.  
  47.     i = 0;
  48.  
  49.     for (; i < strlen(output_file); i++)
  50.         printf("%c", output_file[i]);
  51.  
  52.     foutput = fopen(output_file,"rb");                                    /*Open output file as an input file in order to check*/
  53.  
  54.     if (foutput != NULL)
  55.     {
  56.         printf("\nOutput file already exists.");
  57.         fclose(foutput);        
  58.     }
  59.     else printf("\nOutput file does not exist");
  60.  
  61.     printf("\nEnter the keyword:");
  62.     fgets(keyword,30,stdin);
  63.     keyword[strlen(keyword)-1] = '\0';
  64.  
  65.     for (; i < strlen(keyword); i++)
  66.         printf("\nThe keyword is %c", keyword[i]);
  67.  
  68.     printf("%s",keyword);
  69.  
  70.     printf("\nEnter seed generator value:");
  71.     scanf_s("%d",&seed_val);
  72.     printf("%d\n",seed_val);
  73.  
  74.     /* Read Binary Data from input file*/
  75.  
  76.  
  77.     fread(input_char,sizeof(finput),100000000,finput);
  78.     printf("%d\n",input_char);
  79.  
  80.     /*Seed Random number generator and generate random numbers*/
  81.  
  82.     if (seed_val == NULL)
  83.     {
  84.         srand(s);
  85.     }
  86.     else (srand(seed_val));
  87.  
  88.     key_length == strlen(keyword);  /*Find length of keyword*/
  89.  
  90.     for (i = 0; i < strlen(input_char);)
  91.     {
  92.         encrypt_char = input_char[i];
  93.         rand_char = rand();
  94.         i = (i + 1) % key_length;
  95.         n = 0;
  96.         output_char == encrypt_char ^ rand_char ^ keyword[i];
  97.         fwrite(&output_char,sizeof(output_char),10000,foutput);
  98.     }
  99.  
  100.     foutput = fopen(output_file,"wb");
  101.  
  102.     fclose(foutput);
  103.     fclose(finput);
  104.  
  105.  
  106.  
  107.  
  108. }
  109.  
Sep 10 '07 #1
Share this Question
Share on Google+
1 Reply


Expert 10K+
P: 11,448
You're trying to open your output file using the "rb" flags; that doesn't open a
file for writing.

kind regards,

Jos
Sep 10 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.