在 C++ 中读取和写入 int 到二进制文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15017879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:54:15  来源:igfitidea点击:

Reading and writing int to a binary file in C++

c++visual-c++file-io

提问by user2097891

I am unclear about how reading long integers work. If I say

我不清楚读取长整数是如何工作的。如果我说

long int a[1]={666666}
ofstream o("ex",ios::binary);
o.write((char*)a,sizeof(a));

to store values to a file and want to read them back as it is

将值存储到文件中并希望按原样读取它们

long int stor[1];
ifstream i("ex",ios::binary);
i.read((char*)stor,sizeof(stor));

how will I be able to display the same number as stored using the information stored in multiple bytes of character array?

如何使用存储在字符数组的多个字节中的信息显示与存储的数字相同的数字?

回答by cIph3r

o.writedoes not write character, it writes bytes (if flagged with ios::binary). The char-pointer is used because a char has length 1 Byte.

o.write不写字符,它写字节(如果用 ios::binary 标记)。使用字符指针是因为字符的长度为 1 字节。

o.write((char*)a,sizeof(a)); 

(char*) ais the adress of what o.writeshould write. Then it writes sizeof(a)bytes to a file. There are no characters stored, just bytes.

(char*) ao.write应该写什么的地址。然后它将sizeof(a)字节写入文件。没有存储字符,只有字节。

If you open the file in a Hex-Editor you would see something like this if a is int i = 10: 0A 00 00 00(4 Byte, on x64).

如果您在十六进制编辑器中打开文件,如果 a 是int i = 100A 00 00 00(4 字节,在 x64 上),您会看到类似这样的内容。

Reading is analogue.

阅读是模拟的。

Here is a working example:

这是一个工作示例:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;


int main (int argc, char* argv[]){
    const char* FILENAM = "a.txt";
    int toStore = 10;
    ofstream o(FILENAM,ios::binary);

    o.write((char*)&toStore,sizeof(toStore));
    o.close();

    int toRestore=0;
    ifstream i(FILENAM,ios::binary);
    i.read((char*)&toRestore,sizeof(toRestore));

    cout << toRestore << endl;


    return 0;
}

回答by Indinfer

Sorry I took so long to see your question.

对不起,我花了这么长时间才看到你的问题。

I think the difference between binary is the binary will read and write the file as is. But the non-binary (i.e. text) mode will fix up the end-of-line '\n' with carriage-return '\r'. The fix-up will change back and forth between '\n' and '\r', or "\n\r" or "\r\n" or leave it as '\n'. What it does depends on whether the target operating system is Mac, Windows, Unix, etc.

我认为二进制文件之间的区别在于二进制文件将按原样读取和写入文件。但是非二进制(即文本)模式将使用回车符 '\r' 修复行尾 '\n'。修正会在 '\n' 和 '\r',或 "\n\r" 或 "\r\n" 之间来回更改,或者将其保留为 '\n'。它的作用取决于目标操作系统是 Mac、Windows、Unix 等。

I think if you are reading and writing an integer, it will read and write your integer fine and it will look correct. But if some byte(s) of the integer look like '\r' and '\n', then the integer will not read back correctly from the file.

我认为如果您正在读取和写入一个整数,它会很好地读取和写入您的整数,并且看起来是正确的。但是,如果整数的某些字节看起来像 '\r' 和 '\n',那么整数将无法从文件中正确读回。

Binary assures that reading back an int will always be correct. But you want text mode to format a file to be read in a text editor such as Windows's Notepad.

二进制确保读回 int 总是正确的。但是您希望文本模式格式化要在文本编辑器(例如 Windows 的记事本)中读取的文件。