Thread: Instead of using resources to embed a file in another, help

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    319

    Instead of using resources to embed a file in another, help

    Instead of using windows resources, i would like to embed file data in an array instead inside the application, when it comes to adding the new .h that was created i am presented with a big error list which i have attached to the post, thanks for reading

    Code:
    #include "stdafx.h"
    #include <Windows.h>
    #include <string>
    #include <fstream>
    #include <vector>
    #include <cstdint>
    #include <istream>
    #include <algorithm>    // std::copy
    #include <iterator>     // std::back_inserter
    #include <stdlib.h>
    
    using namespace std;
    
    int main(int, char* argv[])
    {
       std::ifstream i("texture.jpg", std::ios::binary);
       std::ofstream o("bytesHeader.h");
    
       
       std::vector<std::uint8_t> buff;
       std::copy(std::istreambuf_iterator<char>(i), std::istreambuf_iterator<char>(),
           std::back_inserter(buff));
        
       o << "const char charArray[] = {\n";
           
       for (size_t i=0; i<buff.size(); ++i)
       {
          o << "0x" << std::hex << buff[i];
          if (i % 21) o << "\n";
          if (i < buff.size() + 1) o << ",";
       }
       
       o << "};\n";
       
      // system("pause");
       o.close();
       i.close();
       return 0;
    }
    Code:
    #include "stdafx.h"
    #include <Windows.h>
    #include "bytesHeader.h"
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	return 0;
    }
    Attached Files Attached Files

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Have you bothered to look at "bytesHeader.h" to see what it contains?

    Perhaps you could test with a much smaller file, say something with only 10 bytes in it.
    Then your "bytesHeader.h" wouldn't be a massive file, creating massive amounts of error messages.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> if (i < buff.size() + 1) o << ",";

    That condition will always be true, so...

    >> if (i % 21) o << "\n";

    You probably want to be checking for a remainder of zero there, don't you think?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    319
    Instead ive gone with a differenet aproach,that is to load the file in a hex editor and select all the data , click copy to c and paste that array to the header,but when i open the new image file the picture is blurry and corrupted

    Code:
    #include <Windows.h>
    #include "Data.h"
    #include <fstream>
    using namespace std;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        std::fstream fs;
      fs.open ("pic.jpg", std::fstream::out,std::fstream::binary);
    
      fs.write((const char*)rawData,130591);
    
      fs.close();
    	return 0;
    }
    
    
    //The char array looks like this inside Data.h
    
    unsigned char rawData[130591] = {
    	0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10, 0x4A, 0x46, 0x49, 0x46, 0x00, 0x01,
    	0x01, 0x01, 0x00, 0x60, 0x00, 0x60, 0x00, 0x00, 0xFF, 0xDB, 0x00, 0x43,
    	0x00, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02, 0x02,
            to big to put here

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    319
    NVM i got t working, it should be std::fstream:ut | std::fstream::binary

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> fs.open ("pic.jpg", std::fstream::out,std::fstream::binary)
    The open mode goes in the second parameter only.

    gg

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>fs.write((const char*)rawData,130591);
    fs.write((const char*)RawData, sizeof(RawData) / sizeof(RawData[0]));
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    319
    erm i may have to go back to the original plan , as i cant do this with large files
    so i still need help fixing the first code i originally pasted

  9. #9
    Registered User
    Join Date
    Nov 2002
    Posts
    319
    ive added if (i % 21 == 0) o << "\n";

    //output to the header looks like this,which dosent look right
    const char charArray[] = {
    0xM
    ,0xZ,0x

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > //output to the header looks like this,which dosent look right
    Finally, getting somewhere.

    > o << "0x" << std::hex << buff[i];
    Do you know what type buff[i] is?
    Do you know what << does with values of that type?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to embed a file in a C program
    By thetinman in forum C Programming
    Replies: 1
    Last Post: 01-20-2014, 07:50 PM
  2. Loading Text File Resources
    By Brokenhope in forum Windows Programming
    Replies: 4
    Last Post: 10-11-2010, 04:32 AM
  3. Gtk Plug, Socket, embed one program into another.
    By arkashkin in forum Linux Programming
    Replies: 2
    Last Post: 03-27-2010, 01:01 AM
  4. Programmatically add resources to exe file
    By Devils Child in forum C# Programming
    Replies: 15
    Last Post: 12-06-2009, 01:22 PM
  5. Storing resources into a single file
    By LuckY in forum Game Programming
    Replies: 20
    Last Post: 08-14-2004, 11:28 PM