0

I want to write the integer b in the output file. I try to provide the address of a, but it only outputs the first several chars, there is not number after the chars.

Here is my code:

#include <cmath>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(int argc, char *argv[])
{
    int rank;
    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPIO_Request request;
    MPI_Status status;
    MPI_Offset offset = 0;
    MPI_File fh;

    std::string str1 = "output";
    const char *na = str1.c_str();
    std::string str2 = "#!TDV112";
    const char *version = str2.c_str();

    MPI_File_open(MPI_COMM_WORLD, na, MPI_MODE_CREATE | MPI_MODE_WRONLY, MPI_INFO_NULL, &fh);

    if (rank == 0)
    {

        MPI_File_seek(fh, offset, MPI_SEEK_SET);
        // header !version number
        MPI_File_write(fh, version, 8, MPI_CHAR, &status);
        // INTEGER 1
        int a = 1;

        MPI_File_write(fh, &a, 1, MPI_INT, &status);
    }

    return 0;
}
  • 1
    Shouldn't you close the file? Normally that flushes stuff out. Also, maybe you should write a null terminator after your string. – Victor Eijkhout Feb 11 '22 at 22:34
  • you also do need to `MPI_Finalize()` – Gilles Gouaillardet Feb 12 '22 at 02:38
  • "there is no number" how do you know? this code behaves as you expected, even with the errors pointed out above, but if you `cat` the file you won't see what you expect, and if you open in an editor you'll see garbage (a mix of txt and binary). I used `xxd`. – Rob Latham Feb 13 '22 at 15:35

0 Answers0