473,408 Members | 1,874 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

why won't this work after 158th position? get()

#include "stdafx.h"
#include "stdafx.h"
#include<iostream>
#include <fstream>

using namespace std;

using std::cout;

int main(int argc, char* argv[])
{
ifstream infile ("b.bmp");
for (int n=0; n<200;n++)
{

int c= infile.get();

if ((n>145)&&(n<165))//print the interesting bit
cout<<c<<" ";
}
return 0;
}

I thought the output would be
23 23 23 0 24 24 24 0 25 25 25 0 26 26 26 0 27 27 27

but it is
23 23 23 0 24 24 24 0 25 25 25 0 -1 -1 -1 -1 -1 -1 -1
b.bmp is a 2k file
from n

Why won't infile.get() work beyond the 157th place?

Thanx
Tony

Jul 23 '05 #1
14 1250
to*******@aol.com wrote:
#include "stdafx.h"
#include "stdafx.h"
Twice? Please consider weeding out compiler-specific nonsense
when posting there.
#include<iostream>
#include <fstream>

using namespace std;

using std::cout;
Again, twice? You know, 'std::cout' is available as 'cout' after
you wrote 'using namespace std;'...

int main(int argc, char* argv[])
Are you using 'argc' and 'argv'? If not, consider a simpler form
of 'main'.
{
ifstream infile ("b.bmp");
for (int n=0; n<200;n++)
{

int c= infile.get();
What does 'get' get? It gets an int. 'int' is a signed type. Why
are you surprised you get negative numbers?

if ((n>145)&&(n<165))//print the interesting bit
cout<<c<<" ";
}
return 0;
}

I thought the output would be
23 23 23 0 24 24 24 0 25 25 25 0 26 26 26 0 27 27 27
Why did you think that?
but it is
23 23 23 0 24 24 24 0 25 25 25 0 -1 -1 -1 -1 -1 -1 -1
b.bmp is a 2k file
from n
I don't understand. 'b.bmp' is a 2k file. How does that explain what
its contents are?
Why won't infile.get() work beyond the 157th place?


It works just fine. You get some values, don't you?

V
Jul 23 '05 #2
On Thu, 19 May 2005 to*******@aol.com wrote:
Why won't infile.get() work beyond the 157th place?


What is the size of int on your platform?
156*8=1248 and 156*16=2496 which is approaching the size of your file...
Perhaps you run beyond the end-of-file in your loop so maybe you should
check eof().

Sincerely,
Peter Jansson
http://www.jansson.net/

Jul 23 '05 #3
to*******@aol.com wrote:

I thought the output would be
23 23 23 0 24 24 24 0 25 25 25 0 26 26 26 0 27 27 27

but it is
23 23 23 0 24 24 24 0 25 25 25 0 -1 -1 -1 -1 -1 -1 -1


Open the file in binary mode. As is, it tries to read control-Z, which
sometimes represents end-of-file, and from then on, returns EOF.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #4
to*******@aol.com wrote:
#include "stdafx.h"
#include "stdafx.h"
Hmm, this is MS Windows, that has file I/O ramifications...
#include<iostream>
#include <fstream>

using namespace std;

using std::cout;

int main(int argc, char* argv[])
{
ifstream infile ("b.bmp");
In Windows, if "b.bmp" is really a bitmap, then it is a 'binary'
file and must be opened in 'binary' mode to prevent newline
translation and false EOF indicators (i.e. bogus -1 returns by
file reads):

ifstream infile;
infile.open("b.bmp", ios_base::in | ios_base::binary);
for (int n=0; n<200;n++)
{

int c= infile.get();

if ((n>145)&&(n<165))//print the interesting bit
cout<<c<<" ";
}
return 0;
}

I thought the output would be
23 23 23 0 24 24 24 0 25 25 25 0 26 26 26 0 27 27 27

but it is
23 23 23 0 24 24 24 0 25 25 25 0 -1 -1 -1 -1 -1 -1 -1
b.bmp is a 2k file
from n

Why won't infile.get() work beyond the 157th place?

Thanx
Tony


Regards,
Larry

--
Anti-spam address, change each 'X' to '.' to reply directly.
Jul 23 '05 #5
On 19 May 2005 06:55:08 -0700, to*******@aol.com wrote:
#include "stdafx.h"
#include "stdafx.h"
#include<iostream>
#include <fstream>

using namespace std;

using std::cout;

int main(int argc, char* argv[])
{
ifstream infile ("b.bmp");
for (int n=0; n<200;n++)
{

int c= infile.get();

if ((n>145)&&(n<165))//print the interesting bit
cout<<c<<" ";
}
return 0;
}

I thought the output would be
23 23 23 0 24 24 24 0 25 25 25 0 26 26 26 0 27 27 27

but it is
23 23 23 0 24 24 24 0 25 25 25 0 -1 -1 -1 -1 -1 -1 -1
b.bmp is a 2k file
from n

Why won't infile.get() work beyond the 157th place?

Thanx
Tony


You are reading the file in text mode, so 26 is interpreted as the EOF
character. Binary files must be opened as binary files, or you will get
this (and potentially other) unexpected behaviour.

--
Greg Schmidt gr***@trawna.com
Trawna Publications http://trawna.com/
Jul 23 '05 #6
Larry I Smith wrote:
to*******@aol.com wrote:
#include "stdafx.h"
#include "stdafx.h"
Hmm, this is MS Windows, that has file I/O ramifications...
#include<iostream>
#include <fstream>

using namespace std;

using std::cout;

int main(int argc, char* argv[])
{
ifstream infile ("b.bmp");


In Windows, if "b.bmp" is really a bitmap, then it is a 'binary'
file and must be opened in 'binary' mode to prevent newline
translation and false EOF indicators (i.e. bogus -1 returns by
file reads):

ifstream infile;
infile.open("b.bmp", ios_base::in | ios_base::binary);


Or you can just do this:

ifstream infile("b.bmp", ios_base::in |
ios_base::binary);
for (int n=0; n<200;n++)
{

int c= infile.get();

if ((n>145)&&(n<165))//print the interesting bit
cout<<c<<" ";
}
return 0;
}

I thought the output would be
23 23 23 0 24 24 24 0 25 25 25 0 26 26 26 0 27 27 27

but it is
23 23 23 0 24 24 24 0 25 25 25 0 -1 -1 -1 -1 -1 -1 -1
b.bmp is a 2k file
from n

Why won't infile.get() work beyond the 157th place?

Thanx
Tony


Regards,
Larry

--
Anti-spam address, change each 'X' to '.' to reply directly.
Jul 23 '05 #7
Larry I Smith wrote:

In Windows, if "b.bmp" is really a bitmap, then it is a 'binary'
file and must be opened in 'binary' mode to prevent newline
translation and false EOF indicators (i.e. bogus -1 returns by
file reads):


The -1 returns aren't bogus. They're real. Control-Z marks the end of a
file, and having once read that, the eof flag stays set until it's
explicitly cleared.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #8
Thanks.
I think I got it.
Tony

#include<iostream>
#include <fstream>

using namespace std;

int main()
{

ifstream infile ("b.bmp", ifstream::binary);
//binary so it doesn't see 24 as eof
for (int n=0;n<150;n++)
//churn through the first bit without doing anything
infile.get();
for(n=150;n<170;n++)
cout<<infile.get()<<"\n"; //output the results
for(n=170;infile;n++) //runs until infile is false.
{
int d= infile.get(); //I could do something with d!
}

cout<<"\n" "Filesize="<<n<<"\n";

return 0;
}

Jul 23 '05 #9
Pete Becker wrote:
Larry I Smith wrote:

In Windows, if "b.bmp" is really a bitmap, then it is a 'binary'
file and must be opened in 'binary' mode to prevent newline
translation and false EOF indicators (i.e. bogus -1 returns by
file reads):


The -1 returns aren't bogus. They're real. Control-Z marks the end of a
file, and having once read that, the eof flag stays set until it's
explicitly cleared.


This is only true for MS Windows.

They're 'bogus' in the respect that you haven't really
reached the end of the binary file. (:

Larry

--
Anti-spam address, change each 'X' to '.' to reply directly.
Jul 23 '05 #10
Larry I Smith wrote:

This is only true for MS Windows.
It's not an OS issue, but an RTL issue. The convention under DOS and
Windows is that ctrl-Z marks the end of a file, so libraries for those
OS's typically treat it as end of file. There may be other systems that
use the same convention. (Probably CP/M did that, too)

They're 'bogus' in the respect that you haven't really
reached the end of the binary file. (:


It's not a binary file if you're reading it as text. The EOF's are
exactly right.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #11
to*******@aol.com wrote:
//binary so it doesn't see 24 as eof


// binary because the file is binary <g>

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #12
Pete Becker wrote:
Larry I Smith wrote:

This is only true for MS Windows.
It's not an OS issue, but an RTL issue. The convention under DOS and
Windows is that ctrl-Z marks the end of a file, so libraries for those
OS's typically treat it as end of file. There may be other systems that
use the same convention. (Probably CP/M did that, too)


Yes, I know all of the history...
Modern Windows programs rarely write ctrl-z at the end of a file
anymore.

They're 'bogus' in the respect that you haven't really
reached the end of the binary file. (:


It's not a binary file if you're reading it as text. The EOF's are
exactly right.


On Windows one reads a binary file in 'text' mode only by mistake;
because doing so produces undefined results (e.g. EOF when the
physical end of file has not actually been reached).

This debate about Windows flaws could go on forever...
So let's NOT continue it.

Regards,
Larry
--
Anti-spam address, change each 'X' to '.' to reply directly.
Jul 23 '05 #13
Larry I Smith wrote:

This debate about Windows flaws could go on forever...


I'm not debating Windows flaws. I'm simply discussing how file IO works
in C and C++, which have mechanisms for dealing with the various
conventions for text files that various operating systems adopt.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #14
This's been very helpful. I just realised I never knew what a binary
file was. I always had in the back of my mind the vague thought that
all files are binary since theyre all coded in binary logic.
But now I know :-)

cheers

Tony

Jul 23 '05 #15

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

Similar topics

40
by: komone | last post by:
"Now is the time for all good web developers to use stylesheets". Hmm OK, so I start this commercial site design with the express intent of using CSS entirely. (Something I haven't attempted in...
6
by: rajek | last post by:
I posted a similar question yesterday, but didn't get an answer that resolved the issue. (Thanks to those who tried though.) The background: I've read in books and online that if you have one...
17
by: stubbsie | last post by:
Hi, I have redesigned our official public government website in .net and it has taken me a few months to redo. I have been the sole designer of the website from its humble beginnning a few years...
11
by: Robert Baer | last post by:
The following passes the test as valid, and the mouseover for the six indicated areas also work. I need various areas to link to another page, including the six mentioned. However either the MAP...
1
by: laurakr | last post by:
I am trying to use a clear to get my bottom nav bar below the quote box on the right, but it isn't working. I would like the bottom edge of the quote box to "stick" to the footer nav bar but copy...
1
by: judacris | last post by:
I've seen the threads here about molding 2 divs in a centered fashion. but I can't seem to solve this thing. my blogger blog is functioning well on my site for now, but the blog feed (left) and...
2
by: Nathan Sokalski | last post by:
I have the following code which allows you to drag a div in IE, and have it then move back to it's natural position when you release the mouse button: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML...
3
markmcgookin
by: markmcgookin | last post by:
Hi, I have the following XML <AnswerList xmlns="http://tempuri.org/ALPS_Assessmentv1p1_RESCO_Schema.xsd"> <DateTimeLastSaved>12:12:12 1900</DateTimeLastSaved> <UserName>Bob</UserName>...
1
by: Jahamos | last post by:
Background: I have copy and pasted an Excel flowchart into a Dreamweaver html page. I created a frame on the top of the page as a viewer. I created hotspot links over the flowchart items. Each...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
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...
0
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...
0
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...
0
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...

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.