gpt4 book ai didi

c - 写入两个不同的txt文件

转载 作者:行者123 更新时间:2023-11-30 17:18:21 26 4
gpt4 key购买 nike

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

FILE *textFilePointer;
FILE *textFilePointer2;


typedef struct
{
int notes;
int velocity;
}NoteData;


void notetofile (NoteData input, int seqlen, int reclayer);

int main()
{

int count, count2, note, vel = 0;
char choice = 'y';
struct layers;
int reclayer = 1;
int playlayer = 1;
char choice2 = 'y', choice3 = 'y';
int seqlen = 16;
NoteData input;




printf("Welcome To Jonny Maguire's Midi record and playback application. Please Select one of the following options...\n\n");
aserveSay("Welcome To Jonny Maguires Midi record and playback application. Please Select one of the following options");
aserveSleep(8000);
while(choice != 'x')
{



printf("r to Record\np to Playback\nx to exit the program\n");
aserveSay("choose r, to record a sequence, p, to playback your recording. Or select x, at any time to exit the program");

scanf(" %c", &choice);

if(choice == 'r')
{
while(choice2 != 'n')
{
aserveSay("you have chosen to record, enter the no. of notes you wish to record");
printf("You have chosen to record, enter the no. of notes you wish to record \n\n");
scanf(" %d", &seqlen);
printf("Please play %d notes", seqlen);



textFilePointer = fopen("recording1.txt", "w");

textFilePointer2 = fopen("recording2.txt", "w");



if(textFilePointer == NULL or textFilePointer2 == NULL)
{
printf("Error Opening File!");
}
else
{



//Recording 16 note data into txt file

notetofile(input, seqlen, reclayer);
printf("Would you like to record another layer?");
scanf(" %c", &choice2);



}
reclayer++;
}
}
else if (choice == 'p')
{

while(choice3 != 'n')
{
//If P is selected, playback of the txt file
printf("which sequence would like to playback? 1, 2 or both (3)?");
scanf(" %d", &playlayer);


textFilePointer = fopen("recording1.txt", "r");

// textFilePointer2 = fopen("recording2.txt", "r");



if(textFilePointer == NULL)
{
printf("Error Opening File!");
}

//read until end of file and convert frequency
if (playlayer == 1)
{
while(!feof(textFilePointer))
{
float frequency;
float amplitude = vel/127.0;

fscanf(textFilePointer, " %d %d\n", &input.notes, &input.velocity);
printf(" %d %d\n\n", input.notes, input.velocity);

frequency = 440 * pow(2, (note-69) /12.0);

aserveOscillator(0, frequency, amplitude, 0);
aserveSleep(500);
aserveOscillator(0, 0, 0, 0);
}
}
}
fclose(textFilePointer);
playlayer++;
}
}

return 0;
}

//function to write notes to file
void notetofile(NoteData input, int seqlen, int reclayer)
{
for (int count = 1; count <= seqlen;)
{

input.notes = aserveGetNote();
input.velocity = aserveGetVelocity();



//only note on messages are sent to file
if(input.velocity > 0)
{
printf("reclayer = %d\n", reclayer);
if(reclayer == 1)
{
printf("test");
fprintf(textFilePointer, " %d %d\n", input.notes, input.velocity);
fprintf(textFilePointer, "test");
}
else if(reclayer == 2)
{
fprintf(textFilePointer2, " %d %d\n", input.notes, input.velocity);

}

printf("%d %d\n", input.notes, input.velocity);
count++;
}
}

}

该程序旨在读取 MIDI 音符,将其写入 txt 文件,然后从文件读入振荡器。fprintf 未写入“notestofile”函数中的文件。这可能是因为我同时打开了两个 textFilePointers 。

最佳答案

当您写入两个文件recording1.txt和recording2.txt时,您不会追加,因此每次您想要写入时这些文件都会重置。如果你想追加,你必须使用模式“a”(用于追加)而不是“w”。

当您编写这些文件时,两个文件描述符 textFilePointertextFilePointer2永远不会关闭,并且缓冲区不会显式刷新,因此当进程处于事件状态时您看不到文件中的内容。调用fclose这些文件描述符将刷新代码中的写入缓冲区,如下所示:

            textFilePointer = fopen("recording1.txt", "w");
textFilePointer2 = fopen("recording2.txt", "w");

if(textFilePointer == NULL or textFilePointer2 == NULL)
{
printf("Error Opening File!");
}
else
{
//Recording 16 note data into txt file
notetofile(input, seqlen, reclayer);
printf("Would you like to record another layer?");
scanf(" %c", &choice2);

//Added fclose calls to opened files:
fclose( textFilePointer );
fclose( textFilePointer2 );
//TODO Check both fclose return values
}

如果不关闭文件,则可能是文件描述符泄漏,在大多数 Linux 系统上,最大打开文件数的限制设置为 1024,输入 ulimit -n来看看它。

您可以查看事件进程上打开的描述符,以查看 lsof 的泄漏。命令或通过列表 /proc/<pid>/fd目录。

关于c - 写入两个不同的txt文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29197609/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com