Sample code of how to use the library

Registered by Fredrik Ullner

Sample code of how to use the library

Blueprint information

Status:
Not started
Approver:
None
Priority:
Undefined
Drafter:
None
Direction:
Approved
Assignee:
None
Definition:
Approved
Series goal:
None
Implementation:
Unknown
Milestone target:
None

Related branches

Sprints

Whiteboard

Put the following in a "Encoder.c" and compile to "Encoder.exe" (and link to the library)

#include <stdio.h>
#include <stdlib.h>
#include <rpc/rpc.h> /* xdr is a sub-library of rpc */

#pragma comment(lib, "Ws2_32.lib") // Library for ntohl and htonl

main()
{
    XDR xdrs;

   long i;

   FILE* fp;
   fp = fopen( "C:\\Output\\file.txt", "wb+" ); // Need to read the file in binary

   xdrstdio_create(&xdrs, fp, XDR_ENCODE);
   for (i = 0; i < 100; i++) {
        if (!xdr_long(&xdrs, &i)) {
            fprintf(stderr, "failed!\n");
            exit(1);
        }
   }
   exit(0);
}

Put the following in a "Decoder.c" and compile to "Decoder.exe" (and link to the library)

#include <stdio.h>
#include <stdlib.h>
#include <rpc/rpc.h> /* xdr is a sub-library of rpc */

#pragma comment(lib, "Ws2_32.lib") // Library for ntohl and htonl

main()
{
    // Reopens stdin to be the same input stream but in binary mode

     XDR xdrs;
    long i, j;

    FILE* fp;
    fp = fopen( "C:\\Output\\file.txt", "rb+" );

    xdrstdio_create(&xdrs, fp, XDR_DECODE);
    for (j = 0; j < 100; j++)
    {
        if (!xdr_long(&xdrs, &i)) {
            fprintf(stderr, "failed!\n");
            exit(1);
        }
        printf("%ld ", i);
    }

    printf("\n");
    exit(0);
}

Run Encoder.exe | Decoder.exe when both are built.

If Encoder.exe is successful, the numbers 0-100 should be in "file.txt". If Decoder.exe is successful, the numbers 0-100 should be printed.

Note that the we are opening the file in binary mode as is required.

You can also use stdin and stdout instead of an actual file. Remember to change the streams to binary (can be done with freopen()).

(?)

Work Items

This blueprint contains Public information 
Everyone can see this information.

Subscribers

No subscribers.