为什么这个数据流在字节26结束? [英] Why does this Stream of data end at byte 26?
问题描述
我仍然在处理一个星期前的位图I / O问题。我再次陷入困境,所以我决定开始一种类型的I / OI熟悉,并使它更像我需要的稳定(它是一次检查每个字节(像素),并输出到一个文件基于字节的值)。
我开始使用一个程序来读取和检查文本文件的每个字符,如果它高于某个阈值则输出一个'Z',并输出'A'如果它低于它。
该程序工作得很好,所以我决定将其从字符更改为文件中的字节。
现在,我遇到了问题。文件的第一个26(字节0-25)字节是正确的,但其余的是0或-1,这取决于我使用 ifstream.get()
还是 ifstream.read
。
输入文件 Input.FILE
是在十六进制编辑器中创建的,只包含 0x00-0xFF
。长度为256个字节。
代码:
#include< iostream>
#include< fstream>
#include< vector>
using namespace std;
int main()
{
ifstream sourcefile;
ofstream output;
ofstream output2;
output.open(output.txt);
output2.open(messages.txt);
sourcefile.open(Input.FILE);
矢量< char>数据(256);
sourcefile.read(& data [0],256);
sourcefile.seekg(0,ios :: beg);
for(int i =(int)0x00; i {
output2< Value data [<< i<< ] =< (int)data [i]< endl;
if((int)data [i] <0)
{
//因为我不能创建一个unsigned char向量,我用它来转换
//它到正确的数字。这可能是问题吗?
data [i] = 256 + data [i];
output2<< 小于零。 << endl;
}
if(data [i]> 64)
{
data [i] = 0xFF;
output2<< 大于64,设置为0xFF。 << endl;
}
else if(data [i] <64)
{
data [i] = 0x00;
output2<< 小于64,设置为0x00。 << endl;
}
else
{
//这很可能毫无意义,但我不想冒险
data [i] = 0x75;
output2<< 既不大于也不小于64?设置为0x75。 << endl;
}
output2<< endl;
}
output.write(& data [0],256);
}
输出(来自 message.txt
):
注意: data [0-25]
正确的值
...
值数据[19] = 19小于64,设置为0x00。 br>
值数据[20] = 20小于64,设置为0x00。
值数据[21] = 21小于64,设置为0x00。
值数据[22] = 22小于64,设置为0x00。
值数据[23] = 23小于64,设置为0x00。
值数据[24] = 24小于64,设置为0x00。
值数据[25] = 25小于64,设置为0x00。
值数据[26] = 0小于64,如果您查找www.asciitable.com/\"> ascii code 25是你会看到它表示
中等结束
,所以很有可能,如果你正在阅读ascii模式,任何后续的读取都无效。
尝试指定您使用的是二进制文件:
sourcefile.open(Input.FILE,ios :: binary);
I'm still working on that bitmap I/O problem from a week ago. I got stuck again, so I decided to start with a type of I/O I was familiar with, and make it more like what I needed steadily (which is checking each byte (pixel) at a time and outputting to a file based on that byte's value).
I started out with a program that read and checked each character of a text file, and output a 'Z' if it was above some threshold and output an 'A' if it was below it.
That program worked great, so I decided to change it from characters to bytes in a file.
Now, I've been having problems with it. The first 26 (bytes 0-25) bytes of the file are correct, but the rest are 0 or -1, depending on whether I use
ifstream.get()
orifstream.read
.The input file
Input.FILE
was made in a hex editor, and just contains0x00-0xFF
. It's 256 bytes in length.Code:
#include <iostream> #include <fstream> #include <vector> using namespace std; int main() { ifstream sourcefile; ofstream output; ofstream output2; output.open("output.txt"); output2.open("messages.txt"); sourcefile.open("Input.FILE"); vector<char> data(256); sourcefile.read(&data[0],256); sourcefile.seekg(0,ios::beg); for(int i = (int) 0x00 ; i < data.size() ; i++) { output2 << "Value data[" << i << "] = " << (int) data[i] << endl; if((int)data[i] < 0) { // since I can't make an unsigned char vector, I use this to convert // it to the correct number. Could this be the problem? data[i] = 256 + data[i]; output2 << "less than zero." << endl; } if(data[i] > 64) { data[i] = 0xFF; output2 << "greater than 64, set to 0xFF." << endl; } else if(data[i] < 64) { data[i] = 0x00; output2 << "less than 64, set to 0x00." << endl; } else { // This is most likely pointless, but I didn't want to take a chance data[i] = 0x75; output2 << "neither greater nor less than 64? Set to 0x75." << endl; } output2 << endl; } output.write(&data[0],256); }
Output (from
message.txt
):Note:
data[0-25]
contain the correct values...
Value data[19] = 19 less than 64, set to 0x00.
Value data[20] = 20 less than 64, set to 0x00.
Value data[21] = 21 less than 64, set to 0x00.
Value data[22] = 22 less than 64, set to 0x00.
Value data[23] = 23 less than 64, set to 0x00.
Value data[24] = 24 less than 64, set to 0x00.
Value data[25] = 25 less than 64, set to 0x00.
Value data[26] = 0 less than 64, set to 0x00.
解决方案If you look up what ascii code 25 is you'll see it means
end of medium
so there's a good chance that if you're reading in ascii mode, any subsequent reads aren't going to work.Try specifying that you're using a binary file:
sourcefile.open("Input.FILE", ios::binary);
这篇关于为什么这个数据流在字节26结束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!