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. file to array

file to array

Scheduled Pinned Locked Moved C / C++ / MFC
iosdata-structuresquestion
9 Posts 4 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.
  • N Offline
    N Offline
    nmx_de
    wrote on last edited by
    #1

    Hello all, i want to be able to read any file in binary mode and put the files content into a char array, but it just won't work except of text files. How can this be done? Thanks in advance char* Files::ReadFile(const char* file) { ifstream::pos_type size; char* file_array; ifstream file (file, ios::in|ios::binary|ios::ate); if (file.is_open()) { size = file.tellg(); file_array = new char[size]; file.seekg (0, ios::beg); file.read (file_array, size); file.close(); return file_array; } return "Unable to open file"; }

    C P 2 Replies Last reply
    0
    • N nmx_de

      Hello all, i want to be able to read any file in binary mode and put the files content into a char array, but it just won't work except of text files. How can this be done? Thanks in advance char* Files::ReadFile(const char* file) { ifstream::pos_type size; char* file_array; ifstream file (file, ios::in|ios::binary|ios::ate); if (file.is_open()) { size = file.tellg(); file_array = new char[size]; file.seekg (0, ios::beg); file.read (file_array, size); file.close(); return file_array; } return "Unable to open file"; }

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      nmx_de wrote:

      but it just won't work

      Please, when you have a problem, explain it clearly. " It does't work" is very unclear, it can be anything. We cannot guess what the problem is just by looking at the screen.


      Cédric Moonen Software developer
      Charting control

      N 1 Reply Last reply
      0
      • C Cedric Moonen

        nmx_de wrote:

        but it just won't work

        Please, when you have a problem, explain it clearly. " It does't work" is very unclear, it can be anything. We cannot guess what the problem is just by looking at the screen.


        Cédric Moonen Software developer
        Charting control

        N Offline
        N Offline
        nmx_de
        wrote on last edited by
        #3

        Sorry for that. Thought it would be clear as i might not be the only one ever who tried to read a file into an array. When I read a text file, I get the content of the file properly. When I read another file (for example pdf), i only get an output like this: %PDF-1.5 $?? Maybe it's an encoding problem, because the string is follow by some special characters. (Took a look at it in UltraEdit) %PDF-1.5 %âãÏÓ 149 0 obj < Is it possible only to read the binary value of the file? Regards, n.

        P C E 3 Replies Last reply
        0
        • N nmx_de

          Hello all, i want to be able to read any file in binary mode and put the files content into a char array, but it just won't work except of text files. How can this be done? Thanks in advance char* Files::ReadFile(const char* file) { ifstream::pos_type size; char* file_array; ifstream file (file, ios::in|ios::binary|ios::ate); if (file.is_open()) { size = file.tellg(); file_array = new char[size]; file.seekg (0, ios::beg); file.read (file_array, size); file.close(); return file_array; } return "Unable to open file"; }

          P Offline
          P Offline
          Parthi_Appu
          wrote on last edited by
          #4

          I hope you are getting some junk value... in file_array for non text files.. if so, its obvious... because you are trying to view char value which is not char... But for text files the data are in the form of char

          Do your Duty and Don't expect the Result

          1 Reply Last reply
          0
          • N nmx_de

            Sorry for that. Thought it would be clear as i might not be the only one ever who tried to read a file into an array. When I read a text file, I get the content of the file properly. When I read another file (for example pdf), i only get an output like this: %PDF-1.5 $?? Maybe it's an encoding problem, because the string is follow by some special characters. (Took a look at it in UltraEdit) %PDF-1.5 %âãÏÓ 149 0 obj < Is it possible only to read the binary value of the file? Regards, n.

            P Offline
            P Offline
            Parthi_Appu
            wrote on last edited by
            #5

            thats right... if you write the file_array as that type of file say 1.pdf then if u open the file then you can get the excat data...

            Do your Duty and Don't expect the Result

            N 1 Reply Last reply
            0
            • N nmx_de

              Sorry for that. Thought it would be clear as i might not be the only one ever who tried to read a file into an array. When I read a text file, I get the content of the file properly. When I read another file (for example pdf), i only get an output like this: %PDF-1.5 $?? Maybe it's an encoding problem, because the string is follow by some special characters. (Took a look at it in UltraEdit) %PDF-1.5 %âãÏÓ 149 0 obj < Is it possible only to read the binary value of the file? Regards, n.

              C Offline
              C Offline
              Cedric Moonen
              wrote on last edited by
              #6

              What are you trying to do exactly ? And what is the purpose ? It's normal that if you try to display the contents of a binary file, you will have junk characters. That's simply because the purpose of a binary file is not to be readable (by a human I mean). So, what did you expect ?


              Cédric Moonen Software developer
              Charting control

              1 Reply Last reply
              0
              • P Parthi_Appu

                thats right... if you write the file_array as that type of file say 1.pdf then if u open the file then you can get the excat data...

                Do your Duty and Don't expect the Result

                N Offline
                N Offline
                nmx_de
                wrote on last edited by
                #7

                Wow, seems to be one of my bad hair days. I still missed to provide some information. I don't want to have a pdf file (or any other binary file) as a result. I only want to get the binary representation of the file. And this has to be some kind of string or char array.

                C 1 Reply Last reply
                0
                • N nmx_de

                  Wow, seems to be one of my bad hair days. I still missed to provide some information. I don't want to have a pdf file (or any other binary file) as a result. I only want to get the binary representation of the file. And this has to be some kind of string or char array.

                  C Offline
                  C Offline
                  Cedric Moonen
                  wrote on last edited by
                  #8

                  nmx_de wrote:

                  I only want to get the binary representation of the file. And this has to be some kind of string or char array.

                  No, a binary file contains junk characters and if you try to display it, you will have junk characters on the screen. Try to open the file with notepad and you will see what's inside. And what do you mean by 'binary representation' ?


                  Cédric Moonen Software developer
                  Charting control

                  1 Reply Last reply
                  0
                  • N nmx_de

                    Sorry for that. Thought it would be clear as i might not be the only one ever who tried to read a file into an array. When I read a text file, I get the content of the file properly. When I read another file (for example pdf), i only get an output like this: %PDF-1.5 $?? Maybe it's an encoding problem, because the string is follow by some special characters. (Took a look at it in UltraEdit) %PDF-1.5 %âãÏÓ 149 0 obj < Is it possible only to read the binary value of the file? Regards, n.

                    E Offline
                    E Offline
                    earl
                    wrote on last edited by
                    #9

                    What did you think you were going to get? That's what a binary file is. Open it in any hex reader (or visual studio -- do file->open and select "Open As" = binary). earl

                    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