У меня есть код C ++. Я пытаюсь открыть около 20 файлов WAV, используя этот код. Но когда он попадает в 10-й файл, он перестает открывать и читать файлы? В чем проблема? У меня 20 файлов WAV хранятся на моем компьютере по определенному пути. Он отлично работает для файлов с номерами от 1 до 9, но он не открывается из файла с 10 по 20. Я уверен, что мой путь к файлу правильный, который я даю функции libsnd open file.
#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;
}
Это мой вывод
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.
Как я мог решить эту проблему?
Редактировать - я заставил его работать, переименовав все мои файлы в sampleA, sampleB, sampleC и т. Д., И я сделал samplenum как тип символов, который перебирает от 'a' до 'u' [a = 1 и u = 21]. Но я все еще хочу знать, почему это не работает, когда я даю цифры.