Thread: binary read of two files getting all "0" zeros.

  1. #1
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638

    binary read of two files getting all "0" zeros.

    have been reading "thinking in c++" to see if i could find out why iostreem is returning all "0" zeros. had #include<fstream> and #include<iostream> . thought it was a header problem. so i added include<cstdio> and #include<cstdlib> . i want to read two files in binary and write one file in binary.

    Code:
    //  sidebysidesecuritycampicture.cpp 
    
    #include<cstdio> 
    #include<cstdlib>   
    #include<fstream>
    #include<iostream>
    
    using namespace std;
    
    
        int    imageone[2000000] ; // first security camera
        int    imagetwo[2000000] ; // second security camera 
        int    imagethree[2000000] ; // sidebysideimages out
    
    int main() {
        int b;  // imageone line in for later use
        int d;  // imagetwo line in for later use
        int e; // line one from image one and line one from image2 together put in later 
    
        ifstream in ("picture1.bmp" , ios::binary ) ;   
        ifstream twoin ("picture2.bmp" , ios::binary) ;
        ofstream out ("sidebysidepicture1.bmp" , ios::binary) ;
    
    printf( "sizeif in == %d " , sizeof(in) );  //debug check file size
     
    // load file imageone.bmp from disk to ram  
       for(int a = 0 ; a < 2000000 ; a++) { 
       in >> imageone[a] ;
       cout << imageone[a] << " " << a << endl ; //debug check what is going to image one in ram
        } // end fill for
    
       for(int a = 0 ; a < 2000000 ; a++) { 
       twoin >> imagetwo[a] ;
       cout << imagetwo[a] << " " << a << endl ; // debug check what is going to image two in ram
        } // end fill for
    
    
         for(int a = 0 ; a < 64  ; a++ ) {
              out << imageone[a];  		// copy header of file picture one
     cout << imageone[a]<< " " << a << endl ;  // check what is going out to file sidebysidepicture1.bmp
               }     
               
        // next part read file 1 then file 2 and writes to sidebysidepicture1.bmp
    ....
    
       for(int a = 65 ; a < 2000000  ; a++ ) {
                out << imagethree[a] ;
                cout << imagethree[a] << " " << a << " " << e << endl ;
                } // end for moved
                
    
    // fclose(in);
    // fclose(twoin);
    // fclose(out);
    
    } // end main
    finished image will be two pictures side by side. will go back finish header edit of image width later. next issue is sizeof(in) return 252 bytes and not 2000000. meow. another issue is i get errors with fclose(). i know iostream closed files for you. thought the file was not closing properly so i put that in there. then added the debug lines to find out that it is not even reading the files. then added ios::binary to the streams.

    it should at least copy the header at this point. have done many times in c but not cpp.

    tia.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I imagine you want to read in the file as an array of bytes (not integers), so you should declare your arrays like this:
    Code:
    const size_t FILESIZE = 2000000;
    unsigned char imageone[FILESIZE];
    ...
    (Are your bmp files exactly 2 million bytes?)

    You don't want to use >> (the "extraction operator") with binary data. Use read like this:
    Code:
    in.read(imageone, FILESIZE);
    fclose is C, not C++. You close your file like this:
    Code:
    in.close();

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I'm sure you have many other problems, but throwing random code at your compiler and expecting the compiler to interpret your intent is your biggest problem with "Thinking in C++" coming in a close second.

    Seriously, this isn't even close to the first time you've posted horribly broken things like this that only implies a idea. STOP TRYING TO PROGRAM BY FLINGING CODE AT YOUR COMPUTER! Get a good book, which "Thinking in C++" is not, and read it while doing every exercise from first to last. (Skipping around is clearly not an option for you.)

    Soma

  4. #4
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    that was not the whole file. it was just a snippet to illustrate the problem. of relevant code. the program is not completely finished. how is that random ? know what i wanted to do and started coding . usually debug with out getting stuck.

    (Are your bmp files exactly 2 million bytes?)
    no but that is why i want to use sizeof(in) to see how large the file is. under c:\ with dir file read about 2meggz 600x800 . i know out need adjusting too.

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You want something like this then:
    Code:
    char   *imageone;
    size_t  len;
    
    // ...
    
    in.seekg(0, ios::end);
    len = in.tellg();
    in.seekg(0, ios::beg);
    imageone = new char[len];
    in.read(buf, len);
    in.close();
    
    // ...
    
    delete [] imageone; // when you're finished with it
    Although for what you seem to want to do you may not actually have to read in the whole file at once. You may be able to process your two files a little at a time (like a scan line at a time, for instance).

  6. #6
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    37 C:\Dev-Cpp\....cpp invalid conversion from `unsigned char*' to `char*'
    37 C:\Dev-Cpp\.....cpp initializing argument 1 of `std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::read(_CharT*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>]'

    68 C:\Dev-Cpp\....cpp invalid conversion from `unsigned char*' to `const char*'
    68 C:\Dev-Cpp\....cpp initializing argument 1 of `std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::write(const _CharT*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>]'

    in.read(imageone, FILESIZE); <== this is line 37
    have several of the top error lines and one of the bottom. i see where it is an unsigned char imageone[FILESIZE] but fail to see how it is converted or tries to convert to char *. lost on what the stuff underneath is trying to say.

    was hoping to read a byte at a time after the header. also was hoping to avoid read() write() . thank you very much. have read the whole book but sometimes forget what it says. i know there are better books also have cpp in 21 days. think i need more sleep. for better retension of data.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by kryptkat View Post
    ... that is why i want to use sizeof(in) to see how large the file is.
    sizeof can't do that. sizeof is only for something whose size is known by the compiler at compile time. It is replaced with a hard-coded number as your program is being built.

    Try using a std::vector to hold the data. Find out the length in bytes of the file, resize the vector to that size, and then read the data directly into the address of the first element in the vector.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    the c way would be to count each byte until eof. like while( in != eof ) that would be the c way so c++ way would be using vector. ok did this
    Code:
      // imageone vector
        
      vector<int> imageone ;
      size_t filesize;
    
      ifstream in ("picture1.bmp",ios::in|ios::ate|ios::binary);
      if (in.is_open())
      {
        filesize=in.tellg();
    
        imageone.reserve(filesize);
    
        in.seekg(0);
        while (!in.eof())
        {
          imageone.push_back( in.get() );
        }
    
        // print out content:
        vector<int>::iterator it;
        for (it=imageone.begin() ; it<imageone.end() ; it++)
          cout << hex << *it;
      }
    did that for both file. however when i did ofstream i got no get() and no tellg() and a few other errors. that is ok i commented out the ofstream. now it read the files. i can work with that. will lookup ofstream to see what to use. now it reads both files.

    how do you release the command line ? it just stick after out putting the hex.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing to "binary" files
    By robquigley in forum C++ Programming
    Replies: 1
    Last Post: 10-03-2003, 12:15 PM
  2. Making Binary Output to files using VC++ 6's "fstream"
    By Perica in forum C++ Programming
    Replies: 5
    Last Post: 02-05-2003, 01:48 AM
  3. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  4. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM
  5. Write and read the book called "registry""
    By CodeMonkey in forum Windows Programming
    Replies: 4
    Last Post: 03-10-2002, 10:45 AM