Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
I am trying to copy - write an image using C++ fstream functions (without using any standard commands for copying files using C++).
 
Below is the code i wrote :
 
void main()
{
	char *imgs=NULL;
	imgs = new char[filsz()];
	ifstream inm("aaa.jpg",ios::in|ios::binary);
	for(int i=0;i<filsz();i++)
	{
		inm>>imgs[i];
	}
	ofstream outnm("linm.jpg",ios::out|ios::binary);
	for(int i=0;i<filsz();i++)
	{
		outnm.put(imgs[i]);
	}
	
	
	
}
 
long filsz()
{
	fstream inm("aaa.jpg",ios::in);
	inm.seekg(ios::beg,ios::end);
	long sz = inm.tellg();
	return sz;
	
}
</pre>
 

The file size of the input and output file was the same , i could open the original file with windows image viewer however when it comes to copied file it wont open and shows an error.
 
What could be the reason ?
Posted 10-May-13 22:51pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

The first thing I would do, is to change from char to unsigned char in case it is "losing" the top bit, and try caching the file size to save waste: you are calling the filsz function for each and every byte in the file, twice, as it is called each time the for loop needs to see if it should go round again:
int bytes = filsz();
imgs = new unsigned char[bytes];
ifstream inm("aaa.jpg",ios::in|ios::binary);
for(int i=0; i < bytes; i++)
 
If that doesn't sort it, then you need to look at the actual data in the file(s) to work out what is being output for what input. PsPad[^] has a HEX mode which would make that relatively simple (and it's not a bad editor, either if you need more complex editing than VS can provide - I don't use it much, but it can save a lot of time)
 
[edit]Stupid HTML... - OriginalGriff[/edit]
  Permalink  
v2
Comments
compuknow at 11-May-13 5:19am
   
Changing it unsigned doesn't work , as suspected all the data in the input file does not match with those on the output file.
 
Is there any way to absorb image data from the image in hexadecimal matrix (array) format and then output it as a new image file.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I see that you haven't closed any of the two files. While its okay when you are reading the file, after writing all the data to the file, close it with a simple
 
outnm.close();
 
after the write loop.
 
This will flush all the contents in the file that the system usually caches
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 381
1 Sergey Alexandrovich Kryukov 265
2 Praneet Nadkar 237
3 Marcin Kozub 225
4 /\jmot 189
0 OriginalGriff 8,284
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,614
3 Maciej Los 4,989
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 13 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100