File I/O

Started by
3 comments, last by shalrath 22 years, 8 months ago
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++)?
Advertisement
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 >>.
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
WNDCLASSEX Reality;......Reality.lpfnWndProc=ComputerGames;......RegisterClassEx(&Reality);Unable to register Reality...what's wrong?---------Dan Uptonhttp://0to1.orghttp://www20.brinkster.com/draqza
#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.)
  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;}  

This topic is closed to new replies.

Advertisement