Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I am trying to write a program for reading and writing data. It uses pointers.

It reads a file mm.jpg to and writes it to jj.jpg (Not specified here). But jj.jpg is 0 bytes while mm.jpg 54 bytes.

I have modified the whole program. void main() is here just to check whether these function work correctly.

Actually , it is to be included has header as part of an other program.


long sizeoffile(char filename[])
{
	fstream fil(filename,ios::in);

	fil.seekg(ios::beg,ios::end);
	

	long ans1 = fil.tellg();
	
	return ans1;
}


bool readfile(char filename[],char * inputdat)
{
	ifstream getit(filename,ios::in|ios::binary);
	
	long filsize = sizeoffile(filename);
	
	if(!getit)
	{	
		return false;
	}
	
	
	inputdat = new char[filsize];
	
	if(!inputdat)
	{
		
		return false;
	}
	getit.read((char*)inputdat,(filsize));
	getit.close(); 
	
	return true;
}

bool writefile(char filename[],char * outputdat,long buffsize)
{
	ofstream putit(filename,ios::out|ios::binary);
	
	if(!outputdat)
	{
		
		return false;
	}
	
	if(!putit)
	{
		
;		return false;
	}
	
	putit.write((char*)outputdat,(buffsize));
	putit.close();
	
	return true;
}

void main()
{
	char * strdat = NULL;	
	if(!(readfile("mm.jpg",strdat)))
		cout<<"Unable to read file.";
	
		if(!writefile("jj.jpg",strdat,sizeoffile("mm.jpg")))
			cout<<"Unable to write file.";
			
			delete []strdat;
		
		
	
}
Posted
Updated 25-May-13 4:11am
v5
Comments
Sergey Alexandrovich Kryukov 25-May-13 1:44am    
Preprocessor #define? Aha, so very much of C++11... :-)
—SA


  • You are mixing datatypes for your file size variables. Do not use double as it can lead to loss of precision.
  • You are trying to read more data from the input file than exists:
    C++
    	getit.read((char*)inputdat,(filsize+sizeof(inputdat)));
    // buffer is only filsize characters long
  • Similarly you are trying to write more data than was read in.
  • Note that sizeof(inputdat) and sizeof(outputdat) will alway be constant - the size of a character pointer (4 on 32-bit, 8 on 64-bit).
  • You are also using the size of the output filename in the writefile function, which is likely to be zero; hence you don't write any data.
 
Share this answer
 
Comments
compuknow 25-May-13 5:31am    
I tried changing it as told in (1)(2)(3)(4)(5) but the condition is the same , It is still zero bytes.
Richard MacCutchan 25-May-13 5:50am    
You need to show your changes; please use the "Improve question" link and show the modified code, including your main function.
I have found the problem, which I missed before. You need to modify your code as per the comments below:
C++
// inputdat, needs to be the address of the pointer, rather than the buffer.
long readfile(char filename[],char ** inputdat)
{
	ifstream getit(filename,ios::in|ios::binary);
	
	if(!getit)
	{
		return 0;
	}
	long filsize = sizeoffile(filename,B);
	
	
	// save the buffer address in the original pointer not
        // the temporary parameter variable
	*inputdat = new char[filsize];
	
	if(!*inputdat)
	{
		return 0;
	}
        // read only the number of bytes in the file
	getit.read((char*)*inputdat,(filsize));
	getit.close(); 
	
        // return the size of the input file (and buffer)
	return filsize;
}

// use the size of the input file for the amount to write
bool writefile(char filename[],char * outputdat, long filesize)
{
	ofstream putit(filename,ios::out|ios::binary);
        // no need to call filesize, since the output file does not exist
	
	if(!putit)
	{
		return false;
	}

        // write the correct number of bytes	
	putit.write((char*)outputdat,(filesize));
	putit.close();
	
	return true;
}


int main()
{
	char* buffer = NULL;
	long filesize;
        // send the address of the buffer pointer to the read function
	filesize = readfile("TestIn.bmp", &buffer);
        
        // if some data was read in
	if (filesize > 0)
	{
                // write the number of bytes read in to the buffer
		writefile("TestOut.bmp", buffer, filesize);
	}

	return 0;
}
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900