简体   繁体   English

C ++文件I / O错误?

[英]C++ file i/o error?

Why is everything being read as 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;
        }
    }

My file format code is in C# and written like this: 我的文件格式代码使用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();

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. 在不知道test.txt内容的情况下,很难准确地说出,但是您好像反复将4个字节(大多数平台上int的大小)读入字符缓冲区/字符串,然后尝试将其转换为数字。 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 . 因此,我希望您能够直接读回您的电话号码,而无需atoi

For example: 例如:

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

value should now contain the number from the file. value现在应包含文件中的数字。 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;
        }
    }
}

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.. atoi将NUL终止的字符串转换为整数-您正在从文件读取四个字节 (它处于二进制模式)-可能不正确。

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). 例如,一个有效的字符串( atoi起作用,可以是“ 1234”-注:NUL终止),但是其字节表示形式是0x31 0x32 0x33 0x34(请注意,由于您仅读取4个字节,因此NUL终止了,所以atoi可以做任何事情)。 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. 如果确实是字节表示形式,则数字1234看起来像(取决于字节序)0x00 0x00 0x04 0xD2,正确读取此int方式是逐字节移动。

So, big question - what is the format? 所以,一个大问题-格式是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM