решено - файл не записывается в папку - PullRequest
0 голосов
/ 13 июля 2020

Файл example.bmp не создается. Консоль ведет себя должным образом, я добавил отладочные сообщения, которые показывают ход выполнения программы. Появляется предупреждение AVG, и я выбираю «Разрешить приложение», затем использую «f» и возвращаюсь, чтобы закрыть консоль. Заголовок был скопирован из шестнадцатеричного дампа известного рабочего файла .bmp. Вопрос в том, почему это может происходить, файл в папке поврежден, поэтому я не могу получить к нему доступ?

Проблема решена с помощью предложения в последнем комментарии. f.close (); добавлен непосредственно перед cout << "записанный файл bmp" << endl; Я пробовал f.close (); непосредственно перед возвратом (0); и это раньше не работало. </p>

#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;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...