0
 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", &note);
                printf(" %d\n\n", note);

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

                aserveOscillator(0, frequency, 1, 0);
                aserveSleep(500);

            }

This part of my program reads 16 numbers from a txt file, adds them into the 'note' part of the equation to convert them into a pitch, and then sends it to the serveOscillator which plays 16 notes at the set pitch. Aserve sleep dictates how long each note plays for (500ms) but the 16th does not stop ringing out and i need it to stop after 500ms!

Neo
  • 203
  • 1
  • 11
  • 3
    http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong – Gopi Mar 17 '15 at 12:42
  • To provide a useful error message: `char *path="/p/a/t/h; fp=fopen(path, "r"); if(fp==NULL){perror(path);...}` – William Pursell Mar 17 '15 at 12:47
  • 1
    In lieu of `feof`, it's better to test the result of `fscanf`. (In combination with a `for` loop, since you already know you are *expecting* 16 numbers -- the read fail test is then reduced to Common Sense, instead of being a crucial part of the code.) – Jongware Mar 17 '15 at 12:51
  • Minor: `" "` is not needed before `"%d"`. The specifier `"%d"` consumes leading white-space. – chux - Reinstate Monica Mar 17 '15 at 13:14

1 Answers1

0

Instead of testing feof (see comments above) you should be testing that the file field was read correctly. And you turn off the oscillator with another call.

int note;
float frequency;
while(1 == fscanf(textFilePointer, " %d\n", &note)) {
    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
Weather Vane
  • 33,872
  • 7
  • 36
  • 56