[C++-sig] Strange Behavior w/File Pointers

Raoul Gough RaoulGough at yahoo.co.uk
Fri Mar 7 19:11:07 CET 2008


Michael Wieher wrote:
> mYmodule.cpp
> ---------------------------
>
> istream globalFile;
> struct dataStruct {
>    int a;
>    int b;
>    short c;
> } __attribute__(align(2));
>
> PyObject *
> mYmodule_setFile() {
>     --parseTuple--
>     globalFile.open(pathToFile,ios::in|ios::binary);
> }
>
> PyObject *
> mYmodule_getNBytesAtATime() {
>   --parseTuple--
>   int N;  //the size of a dataStruct
>   globalFile.read((char*)&dataStruct,N);
>   Py_BuildValue('ii',dataStruct->a, dataStruct->b);
> }
>
> ==============
> Given the above rough pseudocode, I find that each time my Python 
> module calls myModule.getNBytesAtATime() the file-pointer shifts 
> strangely.  Most of the time it shifts N bytes.  But, sometimes, on 
> the same points but with no definable reason that I can see, it shifts 
> N*x bytes, where x is some integer.

Are you sure this happens in a single call to the C++ function? If I 
suspected something like what you describe I would probably "instrument" 
the C++ function by having it log something to stdout or stderr, e.g.

std::cout << "Getting " << N << " bytes starting at " << 
globalFile.tellg() << std::endl;
....
std::cout << "Got " << N << " bytes, now at " << globalFile.tellg() << 
std::endl;

(not compiled or tested!). This should show you if the function itself 
is moving the pointer unexpectedly, or if it's happening somewhere else...

-- 
Cheers,
Raoul.




More information about the Cplusplus-sig mailing list