Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How Can I Embed a text file in Code ?

How Can I Embed a text file in Code ?

Scheduled Pinned Locked Moved C / C++ / MFC
question
14 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 0 Offline
    0 Offline
    002comp
    wrote on last edited by
    #1

    Hello Friends I am having one text file storing some constant color data. But I dont want to read it from Disk everytime when I am showing up all these color into Dialog. I need a way that file get embed into project and when I open my color Dialog,it get read tht file and show up all colors. Any Ideas? Regard Yogesh

    C M C J L 5 Replies Last reply
    0
    • 0 002comp

      Hello Friends I am having one text file storing some constant color data. But I dont want to read it from Disk everytime when I am showing up all these color into Dialog. I need a way that file get embed into project and when I open my color Dialog,it get read tht file and show up all colors. Any Ideas? Regard Yogesh

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      there are many ways... the simplest is to store the data as a static array of values:

      const COLORREF clrs[] =
      {
      RGB(0,0,0), RGB(1,2,3), RGB(20,40,60), etc..
      };

      image processing toolkits | batch image processing

      1 Reply Last reply
      0
      • 0 002comp

        Hello Friends I am having one text file storing some constant color data. But I dont want to read it from Disk everytime when I am showing up all these color into Dialog. I need a way that file get embed into project and when I open my color Dialog,it get read tht file and show up all colors. Any Ideas? Regard Yogesh

        M Offline
        M Offline
        Maximilien
        wrote on last edited by
        #3

        Currently, how do you store the data after the file is read ? in an array of "color constants" ? could'nt you simply declare an array in the code and fill it ? for example, we do something like this for color tables:

        static colorStruct colorTable[] =
        {
        "Black", RGB( 0, 0, 0 ),
        "Red", RGB( 255, 0, 0 ),
        "Yellow", RGB( 255, 255, 0 ) ,
        ...
        };

        Watched code never compiles.

        1 Reply Last reply
        0
        • 0 002comp

          Hello Friends I am having one text file storing some constant color data. But I dont want to read it from Disk everytime when I am showing up all these color into Dialog. I need a way that file get embed into project and when I open my color Dialog,it get read tht file and show up all colors. Any Ideas? Regard Yogesh

          C Offline
          C Offline
          Chris Meech
          wrote on last edited by
          #4

          const char ColorData[] = " . . . Embed all your data here . . . . ";

          That might be a start. :)

          Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

          1 Reply Last reply
          0
          • 0 002comp

            Hello Friends I am having one text file storing some constant color data. But I dont want to read it from Disk everytime when I am showing up all these color into Dialog. I need a way that file get embed into project and when I open my color Dialog,it get read tht file and show up all colors. Any Ideas? Regard Yogesh

            J Offline
            J Offline
            Jochen Arndt
            wrote on last edited by
            #5

            With a text file, just embed the content as a text variable to your source file:

            static const char * lpszColorData = "<color data as text>";

            and use the variable like the buffer used so far when reading from the file.

            1 Reply Last reply
            0
            • 0 002comp

              Hello Friends I am having one text file storing some constant color data. But I dont want to read it from Disk everytime when I am showing up all these color into Dialog. I need a way that file get embed into project and when I open my color Dialog,it get read tht file and show up all colors. Any Ideas? Regard Yogesh

              L Offline
              L Offline
              Lakamraju Raghuram
              wrote on last edited by
              #6

              You can also use resources to embed text file into your project. Say the name the text file is data.txt, then in the .rc file add

              IDS_TEXT_DATA RCDATA DISCARDABLE "Data.TXT"

              and in the resource.h give assign appropriate constant to it

              #define IDS_TEXT_DATA 5555

              enhzflepE 0 2 Replies Last reply
              0
              • L Lakamraju Raghuram

                You can also use resources to embed text file into your project. Say the name the text file is data.txt, then in the .rc file add

                IDS_TEXT_DATA RCDATA DISCARDABLE "Data.TXT"

                and in the resource.h give assign appropriate constant to it

                #define IDS_TEXT_DATA 5555

                enhzflepE Offline
                enhzflepE Offline
                enhzflep
                wrote on last edited by
                #7

                That's it! The only answer that actually answers the question as posed. While the other answers achieve (roughly) the same functionality, this is the only answer I'd be satisfied with if it were my question. Nice work. :thumbsup: What if the text file is 2000 lines long and created by some other program? The other answers will all require more work. Admittedly the values will be stored as text, rather than binary - a size penalty, though otherwise this would be my preferred approach. There are pros and cons to each method. Whom are we to assume that we understand the relevance of each of these to the project in question? Let alone give a response that doesn't answer the posed question...

                L 1 Reply Last reply
                0
                • enhzflepE enhzflep

                  That's it! The only answer that actually answers the question as posed. While the other answers achieve (roughly) the same functionality, this is the only answer I'd be satisfied with if it were my question. Nice work. :thumbsup: What if the text file is 2000 lines long and created by some other program? The other answers will all require more work. Admittedly the values will be stored as text, rather than binary - a size penalty, though otherwise this would be my preferred approach. There are pros and cons to each method. Whom are we to assume that we understand the relevance of each of these to the project in question? Let alone give a response that doesn't answer the posed question...

                  L Offline
                  L Offline
                  Lakamraju Raghuram
                  wrote on last edited by
                  #8

                  The only fear I have is of security. If the developer intends to store security critical information in this text file, then I think he is inviting trouble from the hackers/crackers or what ever, as the resource file is wide open for extraction and manipulation. Like pointed rightly by you, it's up to the OP to chose the method :thumbsup:

                  enhzflepE 1 Reply Last reply
                  0
                  • L Lakamraju Raghuram

                    The only fear I have is of security. If the developer intends to store security critical information in this text file, then I think he is inviting trouble from the hackers/crackers or what ever, as the resource file is wide open for extraction and manipulation. Like pointed rightly by you, it's up to the OP to chose the method :thumbsup:

                    enhzflepE Offline
                    enhzflepE Offline
                    enhzflep
                    wrote on last edited by
                    #9

                    It really doesn't make much difference. In fact, with encryption this method could be more secure (difficult and time consuming to reverse - though one can still find/rip and use the decryption function as supplied in the exe along with the encrypted text). There might be a difference of 5-10 mins in understanding the binary data as opposed to the (plain) text version using OllyDbg or (preferably) IDA Pro Advanced. However, I don't see security as a consideration (nor was it mentioned) when storing colour values. :shrugs:

                    0 1 Reply Last reply
                    0
                    • enhzflepE enhzflep

                      It really doesn't make much difference. In fact, with encryption this method could be more secure (difficult and time consuming to reverse - though one can still find/rip and use the decryption function as supplied in the exe along with the encrypted text). There might be a difference of 5-10 mins in understanding the binary data as opposed to the (plain) text version using OllyDbg or (preferably) IDA Pro Advanced. However, I don't see security as a consideration (nor was it mentioned) when storing colour values. :shrugs:

                      0 Offline
                      0 Offline
                      002comp
                      wrote on last edited by
                      #10

                      Hello Friends Thanks EveryOne For their thoughtful consideration for my question. But still I didnt Get my Answer. Till Now, i am reading the file from disk and stroing it into Color Arrays. Directly,i cant assign my RB Values to ColorArray as my File is Having more than 2000 lines and it can be changed later on As it is produced from Third party. And Now,I dont want to keep this file on Disk for Security purpose even. AS i Think,Would it be preferrable If i save color text file into Header file and add it into project and is ther any some way to get data into Structures by using CStdioFile or Some other way ? Any Other Ideas ?? Thanks & Regards Yogesh

                      I 1 Reply Last reply
                      0
                      • L Lakamraju Raghuram

                        You can also use resources to embed text file into your project. Say the name the text file is data.txt, then in the .rc file add

                        IDS_TEXT_DATA RCDATA DISCARDABLE "Data.TXT"

                        and in the resource.h give assign appropriate constant to it

                        #define IDS_TEXT_DATA 5555

                        0 Offline
                        0 Offline
                        002comp
                        wrote on last edited by
                        #11

                        Hello If i Use it like to embed in resource then My Question is that then Do we need text file on Disk when I use exe at other place ? Regards yogesh

                        I 1 Reply Last reply
                        0
                        • 0 002comp

                          Hello Friends Thanks EveryOne For their thoughtful consideration for my question. But still I didnt Get my Answer. Till Now, i am reading the file from disk and stroing it into Color Arrays. Directly,i cant assign my RB Values to ColorArray as my File is Having more than 2000 lines and it can be changed later on As it is produced from Third party. And Now,I dont want to keep this file on Disk for Security purpose even. AS i Think,Would it be preferrable If i save color text file into Header file and add it into project and is ther any some way to get data into Structures by using CStdioFile or Some other way ? Any Other Ideas ?? Thanks & Regards Yogesh

                          I Offline
                          I Offline
                          Iain Clarke Warrior Programmer
                          wrote on last edited by
                          #12

                          Then Lakamraju's answer for you was perfect - The 2rd party just makes a file, which you refer to from your rc resource, and it gets built it. This is less easy that the previous answers to work with, but it satisfies the requirements you've just written (and should have written in the original question - none of us are psychic. Well, maybe Carlo...) Iain.

                          I am one of "those foreigners coming over here and stealing our jobs". Yay me!

                          1 Reply Last reply
                          0
                          • 0 002comp

                            Hello If i Use it like to embed in resource then My Question is that then Do we need text file on Disk when I use exe at other place ? Regards yogesh

                            I Offline
                            I Offline
                            Iain Clarke Warrior Programmer
                            wrote on last edited by
                            #13

                            Resources are built into the executable. You don't need an icon file for your executable to have an icon at another place either. Iain.

                            I am one of "those foreigners coming over here and stealing our jobs". Yay me!

                            0 1 Reply Last reply
                            0
                            • I Iain Clarke Warrior Programmer

                              Resources are built into the executable. You don't need an icon file for your executable to have an icon at another place either. Iain.

                              I am one of "those foreigners coming over here and stealing our jobs". Yay me!

                              0 Offline
                              0 Offline
                              002comp
                              wrote on last edited by
                              #14

                              Thanks Carlo. Regards Yogesh

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups