create and open text file

Good day everyone,

I am still new with C++ language and i hoping if someone could teach me. I' trying to copy paste this coding and the problem is i want it creating and open it. And here is the coding

// Copy a file
#include <fstream>
using namespace std;

int main () {

char * buffer;
long size;

ifstream infile ("test.txt",ifstream::binary);
ofstream outfile ("new.txt",ofstream::binary);

// get size of file
infile.seekg(0,ifstream::end);
size=infile.tellg();
infile.seekg(0);

// allocate memory for file content
buffer = new char [size];

// read content of infile
infile.read (buffer,size);

// write to outfile
outfile.write (buffer,size);

// release dynamically-allocated memory
delete[] buffer;

outfile.close();
infile.close();
return 0;
}

And is it possible i can make it view and what did i miss to put inside this coding? Thanks in advanced

Syed Murtadha Alhabshi
first off, you missed the code tag. on the side of the posting box where it says Format: the code tag is the #, just press that and put the code in between the tags. Besides that, can you be a bit clearer about what you're trying to do here? i think you're overthinking this if you're trying to do what i think you are.
Sorry if make complication, I am not overthinking or something the code here it will only create new text file but what i want to do is when i create the new text file and at the same time i want it to open. is it possible and what else do i need to add inside the coding above?

Thanks a million
You can use CFile class in MFC,
It's easier than file stream.

I test you code,
and I can't find any error.

It goes well,
But you should add the exception detech struct,
use if(-1 == size) or try catch
Thanks a million for the advise.

:))
Topic archived. No new replies allowed.