Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Removing a string from a file

This topic is 5423 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m trying to do a very simple image encrpytion by adding a string such as "don''thackme" at the beginning of my image file using a hex editor. This was a sugguested by somebody on a different forum. This indeeds stops me from being able to open the file using a paint program. However, how do I remove the string from the file so that my program can use it as normal. Please put me right if I''m way off track here. Thanks

Share this post

Link to post
Share on other sites
That's not exactly image encryption... but rather corrupting the image. I assume that by "adding a string such as .... at the beginning" you mean to prepend all of the image data. To undo this, I would open up the image file along with another file in Binary mode (ios::binary). Then, I would move the Get ptr (seekg()) to the first byte AFTER your string. Then, just read characters from the 'enctypted' file and write them to the output file.

Look at the following bit of code:

#include <fstream>
#include <stdio.h>

#define FILEIN "in.jpg"
#define FILEOUT "out.jpg"
#define MYSTRING "DontHackMe"

using namespace std;

void main()
// Create our file streams

fstream fin, fout;

// Open the files

fin.open( FILEIN, ios::binary | ios::in );
fout.open( FILEOUT, ios::binary | ios::out );
printf("Moving data from %s to %s...\n", FILEIN, FILEOUT);

// Make sure the files opened

if( fin.is_open() == false ||
fout.is_open() == false )

// Move the 'get' (read) pointer to the first byte AFTER the string

fin.seekg( strlen(MYSTRING), ios::beg );

// Read the data and then write it until we have reached the end of the file.

char pBuffer [ 1024 ];
const int iBuffSz = 1024;

while( !fin.eof() )
// Read the data

fin.read( pBuffer, iBuffSz );

// Write the data

fout.write( pBuffer, fin.gcount() );

// Show status


[edited by - zackriggle on January 15, 2004 3:33:21 PM]

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!