0

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.

Edited by dot_binary

2
Contributors
2
Replies
3
Views
4 Years
Discussion Span
Last Post by dot_binary
0

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();   

}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.