473,414 Members | 1,928 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,414 software developers and data experts.

Error reading from file

=========Program 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 1671

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

Similar topics

20
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...
2
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...
0
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...
25
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...
0
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...
6
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...
1
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...
1
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...
4
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...
0
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...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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
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...
0
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...
0
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...

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.