473,794 Members | 2,752 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ifstream::tellg () error

I am reading in image files in a program and I read in
the header in ascii mode and the data in binary mode.
The problem is, sometimes tellg() gives me a completely
incorrect result and sometimes it is just fine. It is
quite annoying because there is no other good way to read
these file and I think it is a problem with Visual C++,
since it works just fine when I compile it using g++ on a
Linux system. Here is an example:

char input[200];
std::ifstream fin("blocks.pgm ", std::ios::in);
std::ifstream:: pos_type position = fin.tellg();
fin>>input;
position = fin.tellg();

When I run this code, input gets the first two bytes of
the file as it should. The first few bytes of the file
are: 'P5 302 274 255 '. Now, even though it only read
two bytes into input, when tellg() is called on the next
line it returns a position of 3586 bytes into the file.

If I change the input file to "original.p gm", a different
image file, everything works fine. The first 15 bytes
are identical and after the first two bytes are read,
tellg() returns a position of 2 bytes into the file.

If I delete everything in blocks.pgm after the first 15
bytes it seems to work just fine. I don't understand how
bytes that should not be read could be affecting the tellg
() function.

Chris

Nov 16 '05 #1
1 4354
Chris wrote:
I am reading in image files in a program and I read in
the header in ascii mode and the data in binary mode.
The problem is, sometimes tellg() gives me a completely
incorrect result and sometimes it is just fine. It is
quite annoying because there is no other good way to read
these file and I think it is a problem with Visual C++,
since it works just fine when I compile it using g++ on a
Linux system. Here is an example:

char input[200];
std::ifstream fin("blocks.pgm ", std::ios::in);


You're not using standard IOStreams, but anyway - add std::ios::binar y to
the flags.

-cd
Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3739
by: hall | last post by:
Hi. I ran across a bug in one of my problems and after spending some time tracking it down i found that the problem arose in a piece of code that essentially did this: ----------- ifstream in("in.txt"); if(!in) {cout << "error\n";} in.close(); if(!in) {cout << "error\n";} in.close(); if(!in) {cout << "error\n";}
6
8347
by: Eivind Grimsby Haarr | last post by:
I know that this has been posted before on several other newsgroups, but I need to make sure I got this right, so I hope you can forgive me for posting this. In MVSC6.0, and also in several Borland c++ compilers from what I can see from newsgroup postings, ifstream::tellg() alters the position of the file reading pointer when reading UNIX files (only LF character, not CRLF) in text mode. I can see why it does this, keeping consistency...
3
1268
by: Jason | last post by:
I wrote a piece of code that reads read in about 1000 int values from a text file. I put a cout statement to verify that the input in the code was working as anticipated. It seems to work fine until some point when it just ouputs 0's. So I then took the code and made a app that just read the file and it worked fine. Does anyone have a suggestion. Jason
3
9188
by: jois.de.vivre | last post by:
Hi, I'm trying to write to an ofstream, and for some reason it fails. I know I can check it with fail() or bad(), but it gives me no useful information as to why it fails. Are there any C++ functions that do this or is there any way I can check manually? Thanks, Prashant
7
5633
by: Hamburgpear | last post by:
Dear All, Is it possible to reset the value of xxx.peek() after it reachs EOF ? Regards HP
0
1797
by: Chris | last post by:
I am reading in image files in a program and I read in the header in ascii mode. The problem is, sometimes tellg () gives me a completely incorrect result and sometimes it is just fine. Here is an example: char input; std::ifstream fin("blocks.pgm", std::ios::in); std::ifstream::pos_type position = fin.tellg(); fin>>input; position = fin.tellg();
7
3914
by: zhouchengly | last post by:
when I use the following code to get integer values from file: void getIntegers() { vector<ItemType items; ifstream ifs("test.dat"); //¿¼ÂÇΪitemsÒ»´Î·ÖÅä¿Õ¼ä¡£ ItemType item ; while( !ifs.eof() ) { ifs>>item;
3
7242
by: toton | last post by:
Hi, I want to unread a few things while I am reading a file. One solution I know is putback the characters read to the buffer. But as I need frequent moving file pointer , to a few steps back, I was thinking of seekg & tellg function. But it is not workings as I expect.... here is the test code, std::string word; std::string value;
2
5759
by: im2cre8iv | last post by:
Here is my code where I am receiving the error: #include <fstream> #include "BinaryTree.h" using namespace std; Node* BinaryTree::MakeTree(ifstream& infile) { char name; infile>>name;
0
9518
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10433
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10212
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10161
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9035
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
3720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2919
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.