Archived

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

shalrath

File I/O

Recommended Posts

shalrath    128
Quick question, let''s say I want to grab all the characters in a file (reguardless of whether they''re whitespace/character) and stuff them into another file, in exactly the same format as in their original file, how could I do this using File Streams (C++)?

Share this post


Link to post
Share on other sites
wings656    122
I think there a couple ways. If your programming it for windows, I think there are API functions you can use (I''m not sure what they are, I''m still learning the API). If not, there are standard C++ functions that you can use, I think its something like fin << and fout >>.

Share this post


Link to post
Share on other sites
draqza    122
You figure out a way to use the DOS copy command. But seriously, I''d say check the file I/O tutorials on both NeXe and my site (http://www20.brinkster.com/draqza) and then all you''d have to do would be read one file into a temporary buffer, then stick it back out. I don''t know if just using << and >> would work quite right, though.
--


WNDCLASSEX Reality;
...
...
Reality.lpfnWndProc=ComputerGames;
...
...
RegisterClassEx(&Reality);


Unable to register Reality...what''s wrong?
---------
Dan Upton
Lead Designer
WolfHeart Software

Share this post


Link to post
Share on other sites
Kylotan    9860
#include <fstream>

int main()
{
ofstream outFile("out.txt");
ifstream inFile("in.txt");
outFile << inFile.rdbuf();
}


(There''s a chance this might mangle binary files if done in text mode. If so, just be sure to open them both in binary mode.)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
  

bool FileExists(char *FileName)
{
FILE *Test = fopen(FileName, "r");
if(!Test)
return false
else
{
fclose(Test);
return true;
}
}

bool CopyFile(char *Source, char *Dest, bool Overwrite)
{
FILE *SourceStream = fopen(Source, "rb");
if(!SourceStream)
return false;

if(!Overwrite && FileExists(Dest))
{
fclose(SourceStream);
return false;
}

FILE *DestStream = fopen(Dest, "wb");
if(!DestStream)
{
fclose(SourceStream);
return false;
}

unsigned int BufferSize = 1024;
char *Buffer = new char[BufferSize];


unsigned int BytesRead = 0;
while((BytesRead = fread(Buffer, 1, BufferSize, SourceStream)))
{
if(fwrite(Buffer, 1, BytesRead, DestStream) != BytesRead)
{
delete []Buffer;
fclose(SourceStream);
fclose(DestStream);
return false;
}
}

delete []Buffer;
return true;
}

Share this post


Link to post
Share on other sites