473,395 Members | 1,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

binary system input and output

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
1 3563
JosAH
11,448 Expert 8TB
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

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

Similar topics

5
by: Joseph | last post by:
Hi all, I was wondering if there is any lib or function could do the following things: input: a char output: 8 digits binary presentation for that given char Example:
103
by: Steven T. Hatton | last post by:
§27.4.2.1.4 Type ios_base::openmode Says this about the std::ios::binary openmode flag: *binary*: perform input and output in binary mode (as opposed to text mode) And that is basically _all_ it...
28
by: wwj | last post by:
void main() { char* p="Hello"; printf("%s",p); *p='w'; printf("%s",p); }
2
by: anirudhvr | last post by:
Hi, I needed a basic binary to ascii encoder, so I wrote this piece of code: /* Encoding algo: suppose 11111010 is the byte to be encoded. It is first broken up into two 4-bit parts (1111...
6
by: Fredrik | last post by:
How can I download a binary file (like .doc or .pdf) from http to my harddrive using the .net framework? WebRequest objRequest = System.Net.HttpWebRequest.Create(url); objResponse =...
9
by: Hemang Shah | last post by:
Hello fellow Coders! ok, I"m trying to write a very simple application in C#. (Yes its my first program) What I want to do is : 1) Open a binary file 2) Search this file for a particular...
12
by: Adam J. Schaff | last post by:
I am writing a quick program to edit a binary file that contains file paths (amongst other things). If I look at the files in notepad, they look like: ...
8
by: Mark | last post by:
Hello. I am attempting to write binary data from a file to an OLE Object field, and then write the file back out from the database. I am reading and writing the files in binary mode, and using...
1
by: Michael | last post by:
I have a solution for this, but it feels wrong. If anyone could offer a better one, I'm all ears. (Or technically, eyes.) Basically, I have a bunch of classes. For concreteness, one is a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.