Hello. I'm writing codeing progam. It reads from file using fstream and then converts simbols to ascii, changes ascii, converts ascii to symbols and writes them to file. I tried to code .txt file with it. It worked perfect. But then i tried to convert .bmp file and then i decoded .bmp file and tried to watch pic i windows picture viewer showed me following error: "Drawing failed". I think i did something wrong. but i'm not sure... So i'm asking you is this possible to code and decode all files using fstream and converting symbols to ascii? Please answer me.
P.s sorry for my bad english. If you didn't understood me just ask.

Recommended Answers

All 7 Replies

can you post the code you are using to decode the bmp?

void Kodavimas () {
	using namespace std;
	string ReadBuff;
	int ReadBuffDydis;
	char ReadBuffSimbolis;
	int FailoSimbolioASCII;
	int SifroRaktoIlgis;
	int SifroRaktoSimbolis;
	int SifroRaktoASCII;
	int SifroRaktoIndex = 0;
	int ASCIIiFaila;
	char SimbolisIfaila;
	ifstream ReadFile(OpenFileName);
	ofstream WriteFile(SaveFileName);
	SifroRaktoIlgis = SifroRaktas.GetLength();
	while (getline(ReadFile, ReadBuff)) {
		ReadBuffDydis = ReadBuff.length();
		for (int i2=0; i2<ReadBuffDydis; i2++) {
			ReadBuffSimbolis = ReadBuff[i2];
			FailoSimbolioASCII = int(ReadBuffSimbolis);
			SifroRaktoSimbolis = SifroRaktas[SifroRaktoIndex];
			SifroRaktoIndex+=1;
			if (SifroRaktoIndex = SifroRaktoIlgis)
				SifroRaktoIndex = 0;
			SifroRaktoASCII = int(SifroRaktoSimbolis);
			ASCIIiFaila = FailoSimbolioASCII + SifroRaktoASCII;
			if (ASCIIiFaila > 255)
				ASCIIiFaila-=255;
			SimbolisIfaila = char(ASCIIiFaila);
			WriteFile << SimbolisIfaila;
		}
		WriteFile << "\n";
	}
}

Most variables name are in my language. If you need i can translate them
OpenFileName and SaveFileName are CString variables and i recieve them from CFileDialog.
SifroRaktas is a cipher key variable.

I tried to open coded .bmp file with notepad. It seems that my program only reads very little part of picture symbols. I don't know why. Pls help me someone! :(

.bmp files are binary files, not ascii text files. You have to open the file with ios::binary flag and then use ifstream's read() method to read the binary data. getline() will not work. And you will have to read the file into a character array, not a std::string object

char readbuf[255];
ifsteam in("filename.bmp", ios::binary);
if( in.is_open() )
{
   while( in.read(readbuf, sizeof(readbuf)) )
   {
       int bytesRead = in.gcount(); // get number of bytes read
       // blabla
   }
}

.bmp files are binary files, not ascii text files. You have to open the file with ios::binary flag and then use ifstream's read() method to read the binary data. getline() will not work. And you will have to read the file into a character array, not a std::string object

char readbuf[255];
ifsteam in("filename.bmp", ios::binary);
if( in.is_open() )
{
   while( in.read(readbuf, sizeof(readbuf)) )
   {
       int bytesRead = in.gcount(); // get number of bytes read
       // blabla
   }
}

Thank you. Opening file as binary helped. but i cant use char array because it can store too little chars. However i can't use std::string too, because picture was a mess :D any ideas?

check the file size then allocate a buffer of that size

char* buf = 0;
ifstream in("filename.bmp", ios::binary);
in.seekg(0,ios::end); // go to end of file
unsigned int size = in.tellg(); // get file size
buf = new char[size]; // allocate memory
in.seekg(0,ios::begin); // back to beginning of file
in.read(buf, size); // read the file
void Kodavimas () {
	using namespace std;
	string ReadBuff;
	int ReadBuffDydis;
	char ReadBuffSimbolis;
	int FailoSimbolioASCII;
	int SifroRaktoIlgis;
	int SifroRaktoSimbolis;
	int SifroRaktoASCII;
	int SifroRaktoIndex = 0;
	int ASCIIiFaila;
	char SimbolisIfaila;
	ifstream ReadFile(OpenFileName);
	ofstream WriteFile(SaveFileName);
	SifroRaktoIlgis = SifroRaktas.GetLength();
	while (getline(ReadFile, ReadBuff)) {
		ReadBuffDydis = ReadBuff.length();
		for (int i2=0; i2<ReadBuffDydis; i2++) {
			ReadBuffSimbolis = ReadBuff[i2];
			FailoSimbolioASCII = int(ReadBuffSimbolis);
			SifroRaktoSimbolis = SifroRaktas[SifroRaktoIndex];
			SifroRaktoIndex+=1;
			if (SifroRaktoIndex = SifroRaktoIlgis)
				SifroRaktoIndex = 0;
			SifroRaktoASCII = int(SifroRaktoSimbolis);
			ASCIIiFaila = FailoSimbolioASCII + SifroRaktoASCII;
			if (ASCIIiFaila > 255)
				ASCIIiFaila-=255;
			SimbolisIfaila = char(ASCIIiFaila);
			WriteFile << SimbolisIfaila;
		}
		WriteFile << "\n";
	}
}

Most variables name are in my language. If you need i can translate them
OpenFileName and SaveFileName are CString variables and i recieve them from CFileDialog.
SifroRaktas is a cipher key variable.

I just added ios::binary openmodes at all fstreams. First try was successful, but second not... [IMG]http://img255.imageshack.us/img255/5564/codeingfail.png[/IMG]
Original picture at the left and uncoded picture at the right. Any ideas?

It seems that it's same with all .jpg pictures

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.