So I'm working on a C++ program and part of it involves converting decimal numbers to hexadecimal. I have been using printf("%02X",number) to do this, but I'd like to print to a text file. Just wondering if anyone knew of a way to send formatted text to file rather than the screen. I've been trying to use fprintf with no luck. Any suggestions would be appreciated. I'll also post code if anyone deems it relevant. Thanks.

8 Years
Discussion Span
Last Post by Ancient Dragon

1) open the file

FILE* fp = fopen("myfile.txt");

2) use fprintf()

fprintf(fp,"%x", 123);

How difficult was that??

But this is c++, so you should be using the functions in <fstream>

#include <fstream>
using namespace std;

int main()
     ofstream out("myfile.txt");
     out << hex << 123 << "\n";
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.