简体   繁体   English

C ++ pwrite(),pread()数据到文本文件

[英]C++ pwrite(), pread() data to a text file

I have to pwrite() characters to a text file with each character representing 1 byte. 我必须将pwrite()字符写入文本文件,每个字符代表1个字节。 Also, I need to write integers to the text file, so 12 has to be one byte also, not 2 bytes (even though two characters). 另外,我需要将整数写入文本文件,因此12也必须是一个字节,而不是2个字节(即使是两个字符)。

I am using char *pointer for the characters and integers, but I am getting stuck since the text fill prints jumbled values for the integers (@'s, upside-down ?'s, etc.) Like when I pwrite() pointer[0] = 105; 我正在将char * pointer用于字符和整数,但是由于文本填充会打印出混杂的整数值(@,颠倒的?等),所以我陷入了困境,就像当我使用pwrite()指针[ 0] = 105; The 105 translates 'i' in the text.txt file (and pread() reads as 'i') Somehow the 105 is lost in translation. 105会在text.txt文件中翻译“ i”(pread()读为“ i”)。105会以某种方式丢失。

Any ideas how to pwrite()/pread() correctly? 有什么想法如何正确pwrite()/ pread()吗?

ofstream file; file.open("text.txt");
char *characters = new char;
characters[0] = 105;
cout << pwrite(3, characters, 1, 0);

Also, the 3, is the filedes, which I guess :-P Don't know how to actually find. 另外,3是文件名,我猜:-P不知道如何实际查找。 The text.txt file then has 'i' in it (ASCII 105 I'm assuming). 然后,text.txt文件中就有“ i”(我假设是ASCII 105)。 When I pread() then, how will I know if it was originally and 'i' or 105? 然后,当我pread()时,我怎么知道它原来是'i'还是105?

Breaking this down in chunks: 分解成块:

"I have to pwrite() characters to a text file with each character representing 1 byte" “我必须将pwrite()字符写入文本文件,每个字符代表1个字节”

By definition, each ASCI character is one byte, and you make no mention of need to to write locale-aware multi-byte characters, or Unicode derivatives, so I'm thinking on this one you're probably covered. 根据定义,每个ASCI字符 都是一个字节,并且您没有提到需要编写可感知区域设置的多字节字符或Unicode派生类,因此我认为您可能已经覆盖了这一点。

"Also, I need to write integers to the text file, so 12 has to be one byte also, not 2 bytes (even though two characters)" “而且,我需要将整数写入文本文件,因此12也必须是一个字节,而不是2个字节(即使是两个字符)”

You're describing a binary write of your integer data. 您正在描述整数数据的二进制写入。 However, keep in mind that "integers" as a numeric representation can be larger than just a number represented by "one byte". 但是,请记住,“整数”作为数字表示形式可能会大于仅由“一个字节”表示的数字。 If you want to write an integer that can be represented in a single byte, your options are: 如果要编写一个可以用单个字节表示整数,则可以选择:

  1. For signed data, values can range from [-128,127] 对于签名数据,值的范围可以是[-128,127]
  2. For unsigned data, values can range from [0, 255] 对于无符号数据,值的范围可以是[0,255]

These are the limitations of an integer value in a single octet. 这些是单个八位字节中整数值的限制。

"I am using char *pointer for the characters and integers, but I am getting stuck since the text fill prints jumbled values for the integers (@'s, upside-down ?'s, etc.)" “我将char * pointer用于字符和整数,但是由于文本填充会打印出混杂的整数值(@,颠倒的?等),所以我陷入了困境”

The char pointer for characters we covered before, and will likely be fine. 我们之前讨论过的字符的char指针,可能会很好。 The integers will NOT be. 整数将不是。 Your resulting file per your description will not be a "text" file in the literal sense. 从字面上看,按照您的描述生成的文件不是 “文本”文件。 It will contain both character data (your char buffers) and binary data (your integers). 它将包含字符数据(您的char缓冲区)和二进制数据(您的整数)。 Please remember an integer within a single byte with a value of 0x01 will be just that, a single octet with the first bit set. 请记住,单个字节内的值为0x01的整数将恰好就是设置了第一位的单个八位位组。 A byte representing the ASCI character '1' will have a value of 0x31 (see any ASCI chart ), and value 0xF1 for EBCDIC (don't ask). 代表ASCI 字符 “ 1”的字节的值将为0x31(请参阅任何ASCI图表 ),而EBCDIC的值为0xF1(不要问)。 Using your example, **you cannot write the value 12 in a single byte and have it be displayable "text" (character) data in your file. 使用您的示例,**您不能在单个字节中写入值12,并且使其成为文件中可显示的“文本”(字符)数据。 The single-byte integer value 12 will be represented in your file as a single byte value 0x0C. 单字节整数值12将在文件中表示为单字节值0x0C。 Trying to view this as "text" will not work; 试图将其视为“文本”是行不通的; it is not printable ASCI. 它不是可打印的ASCI。 In fact, the ASCI value of 0x0C is actually a form-feed control character. 实际上,ASCI值0x0C实际上是换页控制字符。

Bottom line, if you don't know the difference between ASCI characters and integer bytes, explaining how pwrite() works will do little good but to confuse you more. 底线是,如果您不知道ASCI字符和整数字节之间的区别,那么说明pwrite()工作原理将pwrite()但会让您更加困惑。

"Like when I pwrite() pointer[0] = 105; The 105 translates 'i' in the text.txt file (and pread() reads as 'i') Somehow the 105 is lost in translation" “就像当我pwrite()指针[0] = 105时; 105转换text.txt文件中的“ i”(而pread()读为“ i”),以某种方式使105丢失了

Refer to the ASCI chart linked several places in this answer. 请参阅此答案中链接几个地方的ASCI图表。 The byte value 105 is, infact, the ASCI value of the character 'i'. 实际上,字节值105是字符“ i”的ASCI值。 The 105 isn't lost; 105并没有丢失; its being displayed as the character it represents. 它被显示为它所代表的字符。

Finally, pwrite() is a POSIX system call for Linux, BSD, and anyone else that chooses to expose it. 最后, pwrite()是POSIX系统调用,适用于Linux,BSD和其他选择公开它的人。 It is not part of the C or C++ standards. 不是 C或C ++标准的一部分。 That said, your first argument for pwrite() should be obtained from a system call, open() . 就是说,您应该从系统调用open()获得pwrite()第一个参数。 You should never piggyback on a file descriptor assumed to be opened by a different api call unless you go through a supported API to do so. 除非您通过受支持的API来这样做,否则您永远不要背负假定由其他api调用打开的文件描述符。 The code in this question does not. 这个问题中的代码没有。

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

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