c++ - 已解决 - 文件未写入文件夹
问题描述
未创建 example.bmp 文件。控制台正常运行,我已经放入了显示程序流程的调试消息。出现 AVG 警告,我选择“允许应用程序”,然后使用“f”并返回以关闭控制台。标头是从已知工作 .bmp 文件的十六进制转储中复制的。问题是为什么会发生这种情况,文件夹中的文件是否已损坏,因此我无法访问它?
最后评论中的建议解决了问题。f.close(); 在 cout << "bmp filewritten"<< endl; 之前添加 我试过 f.close(); 就在返回(0)之前;这以前没有用。
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <ctime>
using namespace std;
int iwidth = 800;
int iheight = 600;
int bmpdata[960000] = {255};//BMP image 800 x 600, RGB values will be all FF black image
char * memblock;
int randomnumber()
{
srand(time(0));
int x=1;
while(x=1);
{
//random number = 1+(rand()%255;
}
}
void createbmp (void)
//this function unwraps 8 bit 8000Hz WAV file of 6700 samples and creates 24 bit bmp file.
//starting bottom left, 8 bit value in array bmpdata[], three columns, same value for each rgb pixel group. Only black or white
//600lpi printer is 28 pixels per mm. 100Mb file size. consider duplicate columns to reduce file size.
//reducing 600lpi resolution impacts multiple of 18 for random dither for half-tone scheme
//WAVE Header is 44 bytes. WAV data is in memory at = *(memblock+(offset+45))
{
cout <<"function createbmp" << endl;
// for (int hposition =0;hposition < (iwidth*3);hposition+3)
//{
// for (int vposition = 0;vposition < iheight;vposition++)
// {
// int offset = ((hposition)+(vposition*iwidth));
//bmpdata[hposition*vposition] = *(memblock+(offset+45));//WAV header is 44 bytes long before data starts!
//bmpdata[(hposition*vposition)+1] = *(memblock+(offset+45));
//bmpdata[(hposition*vposition)+2] = *(memblock+(offset+45));
// }
// }
cout << "loops completed" << endl;
}
//set up for writing output to BMP file
namespace little_endian_io
{
template <typename Word>
std::ostream& write_word( std::ostream& outs, Word value, unsigned size = sizeof( Word ) )
{
for (; size; --size, value >>= 8)
outs.put( static_cast <char> (value & 0xFF) );
return outs;
}
}
using namespace little_endian_io;
int main(int argc, char *argv[])
{
// reading an entire binary file
streampos size;
ifstream file ("D:/Documents and Settings/mark.HERE-F27D04111A/My Documents/lowestoft/test.wav", ios::in|ios::binary|ios::ate);
if (file.is_open())
{
size = file.tellg();
memblock = new char [size];
file.seekg (0, ios::beg);
file.read (memblock, size);
file.close();
cout << "the entire file content is in memory"<< endl;
cout << "filesize = ";
cout << size << endl;
createbmp();//this is where my function to convert WAV file to BMP is called
cout << "bmp created"<< endl;
delete[] memblock;
}
else cout << "Unable to open file"<< endl;
char quit;
quit = '\0';
while (quit != 'q')
{
cout << "Press q to quit " << endl;
cin >> quit;
}
ofstream f( "D:/Documents and Settings/mark.HERE-F27D04111A/My Documents/lowestoft/example.bmp", ios::binary );
// Write the file headers
write_word( f, 66, 1 ); // File Type , 1st of 2 bytes, 'BM' default values ascii M is 77, B is 66. 16973 as 2 byte decimal
write_word( f, 77, 1 ); // File Type , 2nd of 2 bytes, 'BM' default values
write_word( f, 960056, 4 ); // File Size, 4 bytes
write_word( f, 0, 2 ); // reserved
write_word( f, 0, 2 ); // reserved
write_word( f, 54, 4 ); // Pixel Data Offset, 4 bytes
write_word( f, 40, 4 ); // Header Size, 4 bytes
write_word( f, 800, 4 ); // Image Width, 4 bytes
write_word( f, 400, 4 ); // Image Height, 4 bytes
write_word( f, 1, 2 ); // Planes , 2 bytes
write_word( f, 24, 2 ); // Bits Per Pixel, 2 bytes
write_word( f, 0, 4 ); // Compression, 4 bytes
write_word( f, 0, 4 ); // Image Size, 4 bytes
write_word( f, 2834, 4 ); // XPixelsPerMetre, 4 bytes
write_word( f, 2834, 4 ); // YPixelsPerMetre, 4 bytes
write_word( f, 0, 4 ); // Total Colours, 4 bytes
write_word( f, 0, 4 ); // Important Colours, 4 bytes
// Write the bmp RGB values
//
for (int n = 0; n < 960000; n++)
{
write_word( f, (int)(bmpdata[n]), 1 );
}
cout << "bmp file written"<< endl;
while (quit != 'f')
{
cout << "Press f to quit " << endl;
cin >> quit;
}
return 0;
}
解决方案
推荐阅读
- class - 如何在 Jenkins Pipeline 中导入具有常量的类?
- c - 如何动态分配结构而不是它的属性?
- python-3.x - Python 搜索功能
- node.js - 当从浏览器调用 API 时,自定义请求标头被删除
- node.js - 如何将时间戳数据类型从 Firestore 复制到 Cloud SQL Postgres?
- node.js - 使用 mongoose 和 node express 查找一个集合中的所有文档,更改数据并保存到另一个集合中
- javascript - 传单从地图单击创建标记并访问拖动事件的结束
- javascript - 在嵌套对象中查找任何错误值的更好的 vanilla JS 方法?
- r - 通过添加 0 和 1 的新列,为 R 中列中的值或因子的第一个实例返回 1,为所有其他后续实例返回 0
- typescript - 如何在我的 Typescript Vue 组件中启用 chrome 类型的自动完成功能?