-3

I have 2 text files which I have opened up, and I want to copy the contents of both text files into an output file. Here is what I have got so far.

printf("\nWhat is the name of the output file you want to use? (with file extension)\n");
gets_s(thirdfilename);

fopen_s(&text3, thirdfilename, "w");

while (text3 == NULL) 
{
    printf("\nThis file does not exist, please re-enter the name of the output file you want to use (with file extension)\n");
    gets_s(thirdfilename);

    fopen_s(&text3, thirdfilename, "w");
}

if (text3 != NULL)
    printf("\nYou have successfully opened '%s'\n", thirdfilename);

I'm not sure whether to use a while loop to copy the text into the third file, or to use a for loop. I have already created and opened up my first two texts before the code that I have stated above. Any help is much appreciated.

  • C++ way: `std::ofstream("third_file") << std::ifstream("first_file").rdbuf() << std::ifstream("second_file").rdbuf();` – Revolver_Ocelot Dec 12 '15 at 22:04
  • 1
    Possible duplicate of [How to copy words from .txt file into an array. Then print each word on a separate line](http://stackoverflow.com/questions/32670811/how-to-copy-words-from-txt-file-into-an-array-then-print-each-word-on-a-separa) – Ken White Dec 12 '15 at 22:06
  • Your title does not match your question text. Please correct it so that they both refer to the same topic. – Ken White Dec 12 '15 at 22:07
  • See my answer: [Copying a file](http://stackoverflow.com/questions/5420317/reading-and-writing-binary-file/#answer-5422852) – Thomas Matthews Dec 12 '15 at 22:52
  • Possible duplicate: [C++ - Reading and writing binary file](http://stackoverflow.com/questions/5420317/reading-and-writing-binary-file) – Thomas Matthews Dec 12 '15 at 22:55
  • Somehow in the process of posting this function the outside got lost. – Pete Becker Dec 12 '15 at 22:59

1 Answers1

0

To answer the title of your question, you can use the fread method of std::istream:

std::string filename;
std::cout << "Enter name of file to read: ";
std::getline(std::cin, filename);
uint8_t    data_buffer[1024*1024]; // Reserve some space.
std::ifstream input_file(filename.c_str());
input_file.read(&data_buffer[0], sizeof(data_buffer));
Thomas Matthews
  • 52,985
  • 12
  • 85
  • 144