C++文件输入/输出错误?

发布于 2024-10-28 14:58:21 字数 1128 浏览 1 评论 0原文

为什么所有内容都读为 0?

    int width = 5;
    int height = 5;
    int someTile = 1;
    char buff[128];


    ifstream file("test.txt", ios::in|ios::binary);

    if(file.is_open())
    {
        cout << "open";
    }

    file.read(buff, sizeof(int));
    width = atoi(buff);

    file.read(buff, sizeof(int));
    height = atoi(buff);

    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            file.read(buff, sizeof(int));
            someTile = atoi(buff);
            cout << someTile;
        }
    }

我的文件格式代码是用 C# 编写的,如下所示:

FileStream stream = new FileStream("test.txt", FileMode.Create);
            BinaryWriter writer = new BinaryWriter(stream);
            // write a line of text to the file

            writer.Write(15);
            writer.Write(5);

            for (int i = 0; i < 15; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    writer.Write(1);
                }
            }

            // close the stream
            writer.Close();

Why is everything being read as 0?

    int width = 5;
    int height = 5;
    int someTile = 1;
    char buff[128];


    ifstream file("test.txt", ios::in|ios::binary);

    if(file.is_open())
    {
        cout << "open";
    }

    file.read(buff, sizeof(int));
    width = atoi(buff);

    file.read(buff, sizeof(int));
    height = atoi(buff);

    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            file.read(buff, sizeof(int));
            someTile = atoi(buff);
            cout << someTile;
        }
    }

My file format code is in C# and written like this:

FileStream stream = new FileStream("test.txt", FileMode.Create);
            BinaryWriter writer = new BinaryWriter(stream);
            // write a line of text to the file

            writer.Write(15);
            writer.Write(5);

            for (int i = 0; i < 15; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    writer.Write(1);
                }
            }

            // close the stream
            writer.Close();

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

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

发布评论

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

评论(2

浸婚纱 2024-11-04 14:58:21

在不知道 test.txt 的内容的情况下,很难准确地说,但看起来您正在重复将 4 个字节(大多数平台上的 int 大小)读入字符缓冲区/字符串,然后尝试将其转换为数字。除非您的文件完全由以空终止的四个字节块构成,否则我不希望它起作用。

更新:好的,看看你的文件格式,你不是在写字符串,而是在写整数。因此,我希望您能够直接读回您的数字,而不需要 atoi

例如:

int value;
file.read((char*)&value, sizeof(int));

value 现在应该包含文件中的数字。要转换整个示例,您需要寻找如下内容:

int width = 5;
int height = 5;
int someTile = 1;

ifstream file("test.txt", ios::in|ios::binary);

if(file.is_open())
{
    cout << "open";

    file.read(reinterpret_cast<char*>(&width), sizeof(int));
    file.read(reinterpret_cast<char*>(&height), sizeof(int));

    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            file.read(reinterpret_cast<char*>(&someTime), sizeof(int));
            cout << someTile;
        }
    }
}

Without knowing the contents of test.txt it's difficult to say exactly, but it looks like you're repeatedly reading 4 bytes (size of an int on most platforms) into a character buffer / string, and then trying to turn that into a number. Unless your file is constructed entirely of four byte blocks that are null-terminated, I wouldn't expect this to work.

Update: Ok, looking at your file format you're not writing strings, you're writing ints. Therefore I'd expect you to be able to read your numbers straight back in, with no need for atoi.

For example:

int value;
file.read((char*)&value, sizeof(int));

value should now contain the number from the file. To convert your whole example you're looking for something like this:

int width = 5;
int height = 5;
int someTile = 1;

ifstream file("test.txt", ios::in|ios::binary);

if(file.is_open())
{
    cout << "open";

    file.read(reinterpret_cast<char*>(&width), sizeof(int));
    file.read(reinterpret_cast<char*>(&height), sizeof(int));

    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            file.read(reinterpret_cast<char*>(&someTime), sizeof(int));
            cout << someTile;
        }
    }
}
如若梦似彩虹 2024-11-04 14:58:21

atoi 将 NUL 终止的字符串转换为整数 - 您正在从文件中读取四个字节(二进制模式) - 这可能不正确......

例如,有效的字符串(对于atoi来说,可以是“1234” - 注意:NUL终止),但是它的字节表示是0x31 0x32 0x33 0x34(注意NUL终止,因为你只读取了4个字节,所以,atoi 可以做任何事情)。这个文件的格式是什么?如果它确实是字节表示,则数字 1234 看起来像(取决于字节顺序)0x00 0x00 0x04 0xD2,正确读取此 int 的方法是逐字节移位。

那么,大问题 - 格式是什么?

atoi converts a NUL terminated string to an integer - you are reading four bytes from the file (it's in binary mode) - which may not be correct..

for example, a valid string (for atoi to work could be, "1234" - NOTE: NUL terminated), however the byte representation of this is 0x31 0x32 0x33 0x34 (note NUL terminated given you only read 4 bytes, so, atoi could be doing anything). What is the format of this file? If it really is byte representation, the number 1234 would look like (depending on endianess), 0x00 0x00 0x04 0xD2, the way to correctly read this int would be to shift in byte by byte.

So, big question - what is the format?

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