Thread: Weird problem using ifstream / rdbuf

  1. #1
    Registered User
    Join Date
    Apr 2004

    Weird problem using ifstream / rdbuf


    I'm trying to read in large amounts of data using the rdbuf member of ifstream, it works fine as long as the bufferSize I pass into rdbuf as a parametre is an even number, but if its an odd number I get a completely wrong set of data from the file. Ill show the code and the results i'm getting.

    unsigned int bufferSize = 20;
    size_t searching = bufferSize;
    char *bufferPrimary = new char[bufferSize + 1];	
    std::ifstream fin(filename->c_str(), std::ifstream::in);
    	return false;
    	fin.rdbuf()->pubsetbuf(bufferPrimary, bufferSize);
    	searching = fin.rdbuf()->_Sgetn_s(bufferPrimary, bufferSize, bufferSize);
    	bufferPrimary[bufferSize] = fin.widen('\0');
    	std::cout << "Using set: " << bufferPrimary << std::endl;
    Data in file is: LDHGSJDHFKJASTEVEDHSFDLHSFDHSFAHDSVICTORKFHDSAKJFH (file goes on but this is the start)

    If bufferSize is 20 I get: LDHGSJDHFKJASTEVEDHS (this seems correct)
    If bufferSize is 21 I get: FDLHSFDHSFAHDSVICTORF (this is not the start of the file... and I dont know where the F on the end comes from)

    Is this because I may be using pubsetbuf wrong? I used it because I wanted to limit the buffer size read in.
    If anyone could tell me what i'm doing wrong I would be very greatful, thanks.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    _Sgetn_s is a non-standard member. Who knows what it does, or if it will change in the future. Call sgetn() instead.

    You shouldn't call pubsetbuf() on a stream buffer you didn't implement. The effects of calling this are implementation defined - except that pubsetbuf(0,0) will cause the file stream to become unbuffered. Just let the stream do its buffering.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Weird problem
    By eurus in forum C Programming
    Replies: 8
    Last Post: 02-17-2006, 01:35 PM
  2. Weird Problem?
    By Blackroot in forum C++ Programming
    Replies: 8
    Last Post: 01-27-2006, 01:43 AM
  3. Weird problem on '02 3.4L V6 auto
    By VirtualAce in forum A Brief History of
    Replies: 8
    Last Post: 01-12-2006, 12:05 AM
  4. Really Weird itoa Problem
    By Grantyt3 in forum C++ Programming
    Replies: 8
    Last Post: 12-20-2005, 12:44 AM
  5. weird problem with code, not displaying what it should
    By chris285 in forum C++ Programming
    Replies: 2
    Last Post: 01-11-2005, 11:04 AM