Weird problem using ifstream / rdbuf

This is a discussion on Weird problem using ifstream / rdbuf within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm trying to read in large amounts of data using the rdbuf member of ifstream, it works fine as ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    28

    Weird problem using ifstream / rdbuf

    Hi,

    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.

    Code:
    ...
    
    unsigned int bufferSize = 20;
    size_t searching = bufferSize;
    char *bufferPrimary = new char[bufferSize + 1];	
    std::ifstream fin(filename->c_str(), std::ifstream::in);
    
    if(!fin.is_open())
    {
    	fin.close();
    	return false;
    }
    else
    {
    	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
    Posts
    4,660
    _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.

    gg

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21