0

I have a C++ code. I'm trying to open about 20 WAV files using this code. But when it gets on the 10th file, it stops opening and reading files? What is the problem? I have 20 WAV files stored on my computer at a certain path. It works fine for file number 1 to 9 but it wont open from file 10 to 20. I'm sure my file path is correct which I'm giving to the libsnd open file function.

#include <stdio.h>
#include <stdlib.h>
#include <sndfile.h>
#include <iostream>
#include <math.h>
#include <fftw3.h>
#include <stdint.h>
#include <string.h>
using namespace std;

void func_fileopen(int son, char note[], int samplenum) { 
    SNDFILE *sf;
    SF_INFO info;
    int num_channels;
    int num, num_items;
    float *buf = NULL; //pointer to float
    int f, sr, c;
    int i, j;
    FILE *out;
    int samples = 913;
    float outbuf[samples];
    char n[son];
    char temp1[son] = {0};
    char temp2[son + 13] = {0};
    strcpy(n, note);
    char filebasepath[] = "C:\\Users\\Gumm\\Documents\\Audacity\\10ms\\";
    char samplepath[] = "\\Sample";
    char wav[] = ".wav";
    temp1[16] = sprintf(temp1, "%s", n);
    temp2[13] = sprintf(temp2, "%s%d%s", samplepath, samplenum, wav);
    strcat(filebasepath, temp1);
    strcat(filebasepath, temp2);
    puts(filebasepath);

    /* Open the WAV file. */
    info.format = 0;

    sf = sf_open(filebasepath, SFM_READ, &info);

    if (sf == NULL) {
        printf("Failed to open the file.\n");
        exit(-1);
    }

    /* Print some of the info, and figure out how much data to read. */
    f = info.frames;
    sr = info.samplerate;
    c = info.channels;
    printf("frames=%d\n", f);
    printf("samplerate=%d\n", sr);
    printf("channels=%d\n", c);
    num_items = f * c;
    printf("num_items=%d\n", num_items);

    /* Allocate space for the data to be read, then read it. */
    buf = (float *) malloc(num_items * sizeof(float));
    num = sf_read_float(sf, buf, num_items);
    sf_close(sf);
    printf("Read %d items\n", num);
    /* Write the data to filedata.out. */
    //out = fopen("filedata.out","w");
    }

int main() {   
    int notenum;
    int samplenum;
    int notevar;
    int sizeofnote;
    char note[sizeofnote];
    for (notenum = 1; notenum < 8; notenum++)
    {
        for (samplenum = 1; samplenum < 21; samplenum++)
        {

        notevar = notenum;
        if (notevar == 1) {
            char note[] = "ardha chapu";
            sizeofnote = sizeof(note) / sizeof(char) - 1;
            func_fileopen(sizeofnote,note,samplenum); 
        } else if (notevar == 2) {
            char note[] = "chapu";
            sizeofnote = sizeof(note) / sizeof(char) - 1;
            func_fileopen(sizeofnote, note, samplenum);
        } else if (notevar == 3) {
            char note[] = "dheem";
            sizeofnote = sizeof(note) / sizeof(char) - 1;
            func_fileopen(sizeofnote, note, samplenum);
        } else if (notevar == 4) {
            char note[] = "dhi";
            sizeofnote=sizeof(note) / sizeof(char) - 1;
            func_fileopen(sizeofnote, note, samplenum);
        } else if (notevar == 5) {
            char note[] = "num";
            sizeofnote = sizeof(note) / sizeof(char) - 1;
            func_fileopen(sizeofnote, note, samplenum);
        } else if (notevar==6) {
            char note[] = "sampoorna chapu";
            sizeofnote = sizeof(note) / sizeof(char) - 1;
            func_fileopen(sizeofnote, note, samplenum);
        } else {
            char note[] = "ta";
            sizeofnote = sizeof(note) / sizeof(char) - 1;
            func_fileopen(sizeofnote, note, samplenum);
        }
    }
    return 0;    
}

This is my output

C:\Users\Gumm\Documents\Audacity\10ms\ardha chapu\Sample1.wav
frames=944
samplerate=96000
channels=1
num_items=944
Read 944 items

C:\Users\Gumm\Documents\Audacity\10ms\ardha chapu\Sample2.wav
frames=941
samplerate=96000
channels=1
num_items=941
Read 941 items

C:\Users\Gumm\Documents\Audacity\10ms\ardha chapu\Sample3.wav
frames=946
samplerate=96000
channels=1
num_items=946
Read 946 items

C:\Users\Gumm\Documents\Audacity\10ms\ardha chapu\Sample4.wav
frames=961
samplerate=96000
channels=1
num_items=961
Read 961 items

C:\Users\Gumm\Documents\Audacity\10ms\ardha chapu\Sample5.wav
frames=967
samplerate=96000
channels=1
num_items=967
Read 967 items

C:\Users\Gumm\Documents\Audacity\10ms\ardha chapu\Sample6.wav
frames=915
samplerate=96000
channels=1
num_items=915
Read 915 items

C:\Users\Gumm\Documents\Audacity\10ms\ardha chapu\Sample7.wav
frames=973
samplerate=96000
channels=1
num_items=973
Read 973 items

C:\Users\Gumm\Documents\Audacity\10ms\ardha chapu\Sample8.wav
frames=998
samplerate=96000
channels=1
num_items=998
Read 998 items

C:\Users\Gumm\Documents\Audacity\10ms\ardha chapu\Sample9.wav
frames=1005
samplerate=96000
channels=1
num_items=1005
Read 1005 items

C:\Users\Gumm\Documents\Audacity\10ms\ardha chapu\Sample10.wav
Failed to open the file.

How could I solve this problem?

Edit - I've made it work by renaming all my files as sampleA,sampleB,sampleC and so on and I have made samplenum as character type which iterates from 'a' to 'u'[a=1 and u=21]. But I still want to know why it does not work when I give numbers.

  • Did you try to not open the 10th file and go directly to the 11th? – Brighter side Apr 25 '18 at 07:37
  • 2
    `int sizeofnote; char note[sizeofnote];` is undefined behavior because you are using uninitialized variable. Also it is not a valid c++ code because array size must be known at compile time. And this whole code looks more like a C code. – user7860670 Apr 25 '18 at 07:40
  • @GuillaumeMILAN - yes i have..it doesnt open. – user3147192 Apr 25 '18 at 07:43
  • @VTT - my bad, I will initialize it – user3147192 Apr 25 '18 at 07:44
  • 2
    `strcat(filebasepath, temp1);` causes buffer overrun and undefined behavior as well because there is no storage in `filebasepath` array to append anything. – user7860670 Apr 25 '18 at 07:45
  • what is the reason for this pattern: `temp1[16] = sprintf(temp1, "%s", n);`? – Karsten Koop Apr 25 '18 at 07:49
  • Please [edit] your code to reduce it to a [mcve] of your problem. Your current code includes much that is peripheral to your problem - a minimal sample normally looks similar to a good unit test: only performing one task, with input values specified for reproducibility. – Toby Speight Apr 25 '18 at 10:54
  • @KarstenKoop - that's used for storing a string n ( which actually stores the note of a musical instrument) into temp1. And the length of the biggest note is 16. Hence the size. – user3147192 Apr 25 '18 at 12:07
  • `sprintf` returns the number of characters written. Why would you store this number in `temp1[16]`? Also, `temp1` is shorter than 17 in most of your cases – Karsten Koop Apr 25 '18 at 12:29
  • temp2[13] = sprintf(...). That assignment started to byte when the filename length got unlucky and it overwrote the zero-terminator . The statement makes no sense, just use sprintf(). – Hans Passant Apr 25 '18 at 13:42
  • @KarstenKoop - I'm not sure if sprintf can be used to return number of characters written, but it is used to concatenate a string with other types. The first argument will be the final output of concatenation . See here- https://overiq.com/c-programming/101/the-sprintf-function-in-c/ – user3147192 Apr 25 '18 at 14:28
  • The documentation you linked states "[sprintf] returns the formatted string", which is wrong. The next line even shows it returns an `int`. Even if it returned a string, assigning it to a `char` does not make sense. – Karsten Koop Apr 25 '18 at 14:30

0 Answers0