473,770 Members | 1,629 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Error reading from file

1 New Member
=========Progra m compiles on both windows and linux========== =

Greetings, I am currently working on an encrpytion program that will take a file and a password string and flip the bits based on the pass... ex:

pass: 01
11110000 - file
01010101 - pass (repeated until EOF)
10100101 - resulting file

decryption would be the exact same process, but i've made it slightly more complicated. first at the beginning of the file I have a 4 bit unsigned int that is the size of the file, then a 4 byte unsigned int that is the size of the string of its file name. Then it has the actual string for the file name (the ultimate goal is so that I can have multiple files in there similar to an archive)

The problem is when I try to decrypt my console window spazzes out at me displaying a bunch of random paths like application data and what appears to be an ASCII table...



Expand|Select|Wrap|Line Numbers
  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. string getFileName(string input)
  8. {
  9.     int pos = 0;
  10.     while (input.find_first_of("/\\", pos) != string::npos)
  11.     {
  12.         pos = input.find_first_of("/\\", pos) + 1;
  13.     }
  14.     int length = input.length() - pos;
  15.     return input.substr(pos, length);
  16. }
  17.  
  18. string getPath(string input)
  19. {
  20.     int pos = 0;
  21.     while (input.find_first_of("/\\", pos) != string::npos)
  22.     {
  23.         pos = input.find_first_of("/\\", pos) + 1;
  24.     }
  25.     return input.substr(0, pos);
  26. }
  27.  
  28. int main()
  29. {
  30.     //GET USER INPUT
  31.     string filestr, passstr, outputstr, action, filename;
  32.     cout << "Action (package, extract): ";
  33.     getline (cin, action);
  34.     if (action == "package")
  35.     {
  36.         cout << "Path to file: ";
  37.         getline (cin, filestr);
  38.         cout << "Pass: ";
  39.         getline (cin, passstr);
  40.         /*cout << "Output File: ";
  41.         getline (cin, outputstr);*/
  42.         filename = getFileName(filestr);
  43.         outputstr = filestr + ".craft";
  44.         char *file = new char[filestr.length()+1];
  45.         strcpy(file, filestr.c_str());
  46.         char *pass = new char[passstr.length()+1];
  47.         strcpy(pass, passstr.c_str());
  48.         char *output = new char[outputstr.length()+1];
  49.         strcpy(output, outputstr.c_str());
  50.         char buffer[sizeof(pass)];
  51.         ifstream myFile(file, ios::binary);
  52.         ofstream outFile(output, ios::binary);
  53.         FILE * stream = fopen( file, "r" );
  54.            fseek( stream, 0L, SEEK_END );
  55.            unsigned int endPos = ftell( stream );
  56.            fclose( stream );
  57.            //WRITE SIZE OF FILE
  58.            outFile.write((char *)(&endPos), 4);
  59.            //WRITE SIZE OF NAME
  60.            unsigned int sizeofname = filename.length();
  61.            outFile.write((char *)(&sizeofname), 4);
  62.  
  63.            //DEBUG
  64.            //cout << endPos << endl << sizeofname << endl << sizeof(unsigned int) << endl;
  65.            //WRITE NAME OF FILE
  66.            outFile.write((char *)(&filename), sizeofname);
  67.            //cout << filename << endl;
  68.         while (!myFile.eof())
  69.         {
  70.             //cout << "endPos: " << endPos << "Size of pass: " << sizeof(pass) << endl;
  71.             if (endPos < sizeof(pass))
  72.             {
  73.                 myFile.read (buffer, sizeof(pass));
  74.                 for (unsigned int x = 0; x < endPos; x++)
  75.                 {
  76.                     char write = buffer[x] ^ pass[x];
  77.                     outFile.write(&write, 1);
  78.                 }
  79.             } else {
  80.                 myFile.read (buffer, sizeof(pass));
  81.                 for (unsigned int x = 0; x < sizeof(pass); x++)
  82.                 {
  83.                     char write = buffer[x] ^ pass[x];
  84.                     outFile.write(&write, 1);
  85.                 }
  86.                 endPos -= sizeof(pass);
  87.             }
  88.         }
  89.         myFile.close();
  90.         outFile.close();
  91.     }
  92.     if (action == "extract")
  93.     {
  94.         cout << "Path to file: ";
  95.         getline (cin, filestr);
  96.         cout << "Pass: ";
  97.         getline (cin, passstr);
  98.  
  99.         char *file = new char[filestr.length()+1];
  100.         strcpy(file, filestr.c_str());
  101.         char *pass = new char[passstr.length()+1];
  102.         strcpy(pass, passstr.c_str());
  103.         char buffer[sizeof(pass)];
  104.         ifstream myFile(file, ios::binary);
  105.  
  106.  
  107.  
  108.         unsigned int sizeoffile, sizeofname;
  109.         string name = "";
  110.  
  111.         myFile.read ((char *)(&sizeoffile), 4);
  112.         myFile.read ((char *)(&sizeofname), 4);
  113.  
  114.         cout << sizeofname << endl;
  115.         cout << &sizeofname << endl;
  116.         myFile.read ((char *)(&name), sizeofname);
  117.         cout << sizeoffile << endl << sizeofname << endl << name << endl;
  118.         outputstr = "";
  119.         outputstr = getPath(filestr) + name;
  120.  
  121.         char *output = new char[outputstr.length()+1];
  122.         strcpy(output, outputstr.c_str());
  123.         cout << "Output: " << output << endl;
  124.         ofstream outFile(output, ios::binary);
  125.  
  126.         unsigned int pos = 0;
  127.         while (pos < sizeoffile)
  128.         {
  129.             //cout << "pos: " << pos << "sizeoffile: " << sizeoffile << endl;
  130.             unsigned int endPos = sizeoffile - pos;
  131.             if (endPos < sizeof(pass))
  132.             {
  133.                 myFile.read (buffer, sizeof(pass));
  134.                 for (unsigned int x = 0; x < endPos; x++)
  135.                 {
  136.                     char write = buffer[x] ^ pass[x];
  137.                     outFile.write(&write, 1);
  138.                 }
  139.                 pos += endPos;
  140.             } else {
  141.                 myFile.read (buffer, sizeof(pass));
  142.                 for (unsigned int x = 0; x < sizeof(pass); x++)
  143.                 {
  144.                     char write = buffer[x] ^ pass[x];
  145.                     outFile.write(&write, 1);
  146.                 }
  147.                 pos += sizeof(pass);
  148.             }
  149.         }
  150.         myFile.close();
  151.         outFile.close();
  152.     }
  153.     return 0;
  154. }
  155.  
  156.  
Feb 28 '08 #1
0 1703

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

Similar topics

20
7910
by: TTroy | last post by:
Hello, I have found some peculiar behaviour in the fgets runtime library function for my compiler/OS/platform (Dev C++/XP/P4) - making a C console program (which runs in a CMD.exe shell). The standard says about fgets: synopsis #include <stdio.h> char *fgets(char *s, int n, FILE *stream);
2
4069
by: GB | last post by:
Hi Everybody! I have 2 different processes/application. One is writing to a file and another is reading from it. For some reason the code doesnt seems to work and gives mscorlib.dll IOException error "This file is being used by another process". Both the applications are in C#. P.S. Even if I try to open the file with NotePad (while my server is writing data in the file)it gives the same error.
0
5004
by: Rhon Stewart via DotNetMonster.com | last post by:
Hi please visit this link : http://www.eggheadcafe.com/articles/pfc/selfupdater.asp I followed all the steps for listed on the link , when I execute the application it it gives me the following error on the log file: --- ApplicationUpdateManager.StartUpdater] : The Updater has started; the target application's name is 'SayHello'. Time started: 2005_03_01_14:33:21.
25
17218
by: moondaddy | last post by:
I have an application where users need to upload images and in my web.config file I have a setting like this: <httpRuntime maxRequestLength="512" /> Which restricts image larger than 500k from being uploaded. I'm also using the HtmlInputFile control to do the uploading. My problem is that when the user's file size exceeds 512k, the page immediately redirects to the "The page cannot be displayed" error page which is very confusing. ...
0
3589
by: Mart | last post by:
Hi, I have just written (my first) VB.net app using MS Visual Basic 2005 Express Edition Beta. It is fairly simple, it reads some configuration data from an XML file then opens a new window containing a WebBrowser object. This all works fine and I'm happy with my app so I want to 'publish' to a setup file so that others can install it, but when I publish it a get the following errors:
6
4158
by: tgnelson85 | last post by:
Hello, C question here (running on Linux, though there should be no platform specific code). After reading through a few examples, and following one in a book, for linked lists i thought i would try my own small program. The problem is, I seem to be having trouble with memory, i.e. sometimes my program will work and display the correct output, and sometimes it will not and display garbage (in a printf call) so i assume i have been using...
1
3933
by: RADAR | last post by:
hi, the error code was taken from msdn and it occured in my code as follows; i am reading buffer from the file : #include <stdio.h> #include <stdlib.h> #define SIZE 10 #define DSIZE 5 #define DEPTH 150
1
3015
by: Steve Ametjan | last post by:
I've been trying to get MySQL-python to install on Leopard for the past couple of days, and I keep running into relatively the same error. I'm hoping that someone on this list will be able to help me out in solving the issue. I'd like to get this solved so I can continue developing with Django using MySQL since that's what my web server uses as well. I'd hate to have to develop using a different database engine on my local...
4
2150
by: radhikams | last post by:
hi I have written a code for writing the data of a column from database into a file and again reading that file and displaying. Im writing into an .html file....Now the problem is im getting the output but that output is followed by an error..... This is the output ----------------------------- A large number of processes are available, and can be applied to an input or a group of inputs. These processes can be chained...
0
1771
Guido Geurs
by: Guido Geurs | last post by:
I'm writing a program that list the contents of a CDrom and also the contents of the ZIP files. When there is a bad Zip file on the CD, the program keeps traying to reed the file and after +- 50 times it gives the error and ends the function. This function is called from an other function that scans al the files on the disc. Is it possible to give an error but that the program continues with the next file ? =============================...
0
9595
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
10232
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...
0
10059
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10008
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
9873
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
8891
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7420
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6682
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
5454
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.