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.

Dani AI

Generated

Quick checklist of things that commonly make tellg/seekg (or a later read) look like it “works for small files but returns 0” — plus a safer pattern to use in Win32/C++ programs.

First, common root causes seen in GUI/WinAPI apps:

  • The stream never opened (relative paths in GUI apps often point somewhere unexpected). Always test is.is_open() or if (!is) … and prefer full paths or build the path from the executable directory.
  • Undefined behavior elsewhere. In the example above the function is declared char fileio(char *bufferZ) but never returns a value — that alone can produce arbitrary, hard‑to‑trace failures. Make it void or return a meaningful status.
  • Mixing types: use std::streamoff/std::streamsize (or std::filesystem types) rather than int/long so very large files don’t overflow.
  • Be explicit about binary mode: open with std::ios::binary for both size/probing and reading streams.
  • Ensure the destination buffer is allocated to the reported size; otherwise you get silent corruption.

A concise, robust approach (C++17+): get size via std::filesystem and read into a std::vector<char> so you don’t guess sizes or overflow:

#include <filesystem>
#include <fstream>
#include <vector>

auto sz = std::filesystem::file_size(path);
std::vector<char> buf(static_cast<std::size_t>(sz));
std::ifstream is(path, std::ios::binary);
if (!is) /* handle error */;
is.read(buf.data(), static_cast<std::streamsize>(sz));

Extra tips: enable stream exceptions (is.exceptions(...)) while debugging, check is.gcount() after a read, and for WinAPI-specific code you can use CreateFile + GetFileSizeEx to get a guaranteed file size. This set of checks will catch the usual causes that make file-size code appear to fail; see ’s note that the basic tellg approach can work in VC++, which usually means the issue is elsewhere (as later found).

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.