1

Update:
resolved in here: https://stackoverflow.com/a/145309/15603477


https://www.cplusplus.com/reference/ostream/ostream/write/
example not working as i intended. I have 2 files(test.txt, new.txt) in the same directory/folder as main.cpp.
new.txt is empty file. test.txt have one line: https://www.cplusplus.com/reference/ostream/ostream/write/
when I run it, it says: terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc

This example copies a file into memory and then writes its content to a new file. But it now have an error.

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

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

  // allocate memory for file content
  char* 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();

update about path:
after add line std::cout << "Current path is " << fs::current_path() << '\n'; // (1)
It shows: Current path is "C:\\Users\\JIAN HE\\CLionProjects\\stl\\cmake-build-debug"
but current text.txt is C:\Users\JIAN HE\CLionProjects\stl.

update2:
problem solved. Put both file to cmake-build-debug directory.

Mark
  • 1,943
  • 7
  • 17
  • 2
    Questions seeking debugging help should generally include a [mre] of the problem, which includes a function `main` and all `#include` directives. – Andreas Wenzel Jan 22 '22 at 13:41
  • 2
    What value has `size` after getting `infile.tellg();`? – πάντα ῥεῖ Jan 22 '22 at 13:43
  • 4
    There is no official C++ reference. But cppreference.com is often recommended over cplusplus.com – user17732522 Jan 22 '22 at 13:43
  • 1
    Did opening the files succeed? What is the value of `infile.fail()` and `outfile.fail()` after attempting to open the files? Both function calls should return `false`. You can find the documentation for `std::istream::fail` [here](https://en.cppreference.com/w/cpp/io/basic_ios/fail). – Andreas Wenzel Jan 22 '22 at 13:46
  • 2
    "_I have 2 files(test.txt, new.txt) in the same directory/folder as main.cpp._": That is probably wrong. They should be in the folder from which the executable is called (the _working directory_). The shown code doesn't do any error checking, so it wont work if the file is in the wrong place. – user17732522 Jan 22 '22 at 13:47
  • 6
    My guess is that size is `-1` because the file was not found. Related: [https://en.cppreference.com/w/cpp/io/basic_istream/tellg](https://en.cppreference.com/w/cpp/io/basic_istream/tellg) – drescherjm Jan 22 '22 at 13:49
  • @drescherjm I updated my file path info. – Mark Jan 22 '22 at 13:58
  • So now you understand the problem. You should update your code to exit if the file is not found. – drescherjm Jan 22 '22 at 13:59
  • 1
    You could in addition to fixing the code to not crash if the file is not found change the working directory in CLion: [https://stackoverflow.com/questions/25834878/how-do-i-change-the-working-directory-for-my-program](https://stackoverflow.com/questions/25834878/how-do-i-change-the-working-directory-for-my-program) – drescherjm Jan 22 '22 at 14:02

0 Answers0