freopen: возвращаясь назад к исходному потоку

Скажем, расстояние вершин до начала координат равно 1. И скажем, (1, 0) всегда является координатой многоугольника.

Учитывая количество вершин (скажем, n), угол поворота, необходимый для позиционирования (1, 0) к следующей координате, будет (360 / n).

Требуемое здесь вычисление - вращение координат. Вот что это такое; Матрица вращения .

Скажите, что тета = 360 / n;

[cos(theta) -sin(theta)]
[sin(theta) cos(theta)]

будет вашей матрицей вращения.

Если вы знаете линейную алгебру, вы уже знаете, что я имею в виду. Если не просто взглянуть на Умножение матриц

7
задан Jack 4 November 2009 в 13:32
поделиться

2 ответа

This can be achieved using fileno, dup and dup2 calls. I have tried this on linux not sure whether this will work on mac but I am sure you will get some equivalent functions for your setup. See if this sample code works for you. Sorry for lack of error-handling in the code. :)

    #include <stdio.h>

    main()
    {
        int    fd;
        fpos_t pos;

        printf("stdout, ");

        fflush(stdout);
        fgetpos(stdout, &pos);
        fd = dup(fileno(stdout));
        freopen("stdout.out", "w", stdout);

        f();

        fflush(stdout);
        dup2(fd, fileno(stdout));
        close(fd);
        clearerr(stdout);
        fsetpos(stdout, &pos);        /* for C9X */

        printf("stdout again\n");
    }

    f()
    {
    printf("stdout in f()");
    }
11
ответ дан 7 December 2019 в 01:22
поделиться

This seems like a roundabout way of solving your problem. Why not just open each file with fopen() and then writing to its FILE * with fputs, fprintf, fwrite etc? No need to redirect stdout.

-1
ответ дан 7 December 2019 в 01:22
поделиться