0

xdrファイルから入力を読み取ってシェルに結果を表示するコードがありますが、プログラムがgeanyまたはnanoまたは他のプログラムで読み取れる形式で結果を保存することを好みます。プログラム:

#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( "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);
}

ご覧のとおり、ファイルは結果を出力しますが、通常どおり操作して読み取ることができるファイルに保存することをお勧めします。

あなたの助けに感謝します。

4

1 に答える 1

0

このようにすることも、dup2() を使用して出力をファイルにリダイレクトすることもできます。

#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,*fpwrite;
fp = fopen( "file.txt", "rb+" );
fpwrite = fopen("Myfile","w+");
if(fpwrite == NULL){
     printf("Failed to open destination file\n");
} 

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);
    fprintf(fpwrite,"%ld ",i);

}

printf("\n");
fclose(fp);
fclose(fpwrite);
exit(0);

}

于 2013-07-10T09:13:25.343 に答える