Skip to content

Replicating open_memstream example #338

Closed Answered by igrqb
igrqb asked this question in Q&A
Discussion options

You must be logged in to vote

Found a solution:

Updated C code for clarity.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {

    char* buffer = NULL;

    size_t bufferSize = 0;
    FILE* myStream = open_memstream(&buffer, &bufferSize);

    fprintf(myStream, "This is a test");

    fclose(myStream); 
    printf("Size = %d\n", bufferSize);
    printf("String = %s\n", buffer);
    free(buffer);
}

Output from C program:

Size = 14
String = This is a test

Matching Java JNR-FFI code:

  public interface LibC {
    Pointer open_memstream(Pointer stringPtr, Pointer sizePtr);
    int fprintf(Pointer stream, String str);
    int fclose(Pointer stream);
    void free(Pointer pointer);
  }

  public static

Replies: 3 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Answer selected by igrqb
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@headius
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants