I have an MPI process that I need to print to a file. The MPI system uses functional commands like MPI_File_iwrite, which accept a buffer (a char pointer, for instance) and an integer length. Then, the buffer is printed to the file.
So, in order to print what I need to print to the file, I need to somehow replace the printf function with a function that maps the formatting conversion to a char * buffer. This is problematic, since the strings can vary in length...for instance:
printf("something %d, %d, %d, %f, etc.",x,y,z,p,d,...);
How do I recover the string that printf formatting produces, figure out what the length is, and pass it as a char * to the mpi function?
It looks like you want to use snprintf
to a sufficiently-large buffer.
If the buffer was sufficiently large, the return value of snprintf
is equal to the number of characters printed. You can compute the length of the output string as the minimum of the size of the buffer and the return value of snprintf
.