我如何使这个程序循环主要两次?

时间:2015-03-17 14:45:51

标签: c file

我需要我的程序来创建两个不同的文本文件(midinotes1和midinotes2)并在其中存储两位数据以供稍后阅读。有没有复制代码的有效方法?我明白我需要将filepointer1写入midinotes1并将filepointer2写入midinotes2,但我不知道如何让我的程序这样做? 谢谢你的建议!

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

float mtof(int note, float frequency);

int main()
{
    FILE *textFilePointer;
    FILE *textFilePointer2;
    int note;
    int velocity;
    int program;
    int counter = 0;
    char user;
    float frequency;


    do

    {

        printf("Press R to Record (R) or (X) to Exit: \n");
        scanf(" %c", &user);

        if (user == 'r' || user == 'R')
        {
            textFilePointer = fopen("/Users/Luke/Desktop/midinotes1.txt", "w");
            counter = 0;

            if (textFilePointer == NULL)
            {
                printf("Error Opening file.\n");
            }

            else
            {
                do
                {
                    note = aserveGetNote();
                    velocity = aserveGetVelocity();
                    if (velocity > 0)
                    {
                        fprintf(textFilePointer, "%d\n, %d\n", note, velocity);
                        counter++;

                    }
                    program = aserveGetProgram();

                } while (counter < 16);
                fclose(textFilePointer);

            }
        }

        else if(user == 'x' || user == 'X')
            break;

    } while(user != 'x' || user != 'X');
    return 0;

}



float mtof(int note, float frequency)
{
    frequency = 440.0 * pow(2, (note-69) / 12.0);
    printf("%d\n", note);
    return frequency;
}

2 个答案:

答案 0 :(得分:0)

int index = 0;
char filename[128];
do
{
    printf("Press R to Record (R) or (X) to Exit: \n");
    scanf(" %c", &user);

    if (user == 'r' || user == 'R')
    {
        snprintf(filename, 120, "notes%d.txt", (index+1));
        textFilePointer = fopen(filename, "w");
        counter = 0;

        if (textFilePointer == NULL)
        {
            printf("Error Opening file.\n");
        }

        else
        {
            do
            {
                // your work
            } while (counter < 16);
            fclose(textFilePointer);
            index++;
        }
    }
    else if(user == 'x' || user == 'X')
        break;

} while(user != 'x' || user != 'X');
return 0;

答案 1 :(得分:0)

int main()
{
    FILE *textFilePointer;
    char filename[100];
    int note;
    int velocity;
    int program;
    int counter = 0;
    char user;
    float frequency;
    int sample = 0;
    do
    {
        printf("Press R to Record (R) or (X) to Exit: \n");
        scanf(" %c", &user);
        if (user == 'r' || user == 'R')
        {
            sample++;
            sprintf(filename, "/Users/Luke/Desktop/midinotes%d.txt", sample);
            textFilePointer = fopen(filename, "w");
            counter = 0;

            if (textFilePointer == NULL)
                printf("Error Opening file.\n");
            else
            {
                do
                {
                    note = aserveGetNote();
                    velocity = aserveGetVelocity();
                    if (velocity > 0)
                    {
                        fprintf(textFilePointer, "%d\n, %d\n", note, velocity);
                        counter++;
                    }
                    program = aserveGetProgram();
                } while (counter < 16);
                fclose(textFilePointer);
            }
        }
        else if(user == 'x' || user == 'X')
            break;
    } while(user != 'x' || user != 'X');
    return 0;
}