Hello!

I'm trying to get the contents of a binary file into a char array, my read function is in main.cpp, in the header file is referenced as char fileio(char * bufferZ) , and is called in another .cpp file.
The problem is , the code in the function cant get the length of files that are larger than about 10 KB.The length gets to 0.

The read file function is:

char fileio(char * bufferZ){

  // get file size method 1      
  long begin,end;
  ifstream myfile ("1.im");
  begin = myfile.tellg();
  myfile.seekg (0, ios::end);
  end = myfile.tellg();
  myfile.close();

   ifstream is;
   int length;
   is.open ("1.im", ios::binary );

   // get file size method 2
  is.seekg (0, ios::end);
  length = is.tellg();
  is.seekg (0, ios::beg);


  //is.read (bufferZ,length);
  is.read (bufferZ, (end-begin)  );

  is.close();   
}  

I'm using this in a winapi program.

Recommended Answers

All 2 Replies

I don't know what compiler you are using, but this works (your second attempt) with vc++ 2012 Express. Maybe the problem is caused by something else in your program.

#include <iostream>
#include <fstream>
int main()
{
    long length;
    std::ifstream is("l.2");
    is.seekg (0, std::ios::end);
  length = is.tellg();
  std::cout << length << '\n';
  is.close();   

}

Thank you, yes the problem was from somewhere else.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.