c - 我如何让这个程序循环主程序两次?

标签 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;
}

最佳答案

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;

关于c - 我如何让这个程序循环主程序两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29102176/

相关文章:

c - 我找不到内存泄漏。 (SIGSEGV)(埃拉托色尼分段筛)(C)

c++ - 需要字节序解决方法

c - 矩阵中存在障碍物的路径数

c++ - 用 C 编译 C++ 代码

c - 如何将 __FUNCTION__ 添加到系统日志中

c - C 文件的问题

java - Android - 根据文件名获取文件的 R.raw.value?

file - 如何使用shell脚本检查文件的最后更改时间

Android:从相机获取文件名?

java - 从不一致的 Android native 文件中获取 URI 文件路径选择返回的 URI