C从文件读入方程
我程序的这一部分从txt文件中读取16个数字,将它们添加到方程的“音符”部分,将它们转换为音高,然后将其发送到serveOscillator,它以设置的音高播放16个音符。一个有效的睡眠决定了每个音符播放的时间(500毫秒),但第16个音符并没有停止响起,我需要它在500毫秒后停止 您应该测试文件字段的读取是否正确,而不是测试feof(参见上面的注释)。你再打一次电话就把振荡器关掉了C从文件读入方程,c,xcode,pointers,fopen,C,Xcode,Pointers,Fopen,我程序的这一部分从txt文件中读取16个数字,将它们添加到方程的“音符”部分,将它们转换为音高,然后将其发送到serveOscillator,它以设置的音高播放16个音符。一个有效的睡眠决定了每个音符播放的时间(500毫秒),但第16个音符并没有停止响起,我需要它在500毫秒后停止 您应该测试文件字段的读取是否正确,而不是测试feof(参见上面的注释)。你再打一次电话就把振荡器关掉了 else if(choice == 'p') { textFilePo
else if(choice == 'p')
{
textFilePointer = fopen("/Users/jonnymaguire/Documents/Uni Work/Audio Programming /iap/iapProj/Builds/MacOSX/build/Debug/textp15.txt", "r");
if(textFilePointer == NULL)
{
printf("!Error Opening File!");
}
while(!feof(textFilePointer))
{
float frequency;
fscanf(textFilePointer, " %d\n", ¬e);
printf(" %d\n\n", note);
frequency = 440 * pow(2, (note-69) /12.0);
aserveOscillator(0, frequency, 1, 0);
aserveSleep(500);
}
要提供有用的错误消息:
char*path=“/p/a/t/h;fp=fopen(path,“r”);如果(fp==NULL){peror(path);…}
代替feof
,最好测试fscanf
的结果。(与for
循环结合使用,因为您已经知道您期望的是16个数字——然后读取失败测试被简化为常识,而不是代码的关键部分。)次要:“
在%d”
之前不需要。说明符%d”
消耗前导空格。
int note;
float frequency;
while(1 == fscanf(textFilePointer, " %d\n", ¬e)) {
printf(" %d\n\n", note);
frequency = 440 * pow(2, (note-69) /12.0);
aserveOscillator(0, frequency, 1, 0);
aserveSleep(500);
}
aserveOscillator(0, 0, 0, 0); // turn off osc