fprintf(textFilepointer)两次打印到文件中

时间:2015-03-10 14:24:05

标签: c file printf do-while

#include "aservelibs/aservelib.h"
#include <stdio.h>
#include <math.h>

FILE *textFilePointer;

int main()
{
    int counter = 0;
    int note;

    printf("Press key on Axiom MIDI keyboard to display note number\n");


    textFilePointer = fopen("/Users/jonnymaguire/Documents/Uni Work/Audio Programming /iap/iapProj/Builds/MacOSX/build/Debug/textp15.txt", "w");

    do
    {
        if(textFilePointer == NULL)
        {
            printf("!Error Opening File!");
        }
        else
        {
            /*get frequency from user*/

            note = aserveGetNote();

            fprintf(textFilePointer, "note = %d\n", note);
            fprintf(textFilePointer, "hex note = %x\n\n", note);
            counter++;


        }

    }
    while(counter < 16);

    fclose(textFilePointer);

    return 0;               /*end*/
}

该程序只是在用户在键盘上播放后将音符编号打印到文件中。然而,它打印了所有内容的两倍,因此我不能记录15个不同的音符编号和十六进制音符编号,因为它每次写入时只能播放8次。为什么?

1 个答案:

答案 0 :(得分:0)

如果您为每个播放的音符收到两个消息,一个用于按键,一个用于按键释放,您可以通过将音符消息打印到控制台来告知到一个文件。试试这个:

#include "aservelibs/aservelib.h"
#include <stdio.h>
#include <math.h>

int main(void) { 
    int counter = 0;
    int note;
    printf("Press key on Axiom MIDI keyboard to display note number\n");
    do
    {
        /*get frequency from user*/
        note = aserveGetNote();
        printf("note = %d\n", note);
        counter++;
    }
    while(counter < 16);
    return 0;
}