Search code examples
cxcodemultidimensional-arraycountfile-writing

Counting into an array and writing to text file xcode


Hi I am trying to count in note data from keyboard and write the data to a text file which will subsequently be read out of and played back as notes.

I seem to only be able to write one line of numbers to the text file and help would be most appreciated. Sorry i still have some of my function code included in the global.

    #define SEQ_NOTENUM 8
    #define SEQ_NUM 2
    //  structures //

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

    typedef struct
    {
        float frequency;
        float amplitude;
    } OscData;

    // functions //

     float mtof(int note);


    // originally in main //

    OscData noteToOsc(NoteData note);
    int setcount, count;
    int currentset;
    OscData osc;

    int main()

    {
        int key, vel;
        NoteData sequence[SEQ_NUM][SEQ_NOTENUM];
        OscData noteToOsc(NoteData note);
        FILE* Sequence1;

        // START PROGRAM RECORD -- WRITE an IF/ELSE to run program -   
      dummy line atm//
        aserveGetVelocity();


        Sequence1 = fopen ("sequence1.txt", "w");

        if (Sequence1 == NULL)

         {
             printf("file Error\n");
         }

         else
          {
                    for(setcount = 0; setcount < SEQ_NUM; setcount++)
             {
                printf("--- Please enter sequence %d (%d notes)...\n",
    setcount, SEQ_NUM);



                count = 0;

                while(count < SEQ_NOTENUM)
                 {
                      key = aserveGetNote();
                      vel = aserveGetVelocity();

                    if(vel > 0)
                     {
                         sequence[setcount][count].notenumber = key;
                         sequence[setcount][count].velocity = vel;

                fprintf(Sequence1, "note %d - %d/%d\n", count, key,
     vel);
                count++;

            }

            fclose(Sequence1);
        }

        return 0;
    }

    }

Solution

  • The code's indentation is messed up, obscuring the fact that fclose(Sequence1) is closing the file after only one pass through the inner loop. You probably want to move that to somewhere further down.