为什么不能在第158位之后工作?得到() [英] why won't this work after 158th position? get()
问题描述
#include" stdafx.h"
#include" stdafx.h"
#include< iostream>
#include使用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))//打印有趣的位
cout<< c<<"" ;
}
返回0;
}
我以为输出会是
23 23 23 0 24 24 24 0 25 25 25 0 26 26 26 0 27 27 27
但它是
23 23 23 0 24 24 24 0 25 25 25 0 -1 -1 -1 -1 -1 -1 -1
b.bmp是2k文件
来自n
为什么不能infile.get()工作超过第157位?
Thanx
Tony
#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
推荐答案
** *****@aol.com 写道:
#include" stdafx.h"
#include" stdafx.h"
两次?在发布时请考虑清除编译器特定的废话
。
#include< iostream>
#include< fstream>
>使用命名空间std;
使用std :: cout;
又一次,两次?你知道,'std :: cout''可以用''cout''作为'/ b $'后你写''使用命名空间std;''...
int main(int argc,char * argv [])
您使用''argc''和''argv''?如果没有,请考虑一个更简单的表格
''main''。
{
ifstream infile(" b.bmp");
for(int n = 0; n< 200; n ++)
int c = infile.get();
得到什么?它得到一个int。 ''int''是签名类型。为什么
你会惊讶地得到负数?
如果((n> 145)&&(n< 165))//打印有趣的位
cout<< c<<" ;
}
返回0;
}
我认为输出将是
23 23 23 0 24 24 24 0 25 25 25 0 26 26 26 0 27 27 27
你为什么这么认为?
但它是
23 23 23 0 24 24 24 0 25 25 25 0 -1 -1 -1 -1 -1 -1 -1
b.bmp是2k文件
来自n
我不是了解。 ''b.bmp''是一个2k文件。这是如何解释
的内容是什么?
为什么infile.get()工作超出第157位?
#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?
它运作得很好。你得到一些价值,不是吗?
V
It works just fine. You get some values, don''t you?
V
2005年5月19日星期四到******* @ aol.com 写道:
On Thu, 19 May 2005 to*******@aol.com wrote:
为什么不会infile.get ()工作超过第157位?
Why won''t infile.get() work beyond the 157th place?
平台上int的大小是多少?
156 * 8 = 1248和156 * 16 = 2496接近文件的大小...
也许你在循环中超出了文件结尾,所以也许你应该
check eof( )。
此致,
Peter Jansson
http://www.jansson.net/
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/
到******* @ aol.com 写道:
to*******@aol.com wrote:
我以为输出会
23 23 23 0 24 24 24 0 25 25 25 0 26 26 26 0 27 27 27
但它是
23 23 23 0 24 24 24 0 25 25 25 0 -1 -1 -1 -1 -1 -1 -1
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
以二进制模式打开文件。它会尝试读取control-Z,其中
有时代表文件结尾,从那时起,返回EOF。
-
Pete Becker
Dinkumware,Ltd。( http://www.dinkumware.com )
这篇关于为什么不能在第158位之后工作?得到()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!