I'm having problem writing into many files that Ive created.I have successfully read a file and store it as a vector. I have duplicated this file into certain number files and with diffrent names e.g. filemname1, filename2,filename3....My problem is how to write the content of the file that I've read into all these new files. I hope somebody will help me with this .
N:B: The code below is just a fractional part of the source code
>>.>> Not sure what you did. In order to do this is simple.
http://cplusplus.com/reference/iostream/ostream/write/
May not SEEM simple but once you understand it, it's really simple. What this does is create two file stream containers, and opens them.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
infile.seekg(0,ifstream::end); //Moves towards the end of the file with an offset of 0.
size=infile.tellg(); //Because your get pointer is at the end, you can tell how many characters are there until you reach 0. So size is the size of the file buffer.
infile.seekg(0); //This sets the get pointer to the beginning of the file stream
buffer = newchar [size]; //Actual buffer which holds the content of the file.
infile.read (buffer,size); //This reads the content into the buffer with the size of variable size
// write to outfile
outfile.write (buffer,size); //Dump all of the content of buffer into the outfile.
// release dynamically-allocated memory
delete[] buffer; //Delete the buffer which is probably taking up some space.
outfile.close(); //Deconstruct
infile.close(); //Deconstruct
return 0; //End application
If you wondering how it knows how to format, it uses \n for enter and a couple others. When it's read into the buffer, its read as one large line with symbols like \n for formatting.