Archived

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

Zidane

Stupidly Easy File Question - But I have No Answer :(

Recommended Posts

Using Streams, how can i copy a whole input stream (file) into an output stream: Here is my current code:

PackageFileResult CPackageFile::AddFile(const std::string c_FileName_)
{
	std::ifstream FileStream;
	EdenByte pBuffer;
	CLogFile* pLog;

	pLog = CEdenEngine::GetInstance()->GetPointerToLogFile();

	int fSize=0;



	FileStream.open(c_FileName_.c_str());
	// doesnt work 
	while(FileStream.read(&pBuffer, 1))
	{
		m_OutputStream.write(&pBuffer, 1);
	}


	


	return(PACKFILE_OK); 
}


[\CODE]


I may just be stupid, but this is my first experience with streams      
[edited by - Zidane on October 15, 2003 2:30:49 PM] [edited by - Zidane on October 15, 2003 2:31:20 PM]

Share this post


Link to post
Share on other sites
I'm having a hard time reading your code for some reason...anyway:


char* source = "blahblah";
char* dest = "blahha";
std::ifstream fin(source);
std::ofstream fout(dest);
if (!fin || !fout) fail(); // return false or whatever

fout << fin.rdbuf();
fin.close();
fout.close();


[EDIT] Whoops, ofstream instead of ifstream

______________________________________________________________
The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ
MySite
______________________________________________________________


[edited by - Thunder_Hawk on October 19, 2003 3:49:33 PM]

Share this post


Link to post
Share on other sites
Well a few things you need to note. When you open a file you should always check whether it failed or not before trying to use the stream.


eg.
FileStream.open(c_FileName_.c_str());
if(FileStream.fail())
//do something, output an error etc.


When opening a stream it''s ususally a good idea to specify explicitly the flags and options to use:


FileStream.open(c_FileName_.c_str(),ios::in|ios::binary);
//ios::binary if you''re using binary mode
//use ios_base if ios doesn''t work


And over here:
quote:

while(FileStream.read(&pBuffer, 1))
{
m_OutputStream.write(&pBuffer, 1);
}



In order to use the read() and write() functions you must have your filestreams opened in binary mode like demostrated earlier. For just text input and ouput use the insertor and extractor(<< >>) or the get(), getline() functions.

Also remember to close your filestreams once you''re done.





--{You fight like a dairy farmer!}

Share this post


Link to post
Share on other sites
If your trying to do what I think your trying to do, which is copy one text file''s information over to another, then do this:

//Please note that you have to include <iostream.h>,

//<fstream.h> and <string> for this to work

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main(void)
{
std::string textinfo;
std::ifstream datafile001( "test.txt" );
while(!datafile001.eof())
{
datafile001 >> textinfo;
ofstream datafile002( "test2.txt" );
datafile002 << textinfo;
datafile002.close();
}
return(0);
}

Hope that helps, I''m counfused on your question so
I hope that''s what you asked.
-Sathenzar

Share this post


Link to post
Share on other sites