0

I have this code in c++ ( it is after I did some tests to see why I can not read enough data from file, so it is not final code and I am looking to find why I am getting this result)

size_t readSize=629312;
_rawImageFile.seekg(0,ifstream::end);
size_t s=_rawImageFile.tellg();
char *buffer=(char*) malloc(readSize);
_rawImageFile.seekg(0);
int p=_rawImageFile.tellg();
_rawImageFile.read(buffer,readSize);
size_t extracted = _rawImageFile.gcount();
cout << "s="<< s <<endl;
cout << "p="<< p <<endl;
cout << "readsize="<< readSize<<endl;
cout << "extracted="<< extracted <<endl;
cout << "eof ="<< _rawImageFile.eofbit<<endl;
cout << "fail="<< _rawImageFile.failbit <<endl;

The output is as follow:

s=3493940224
p=0
readsize=629312
extracted=2085
eof =1
fail=2

As you can see the file size is 3493940224 and I am at the start of file (p=0) and I am trying to read 629312 bytes, but I can only read 2085?

What is the problem with this code? I did open this file in other methods and read some data out of it, but am using seekg to move pointer to the beginning of file.

The file was opened as binary.

edit 1

To find a solution, I put all code inside a function and here is it:

    _config=config;
    ifstream t_rawImageFile;
    t_rawImageFile.open(rawImageFileName,std::ifstream::in || std::ios::binary );
    t_rawImageFile.seekg (0);
    size_t readSize=629312;
    t_rawImageFile.seekg(0,ifstream::end);
    size_t s=t_rawImageFile.tellg();
    char *buffer=(char*) malloc(readSize);
    t_rawImageFile.seekg(0);
    size_t p=t_rawImageFile.tellg();
    t_rawImageFile.read(buffer,readSize);
    size_t x=t_rawImageFile.tellg();
    size_t extracted = t_rawImageFile.gcount();
    cout << "s="<< s <<endl;
    cout << "p="<< p <<endl;
    cout << "x="<< x <<endl;
    cout << "readsize="<< readSize<<endl;
    cout << "extracted="<< extracted <<endl;
    cout << "eof ="<< t_rawImageFile.eof()<<endl;
cout << "fail="<< t_rawImageFile.fail() <<endl;

and the result is:

s=3493940224
p=0
x=4294967295
readsize=629312
extracted=2085
eof =1
fail=1

Interestingly, after read the file pointer moves to a very big value. is it possible that since the file size is very big, the application fails?

edit 2

Tested the same code with another file. the result is as follow:

s=2993007872
p=0
x=4294967295
readsize=629312
extracted=1859
eof =1
fail=1

What I can read from this test is that: after read the file pointer moves to a big number which is always the same. The amount that it reads depend on file (!).

edit 3

After changing the size_t to fstream::pos_type the result is as follow:

s=2993007872
p=0
x=-1
readsize=629312
extracted=1859
eof =1
fail=1

Why file position goes to -1 after a read?

12
  • 1
    Where and how do you set readSize? Also, what is the stream state (fail/eof/etc)? Commented May 31, 2013 at 11:10
  • 1
    Unrelated to your problem, but why do you use malloc in a C++ program? Commented May 31, 2013 at 11:12
  • 2
    C++ is a multi-paradigm language. It is C++ code. (question remains how good the code is) Commented May 31, 2013 at 11:15
  • 4
    @not-sehe: Since when did C have class member functions and overloaded operators? This is C++, even if it doesn't use the idioms you prefer. Commented May 31, 2013 at 11:22
  • 1
    I can't see any other obvious reason than that something is truncating your file while you have it open, so your tellg() shows a location that's where you seekg'd to before truncation. Commented May 31, 2013 at 11:25

3 Answers 3

2
t_rawImageFile.open(rawImageFileName, std::ifstream::in || std::ios::binary );

...does not open the file in binary mode. Since || is the lazy or operator and std::ifstream::in is non zero, the whole expression has the value 1.

t_rawImageFile.open(rawImageFileName, std::ifstream::in | std::ios::binary );

...will surely work better.

Sign up to request clarification or add additional context in comments.

Comments

1

You don't show the part where your file is being opened, but I'm pretty sure it is missing ios::binary to make sure the C runtime code doesn't interpret CTRL-Z (or CTRL-D) as end of file.

2 Comments

the file opened in this way: _rawImageFile.open(rawImageFileName,std::ifstream::in || std::ifstream::binary);
Then something else is wrong. Without access to your file and your environment, it's hard to say what.
1

Change this line:

t_rawImageFile.open(rawImageFileName,std::ifstream::in || std::ios::binary );

into this:

t_rawImageFile.open(rawImageFileName,std::ifstream::in | std::ios::binary );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.