fstream opening binary file

I'm trying to open a binary file (about 700mb) with fstream and flag ios::binary but it always fails.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  // RandomProject.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <fstream>


int _tmain(int argc, _TCHAR* argv[])
{
	
	std::fstream file("C:\\Users\\Name\\Desktop\\file.bin",std::ios::binary);
	if (file.is_open()==false)
		std::cout << "Cannot open file\n";
	else {

		file.seekg(std::ios::end);
		std::cout << file.tellg() << std::endl;
	}


	return 0;
}



When i add ios::in or out flags tellg() returns 2. What's wrong with my code?
Also how would I open a large file that is > 2gb in size?

Thanks
Last edited on
i have been using this and it works for me.

http://www.cplusplus.com/doc/tutorial/files/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// reading a complete binary file
#include <iostream>
#include <fstream>
using namespace std;

ifstream::pos_type size;
char * memblock;

int main () {
  ifstream file ("example.bin", ios::in|ios::binary|ios::ate);
  if (file.is_open())
  {
    size = file.tellg();
    memblock = new char [size];
    file.seekg (0, ios::beg);
    file.read (memblock, size);
    file.close();

    cout << "the complete file content is in memory";

    delete[] memblock;
  }
  else cout << "Unable to open file";
  return 0;
}
Since I'm going to be handling large files it's a bad idea to allocate 700mb of memory. I'm more concerned why it's not opening the file.
Why doesn't fstream work?
Last edited on
There should be some configuration options in the compiler you are using to let the standard library do that.

It may be necessary to recompile your libstdc++ with large file support.

It was my understanding that VS 9.0+ was able to open large files.


But from what you post, your file is opening just fine.

You should be aware, however, that if your program is compiled in 32-bit mode that the file seek and tell functions cannot do anything useful for you, as a 32-bit size_t/ptrdiff_t is not capable of handling such a large file (and, as a result, invalidates some of the invariants that the standard applies to those types).

To get the full size of the file, you'll have to use some Windows API functions directly (specifically, FindFirstFile()), and look at the WIN32_FIND_DATA structure members nFileSizeHigh and nFileSizeLow.


The other option is to use Boost Filesystem, which I believe has LFS support enabled by default on systems that can do it...

Good luck!
try

ifstream file ("C:\\Users\\Name\\Desktop\\file.bin", ios::in|ios::binary|ios::ate);

you didn't specify what you will do with the data. i assumed you would want to manipulate it somehow. wouldn't you need to read it into memory in order to do something with it.


Yes I was aware that the seek cannot return higher than a 32 bit int for me. I guess WinAPI or Boost Filesystem will have to do as you mentioned.

You said my file is opening file but it kept cout'ing cannot open file. Why does it not work with fstream? I tried ifstream and it worked.

1
2
 std::ifstream file("C:\\Users\\Name\\Desktop\\file.bin",std::ios::binary) // Works
 std::fstream file("C:\\Users\\Name\\Desktop\\file.bin",std::ios::binary) // Doesn't open? 
Last edited on
Ok i've read up the file section again (I guess I missed out key info...)


For ifstream and ofstream classes, ios::in and ios::out are automatically and respectively assumed, even if a mode that does not include them is passed as second argument to the open() member function.

The default value is only applied if the function is called without specifying any value for the mode parameter. If the function is called with any value in that parameter the default mode is overridden, not combined.


So when I specify fstream and then ios::binary it's overwritten not combined which is why it fails to open the file right? So that's why I have to include the in and out flags?
Last edited on
Topic archived. No new replies allowed.