c - fprintf(textFilepointer) 在文件中打印两次

标签 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 个不同的音符编号和十六进制音符编号,因为它每次都写双倍。为什么?

最佳答案

如果您在播放每个音符时收到两条消息,一条是按键,一条是按键释放,您可以通过将音符消息打印到控制台来判断这一点,而不是的到一个文件。试试这个:

#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;
}

关于c - fprintf(textFilepointer) 在文件中打印两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28966266/

相关文章:

c - 当工作区位于 ~/src 而不是/tmp 时,VS Code 在 "stdio.h"下显示红色曲线

python - 如何确定python中的配置文件路径?

c - 奇怪的 printf 和 puts 语句解释

c - 从多维数组中提取时序并写入c中的文件

c - 在 C 中的一个 printf 中打印(部分)数组

c - c程序中execlp()的使用

c - 空指针操作

c - 时钟时间错误

c# - 将大字符串写入文件的最佳方法

javascript - 什么是 JavaScript FileReader 以及它可以在哪里使用?