读取超过127个ASCII值时,C ++ cin失败 [英] C++ cin fails when reading more than 127 ASCII values
问题描述
我创建了一个包含256个字符的文本文件,文本文件的第一个字符是ASCII值0,文本值的最后一个字符是ASCII值255.两者之间的字符从0到255均匀增加。所以字符#27是ASCII值27.字符#148应该是ASCII值148。
我的目标是读取此文本文件的每个字符。
我试着用 cin
阅读。我尝试了 cin.get()
和 cin.read()
,两者都应该读取未格式化的输入。 我想当我使用 unsigned char
, cin
表示正在读取 255
,这根本不是真的。当我使用正常的签名 char
, cin
表示它正在读取 -1
。它应该读取任何等同于ASCII 26
的字符。也许 cin
认为它被击中 EOF
?但我已经读过单独的StackOverflow的帖子以前, EOF
不是一个可以写的实际字符。所以我失去了为什么 cin
对表示整数 -1
或整数<$ c的字符值咳嗽$ c> 255 。有人可以告诉我我做错了什么,为什么,什么是最好的解决方案,为什么?
没有太多具体的代码粘贴。我尝试了一些不同的非工作组合,所有涉及 cin.get()
或 cin.read()
使用 char
或 unsigned char
并调用 char
和 int
之间。我没有运气能够读取超过第26个字符,除了这一点:
unsigned char character;
while((character =(unsigned char)cin.get())!= EOF){...}
有趣的是,虽然这不会阻止我的while循环在第26个字符,它也不移动。看起来像 cin
,无论它的 cin.get()
还是 cin.read()
只是拒绝前进到下一个字符的时候,它检测到它不喜欢的东西。我也知道存在 cin.ignore()
,但我的输入是不可预测的;也就是说,我的文本文件的这256个字符只是一个测试用例,真正的输入是随机的。这是一个更大的家庭作业的一部分,但这个具体的问题与任务无关;
cin
之前。 更新:
在Windows上,它停止在字符26之后可能是由于Ctrl-Z的东西,我不在乎这么多
但是在Linux上,它读取0到127之间的所有字符。但它似乎没有读取扩展ASCII字符从127到255.有一个解决方案程序产生的输出,我们应该模仿,该程序能够以某种方式读取所有255个字符。
问题:如何使用 cin
,我可以读取所有255个ASCII字符吗?
已解决
使用:
int characterInt;
unsigned char字符;
while((characterInt = getchar())!= EOF)
{
//'character'现在存储0到255之间的值
character = char)(characterInt);
}
std :: cin
读取文本流,而不是任意的二进制数据。
至于为什么第26个字符很有趣,你可能使用的是CP / M衍生词(如MS-DOS或MS-Windows)。在这些操作系统中,Control-Z用作文本文件中的EOF字符。
EDIT :
在Linux上,使用g ++ 4.4.3,以下程序的行为与预期的一致,打印数字0到255,包括:
#include< iostream>
#include< iomanip>
int main(){
int ch;
while((ch = std :: cin.get())!= std :: istream :: traits_type :: eof())
std :: cout< ch < ;
std :: cout<< \\\
;
}
I've created a text file that has 256 characters, the first character of the text file being ASCII value 0 and the last character of the text value being ASCII value 255. The characters in between increment from 0 to 255 evenly. So character #27 is ASCII value 27. Character #148 should be ASCII value 148.
My goal is to read every character of this text file.
I've tried reading this with cin
. I tried cin.get()
and cin.read()
, both of which are supposed to read unformatted input. But both fail when reading the 26th character. I think when I used an unsigned char
, cin
said it was reading read in 255
, which simply isn't true. And when I used a normal signed char
, cin
said it was reading in -1
. It should be reading in whatever the character equivalent of ASCII 26
is. Perhaps cin
thinks it's hit EOF
? But I've read on separate StackOverflow posts previously that EOF
isn't an actual character that one can write. So I'm lost as to why cin
is coughing on character values that represent integer -1
or integer 255
. Could someone please tell me what I'm doing wrong, why, and what the best solution is, and why?
There's not much concrete code to paste. I've tried a few different non-working combinations all involving either cin.get()
or cin.read()
with either char
or unsigned char
and call casts to char
and int
in between. I've had no luck with being able to read past the 26th character, except for this:
unsigned char character;
while ( (character = (unsigned char)cin.get()) != EOF) { ... }
Interestingly enough though, although this doesn't stop my while loop at the 26th character, it doesn't move on either. It seems like cin
, whether its cin.get()
or cin.read()
just refuses to advance to the next character the moment it detects something it doesn't like. I'm also aware that something like cin.ignore()
exists, but my input isn't predictable; that is, these 256 characters for my text file are just a test case, and the real input is rather random. This is part of a larger homework assignment, but this specific question is not related to the assignment; I"m just stuck on part of the process.
Note: I am reading from the standard input stream, not a specific text file. Still no straightforward solution it seems. I can't believe this hasn't been done on cin
before.
Update:
On Windows, it stops after character 26 probably due to that Ctrl-Z thing. I don't care that much for this problem. It only needs to work on Linux.
On Linux, though, it reads all characters from 0 - 127. But it doesn't seem to be reading the extended ASCII characters from 127 to 255. There's a "solution" program that produces output we're supposed to imitate, and that program is able to read all 255 characters somehow.
Question: How, using cin
, can I read all 255 ASCII characters?
Solved
Using:
int characterInt;
unsigned char character;
while ( (characterInt = getchar()) != EOF )
{
// 'character' now stores values from 0 - 255
character = (unsigned char)(characterInt);
}
std::cin
reads text streams, not arbitrary binary data.
As to why the 26th character is interesting, you are probably using a CP/M derivative (such as MS-DOS or MS-Windows). In those operating systems, Control-Z is used as an EOF character in text files.
EDIT: On Linux, using g++ 4.4.3, the following program behaves precisely as expected, printing the numbers 0 thru 255, inclusive:
#include <iostream>
#include <iomanip>
int main () {
int ch;
while( (ch=std::cin.get()) != std::istream::traits_type::eof() )
std::cout << ch << " ";
std::cout << "\n";
}
这篇关于读取超过127个ASCII值时,C ++ cin失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!