0

I am trying to read from multiple audio files using inputs read from a text file. The files are read using ligand file. I am wondering what is the best way to go about opening an unknown number of audio files. I am trying to use an array of pointers however I am getting segmentation faults I can't appear to correct.

Any suggestions would be really appreciated.

#include <stdio.h>
#include <stdlib.h>
#include <sndfile.h>

#define  BUFFSIZE  1024

int main(int argc, char* argv[])
{
  if(argc < 1)
  {
    printf("please enter a file to read instructions from");
    exit(1);
  }

  //* INPUT USER INSTRUCTIONS FROM TEXT FILE *//

  FILE *txtfile;
  char filechar[40], chr;
  int i,j,k;
  int count_lines = 0;

  txtfile = fopen(argv[1],"r");
  if (txtfile == NULL)
  {
    printf("Error opening file....\n");
    exit(0);
  }

char** Track_Names = malloc(sizeof(char*) * 100);
float* start_time = malloc(sizeof(float*) * 100);
float* gain = malloc(sizeof(float*) * 100);
float* pan = malloc(sizeof(float*) * 100);

i = 0;

   while(!feof(txtfile))
    {
      Track_Names[i] = malloc(sizeof(char) * 100);

     fscanf(txtfile, "%s %f %f %f ", &Track_Names[i][0], &start_time[i], &gain[i], &pan[i]);
     printf("\n name-%s time-%.2f gain-%.2f pan-%.2f \n", Track_Names[i], start_time[i], gain[i], pan[i]);
     i++;
     //* later use a pointer to store txt file values into arrays *\\

    }

   printf("%d\n", i);
   int track_number = i;
    //* Counting number of Files to process *//
    printf("%s",Track_Names[1]);
   fclose(txtfile);

SNDFILE  *psf_in[i-1], *psf_out;
SF_INFO   sfinfo_in[i-1], sfinfo_out;
float     buffer[BUFFSIZE], samp[BUFFSIZE/i-1];
sf_count_t  count;
printf("test");

///////////////* OPEN AUDIO FILES *\\\\\\\\\\\\
for (j=0; j <i; j++) ///Here is roughly where the segementation fault occurs//
{

 psf_in[j] = sf_open(Track_Names[j], SFM_READ, &sfinfo_in[j]);

}


for (j = 0; j <=i; j++)
{
 sf_close(psf_in[j]);
}

   free(Track_Names);
   free(start_time);
   free(gain);
   free(pan);


   return (0);
}

1 Answers1

0

SNDFILE *psf_in[i-1] means, among other things, that the larges valid index into this array is i - 2. However, the loop

for (j=0; j <i; j++) {
    psf_in[j] = sf_open(Track_Names[j], SFM_READ, &sfinfo_in[j]);
}

accesses psf_in[i - 1]. All bets are off. Declare your array as

SNDFILE  *psf_in[i];

or adjust the loop bound.

user58697
  • 7,808
  • 1
  • 14
  • 28