473,799 Members | 3,159 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

binary system input and output

5 New Member
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...everythi ng 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 3615
JosAH
11,448 Recognized Expert MVP
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
25671
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
48769
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 says about it. What the heck does the binary flag mean? -- If our hypothesis is about anything and not about some one or more particular things, then our deductions constitute mathematics. Thus mathematics may be defined as the subject in...
28
2809
by: wwj | last post by:
void main() { char* p="Hello"; printf("%s",p); *p='w'; printf("%s",p); }
2
3302
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 and 1010) This 4-bit quantity is then padded between a 01 and a 10, ie, 01xxxx10 will be written out to the output file.
6
32182
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 = objRequest.GetResponse(); BinaryReader bsRead = new BinaryReader(objResponse.GetResponseStream() FileStream fs = new FileStream("test.doc", FileMode.CreateNew); while(bsRead.PeekChar() != -1) { cbuf=bsRead.ReadByte();
9
11422
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 string. 3) Close the file
12
5934
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: <gibberish>file//g:\pathtofile1<gibberish>file//g:\pathtofile2<gibberish> etc. I want to remove the "g:\" from the file paths. I wrote a console app that successfully reads the file and writes a duplicate of it, but fails for some reason to do the "replacing" of the "g:\". The code...
8
8934
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 GetChunk and AppendChunk to read and write binary data from the OLE Object field. I am using VBA and DAO for this experiment. The OLE Object field is being used to store Long Binary data.
1
2602
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 Matrix class, but that's only one example, so please don't get too hung up on it. I need to output and input these classes. I'd like a nice, pretty, human readable output, something like: 1 2 3
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9538
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10470
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10214
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10023
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6803
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5459
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4135
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3751
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.