迟钝的问题 - 产生的回报 [英] retarded question--generating carrage return

查看:55
本文介绍了迟钝的问题 - 产生的回报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在输出文件的数据部分

之前输出文本头文件。但是当我使用\ n时或endl作为行尾,

它只是在文件中放入一个0x​​0a(这是非打印的)...在Windows中

你需要(0x0d 0x0a)对,如果你想要一个文本中的新行

编辑器。将这个2字节序列转换成

文件的正确方法是什么?


谢谢


#include< iostream>

#include< fstream>

#include< string>

using namespace std;


int main(){


string lf =" 00" ;; //愚蠢的方式

lf [0] = 13; //生成

lf [1] = 10; // carrage返回


int data = 1234;


ofstream out(" test.txt",ios :: out | ios: :二进制);

cout<< Writing file:test.txt" << endl;

out<< 比特大小: << lf<<数据<< lf;

out<< 初始化矢量: << lf<<数据<< lf;

返回0;

}

I''m wanting to output a text header file in front of the data portion
of an output file. However when I use "\n" or "endl" as end of line,
it just puts a 0x0a in the file(which is non-printing)...in Windows
you need the (0x0d 0x0a) pair, if you want a new-line in a text
editor. What''s the correct way to get this 2-byte sequence into a
file?

Thanks

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main(){

string lf = "00"; //stupid way
lf[0]=13; //to generate
lf[1]=10; //carrage return

int data = 1234;

ofstream out("test.txt", ios::out|ios::binary);
cout << "Writing file: test.txt" << endl;
out << "Bit Size:" << lf << data << lf;
out << "Initialization Vector:" << lf << data << lf ;
return 0;
}

推荐答案

2003年9月15日22:05 :17 -0700, ma**********@yahoo.com (J坎贝尔)

在comp.lang.c ++中写道:
On 15 Sep 2003 22:05:17 -0700, ma**********@yahoo.com (J. Campbell)
wrote in comp.lang.c++:
我想在数据部分前输出文本头文件/>输出文件。但是当我使用\ n时或endl作为行尾,它只是在文件中放入一个0x​​0a(非打印)...在Windows中你需要(0x0d 0x0a)对,如果你想要一个换行符一个文本编辑器。将这个2字节序列放入
文件的正确方法是什么?

#include< iostream>
#包括< fstream>
#include< string>
使用命名空间std;

int main(){

string lf =" 00" ;; //愚蠢的方式
lf [0] = 13; //生成
lf [1] = 10; // carrage返回

int data = 1234;

ofstream out(" test.txt",ios :: out | ios :: binary);
^^^^^^^^^^^


如果您希望扩展名为.txt的文件看起来像标准

文本文件,以文本模式打开。如果您没有指定二进制文件,则文本模式是默认值。

cout<< Writing file:test.txt" << endl;
out<< 比特大小: << lf<<数据<< lf;
out<< 初始化矢量: << lf<<数据<< lf;
返回0;
}
I''m wanting to output a text header file in front of the data portion
of an output file. However when I use "\n" or "endl" as end of line,
it just puts a 0x0a in the file(which is non-printing)...in Windows
you need the (0x0d 0x0a) pair, if you want a new-line in a text
editor. What''s the correct way to get this 2-byte sequence into a
file?

Thanks

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main(){

string lf = "00"; //stupid way
lf[0]=13; //to generate
lf[1]=10; //carrage return

int data = 1234;

ofstream out("test.txt", ios::out|ios::binary); ^^^^^^^^^^^

If you want a file with the extension of .txt to look like a standard
text file, open it in text mode. Text mode is the default if you
don''t specify binary.
cout << "Writing file: test.txt" << endl;
out << "Bit Size:" << lf << data << lf;
out << "Initialization Vector:" << lf << data << lf ;
return 0;
}




-

Jack Klein

主页: http://JK-Technology.Com

常见问题解答for

comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt .comp.lang.learn.c-c ++ ftp:// snurse- l.org/pub/acllc-c++/faq



--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq


J. Campbell写道:
J. Campbell wrote:
我想在输出文件的数据部分前面输出文本头文件。但是当我使用\ n时或endl作为行尾,它只是在文件中放入一个0x​​0a(非打印)...在Windows中你需要(0x0d 0x0a)对,如果你想要一个换行符一个文本编辑器。将这个2字节序列放入
文件的正确方法是什么?

#include< iostream>
#包括< fstream>
#include< string>
使用命名空间std;

int main(){

string lf =" 00" ;; //愚蠢的方式
lf [0] = 13; //生成
lf [1] = 10; // carrage返回

int data = 1234;

ofstream out(" test.txt",ios :: out | ios :: binary);
cout<< Writing file:test.txt" << endl;
out<< 比特大小: << lf<<数据<< lf;
out<< 初始化矢量: << lf<<数据<< lf;
返回0;
}
I''m wanting to output a text header file in front of the data portion
of an output file. However when I use "\n" or "endl" as end of line,
it just puts a 0x0a in the file(which is non-printing)...in Windows
you need the (0x0d 0x0a) pair, if you want a new-line in a text
editor. What''s the correct way to get this 2-byte sequence into a
file?

Thanks

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main(){

string lf = "00"; //stupid way
lf[0]=13; //to generate
lf[1]=10; //carrage return

int data = 1234;

ofstream out("test.txt", ios::out|ios::binary);
cout << "Writing file: test.txt" << endl;
out << "Bit Size:" << lf << data << lf;
out << "Initialization Vector:" << lf << data << lf ;
return 0;
}



如果您的文件处于二进制模式(我可以看到您在上面执行)那么

如果你有endl,你可以用\\\\ n取代它。同样

,无论你有什么\ n,用\\\\ n替换它。


.... 。

string lf =" \r\\\
" ;;

// nuke the lf [0] = 13; LF [1] = 10; - 坏坏坏

.....

或者你可以用行中的字符串创建你的文字

并让编译器执行连接它们的工作。


#define LE" \\ nn"


int main()

{

....

out<<

" Bit Size:" LE<<数据<<

LE"初始化矢量:" LE<<数据<< LE;

....

}


对于ASCII大多数Windows代码页,UNICODE等。

13 ==(int)''\ r''

10 ==(int)''\ n''


不所以对于其他编码。


可以选择像往常一样编写文本文件然后

" convert"文件到DOS行的结尾。

现在,如果你在一台机器上没有二进制模式打开了一个流,那就是需要CR LR行结束,那么endl和\\ \
"将自动转换为CR LF

行结尾。



If you have your file in binary mode (I can see you do above) then
whereever you have endl, you can replace it with "\r\n" and likewise
with whereever you have "\n", replace it with "\r\n".

.....
string lf = "\r\n";
// nuke the lf[0]=13; lf[1]=10; - bad bad bad
.....
or you can create your literals with line enings in them
and make the compiler do the work of concatenating them.

#define LE "\r\n"

int main()
{
....
out <<
"Bit Size:" LE << data <<
LE "Initialization Vector:" LE << data << LE;
....
}

For ASCII most windows codepages, UNICODE and others.
13 == (int)''\r''
10 == (int)''\n''

Not so for other encodings.

The alternative it to write a text file like you would normally and then
"convert" the file to DOS line endings.
Now, if you opened a ofstream without the binary mode on a machine that
required CR LR line endings, then endl and "\n" would convert to CR LF
line endings automagically.



J. Campbell写道:
J. Campbell wrote:
我想在输出文件的数据部分前输出文本头文件。


所以你想要一个二进制文件,但你想要它的一部分在目标系统上有一个文本文件的

属性吗?


好​​吧,首先我认为你应该真的质疑这是否适合。你已经定义了一个二进制文件格式,但是你想让系统定义它的一部分而不是自己定义吗?

的结果是你不能轻易地在系统之间传输文件。

任何读取文件的程序都必须准备好所有可能的文本

文件格式,或者你必须做一些转换到

目标系统在阅读文件之前的格式 - 以及标准工具

这样做(如dos2unix)不会工作。


这里是我看到的选项:1)您可以使用系统定义的文本

格式。 2)你可以选择一种特定的格式(也许是Windows

格式)并将其用于所有系统。


如果你想选择1,我可以想到两种可能的方法来实现它。
实现它。 1)您可以将输出文件作为文本打开,写入文本

part,关闭它,再次打开它作为二进制文件,然后编写二进制文件。 2)

你可以有一个系统特定的常量字符串给出换行符

序列,在不同的系统上必须有不同的(你可以

将它粘贴在一些包含系统相关内容的源文件中,并且

为你移植的每个系统都有这个文件的新版本。


2的更通用版本将考虑可能需要进行的所有可能的翻译,以使您的文本输出与系统预期的

格式相匹配。这与I / O

系统已经为文本翻译做的很相似。

< snip>

string lf =" 00英寸; //愚蠢的方式
lf [0] = 13; //生成
lf [1] = 10; // carrage return
I''m wanting to output a text header file in front of the data portion
of an output file.
So you want a binary file, but you want part of it to have the
properties of a text file on the target system?

Well, for one thing I think you should really question whether this is
appropriate. You''ve defined a binary file format, but you want to make
part of it system-defined instead of defining it yourself? The result of
this is that you can''t easily transfer the file between systems. Either
any program reading the file has to be prepared for all possible text
file formats, or you have to do some kind of conversion to the
destination system''s format before reading the file - and standard tools
for doing this (like dos2unix) won''t work.

Here are the options I see: 1) You could use the system-defined text
format. 2) You could choose a particular format (perhaps the Windows
format) and use that for all systems.

If you want to go with option 1, I can think of 2 possible methods to
implement it. 1) You could open the output file as text, write the text
part, close it, open it again as binary, and write the binary part. 2)
You could have a system-specific constant string giving the newline
sequence, which has to be differently on different systems (you could
stick it in some source file that contains system-dependent stuff, and
have a new version of this file for each system you port to, for example).

A more generalized version of 2 would take into account all possible
translations that might need to occur to make your text output match the
format expected by the system. This would be a lot like what the I/O
system already does for text translation.
<snip>

string lf = "00"; //stupid way
lf[0]=13; //to generate
lf[1]=10; //carrage return




您还可以使用转义序列。例如:


const string lf(" \ x0d \ x0a");




const string lf(" \\\\ nn;);


-Kevin

-

我的电子邮件地址有效,但会定期更改。

要联系我,请使用最近发布的地址。



There are escape sequences you could use also. For example:

const string lf("\x0d\x0a");

or

const string lf("\r\n");

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.


这篇关于迟钝的问题 - 产生的回报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆