为什么这个数据流在字节 26 处结束?

发布于 2024-11-13 23:22:09 字数 2328 浏览 3 评论 0原文

我仍在解决一周前的位图 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");

   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);
}

输出(来自 message.txt):

注意:data[0-25] 包含正确的值

...
值 data[19] = 19 小于 64,设置为 0x00。
数值数据[20] = 20 小于 64,设置为 0x00。
值 data[21] = 21 小于 64,设置为 0x00。
值 data[22] = 22 小于 64,设置为 0x00。
值 data[23] = 23 小于 64,设置为 0x00。
值 data[24] = 24 小于 64,设置为 0x00。
数值数据[25] = 25 小于 64,设置为 0x00。
值 data[26] = 0 小于 64,设置为 0x00。

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() or ifstream.read.

The input file Input.FILE was made in a hex editor, and just contains 0x00-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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

此岸叶落 2024-11-20 23:22:09

以二进制模式打开流:

sourcefile.open("Input.FILE", ios::binary);

Open your stream in binary mode:

sourcefile.open("Input.FILE", ios::binary);
残龙傲雪 2024-11-20 23:22:09

如果你查一下 ascii 代码 25 是什么,你会发现它意味着介质结束,所以有一个很好的如果您在 ascii 模式下阅读,则任何后续读取都将不起作用。

尝试指定您使用的是二进制文件:

sourcefile.open("Input.FILE", ios::binary);

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);
叶落知秋 2024-11-20 23:22:09

尝试 sourcefile.open("Input.FILE", std::ifstream::binary)

Try sourcefile.open("Input.FILE", std::ifstream::binary).

红衣飘飘貌似仙 2024-11-20 23:22:09

也许您 1) 使用古老的操作系统(例如 CP/M 或 DOS),其中内联控制 Z 代表 EOF,并且 2) 不以二进制模式打开文件。

尝试 sourcefile.open("Input.FILE", ios::binary);

Perhaps you are 1) using an archaic OS (such as CP/M or DOS) in which an in-line control-Z represents EOF, and 2) not opening the file in binary mode.

Try sourcefile.open("Input.FILE", ios::binary);.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文