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.

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";
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.