LearnFrenzy
Sign in Take a free test

File Handling (fstream) · Find the Error · Question 6

Error: write() with an int pointer Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 6 Back to Exercise

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <fstream>

int main() {
    std::ofstream out("data.bin", std::ios::binary);
    int values[3] = {1, 2, 3};
    out.write(values, sizeof values);
    return 0;
}
  • A sizeof cannot be used on arrays
  • B write() takes const char*; convert explicitly with reinterpret_cast<const char*>(values) Correct
  • C binary mode is not allowed with write
  • D values must be a vector
Correct Answer: Option B

The cast makes the "raw bytes" intent visible.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links