ifstream binary file reading

Mar 18, 2009 at 4:49pm
Hi!
Im new to cpp, and i have problem with reading binary file
My code is:
1
2
3
4
5
6
7
8
	std::ifstream ifs(L"c:\\james.rar");
	char buff[1000];
	ifs.seekg (0, std::ios::beg);
	int count = 0;
	while(!ifs.eof()) {
		ifs.getline(buff, 1000);
		std::cout << count++ << std::endl;
	}


code stops at first iteration :( whats wrong with my code ? i cant read this file with one "read" call because its too big, so i need to read with buff to send via ethernet.

Thanks!
Mar 18, 2009 at 4:54pm
oh... made changes to code
 
std::ifstream ifs(L"c:\\james.rar", std::ios::binary);

so now my while never stops :) lol... how to check eof in binary mode ? My OS is Win XP.
Mar 18, 2009 at 5:35pm
this is the only way to find eof in any file. eof gives correct results for binary stream also..
this should not happen..code also looks fine..

what you can try is stop fetching data when the getline function returns empty string.
Mar 18, 2009 at 8:40pm
strange, but when i change getline to read it work fine:
1
2
3
4
5
6
7
8
	std::ifstream ifs(L"c:\\james.rar", std::ios::binary);
	char buff[1000];
	ifs.seekg (0, std::ios::beg);
	int count = 0;
	while(!ifs.eof()) {
		ifs.read(buff, 1000);
		std::cout << count++ << std::endl;
	}


Tanks for help!
Mar 19, 2009 at 2:01am
Read in your case is storing the first 1000 characters into the buffer buff. It is performing the same thing as a getline statement in a sense.

Also read and write are used for binary i/o I don't know if getline will handle binary code.
Last edited on Mar 19, 2009 at 2:02am
Mar 19, 2009 at 8:38am
strange..!!
Topic archived. No new replies allowed.