【问题标题】:c++ ifstream read can't store into short array bufferc ++ ifstream读取无法存储到短数组缓冲区中
【发布时间】:2018-11-12 07:37:02
【问题描述】:

我想将 ifstream 中的数据存储到短数组中,

但它在打印出运行 2 之前崩溃了

short *Data;
// 88200 / (16/8) = 44100

size_t sdata_size = wavHeader.data_size /(wavHeader.bits_per_sample/8);

Data = new short [sdata_size];

std::cout << "run1" << std::endl;

in.read ((char*)&Data,sdata_size);

std::cout << "run2" << std::endl;

好的,正如 Alan Birtles 指出的那样,我做了一些更正,

short *Data;
// 88200 / (16/8) = 44100
Data = new short [wavHeader.data_size/(wavHeader.bits_per_sample/8)];

std::cout << "run1" << std::endl;

in.read ((char*)Data,wavHeader.data_size /(wavHeader.bits_per_sample/8));

std::cout << "run2" << std::endl;

in.close();

// this start point at first element and then increment to next array
short *ptr1 = Data;
// this start at the end element and then decrement to the next,
//- minus 1 for last element
 short *ptr2 = Data 
+(wavHeader.data_size/(wavHeader.bits_per_sample/8)) - 1;

for (; ptr1 < ptr2; ++ptr1, --ptr2)
{
  short tmp = *ptr1;
  *ptr1 = *ptr2;
  *ptr2 = tmp;
}
out.write ((char*)Data,wavHeader.data_size /(wavHeader.bits_per_sample/8));

// clean up the new
delete [] Data;

我想要实现的是我想反转 wav 音频数据并将其写入另一个 wav 文件。但是输出是错误的任何想法为什么?

预期结果 enter image description here

我的结果

enter image description here

【问题讨论】:

  • ok 应该是 in.read ((char*)Data,wavHeader.data_size );并解决问题非常感谢

标签: c++ arrays pointers ifstream


【解决方案1】:

(char*)&amp;Data 应该只是 (char*)Data 并且您应该创建一个大小为 sdata_size / sizeof( short ) 的数组(假设 sdata_size 是 2 的倍数)。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2011-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多