473,387 Members | 3,750 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

retarded question--generating carrage return

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;
}
Jul 19 '05 #1
13 3765
On 15 Sep 2003 22:05:17 -0700, ma**********@yahoo.com (J. Campbell)
wrote in comp.lang.c++:
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
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
Jul 19 '05 #2
J. Campbell wrote:
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;
}

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.


Jul 19 '05 #3
J. Campbell wrote:
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


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.

Jul 19 '05 #4
Gianni Mariani <gi*******@mariani.ws> wrote in message > ....
string lf = "\r\n";
// nuke the lf[0]=13; lf[1]=10; - bad bad bad
....


Thanks for your help, guys. I need a better reference. My "book"
doesn't have the complete escape sequence listing.

A helpful discussion regarding mixed formats: Actually, the intent
was just to make the header values easy to find, read, and identify
while working on the program that writes/reads the files. In the
final iteration, the header values would be fixed-length binary
without text identifiers, in order to fix the offsets to each header
value and to the bulk data.

I agree with kevin that using \n sense, then write the file reader to
use same, and don't worry about the OS. I was just trying to make it
convenient for me to double-click my file to check the contents of the
header.

Thanks again.
Jul 19 '05 #5
Jack Klein <ja*******@spamcop.net> spoke thus:
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.


In C, there is very little difference between opening a file "w" and "wb". Is
there a substiantial difference, in C++, between text and binary modes?

--
Christopher Benson-Manica | Jumonji giri, for honour.
ataru(at)cyberspace.org |
Jul 19 '05 #6
Kevin Goodsell wrote:
You could open the output file as text, write the text
part, close it, open it again as binary, and write the binary part.
-Kevin


This seemed reasonable. It works fine. However, not quite as I
expected. I have some questions about the following:

fstream out(filename, ios::modex|ios::modey|ios::modez);
write(char_pointer, n_bytes);

specifically, why does:

ofstream out(fileout.c_str(), ios::out|ios::binary|ios::ate);
//overwrites
write(char_pointer, n_bytes); ^^^^^^^^

ofstream out(fileout.c_str(), ios::out|ios::binary|ios::app);
//appends
write(char_pointer, n_bytes); ^^^^^^^^

ofstream out(fileout.c_str(), ios::in|ios::out|ios::binary|ios::ate);
//appends
write(char_pointer, n_bytes); ^^^^^^^ ^^^^^^^^

I don't understand this behavior. I would think that all 3 constructs
should append. Why does the first one, that opens the file with the
put pointer pointing "at the end" overwrite pre-esisting data in the
file??
Jul 19 '05 #7
Christopher Benson-Manica wrote:
Jack Klein <ja*******@spamcop.net> spoke thus:
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.


In C, there is very little difference between opening a file "w" and
"wb". Is there a substiantial difference, in C++, between text and
binary modes?


Yes, the same substantial difference as in C.

--
Attila aka WW
Jul 19 '05 #8
In article <bk**********@chessie.cirr.com>, at***@nospam.cyberspace.org
says...

[ ... ]
In C, there is very little difference between opening a file "w" and "wb".
In either C or C++, the difference between the two is implementation
defined -- it may be small or it may be huge.
Is
there a substiantial difference, in C++, between text and binary modes?


C++ is basically the same as C in this respect -- the implementation
(and mostly the platform) is what matters. If you happen to do most of
your work on UNIX (or something extremely similar), there normally won't
be any difference at all. On some other machines (e.g. IBM mainframes)
the difference may be quite substantial indeed.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #9
Christopher Benson-Manica escribió:
In C, there is very little difference between opening a file "w" and "wb".
Write a program to copy files that uses no binary mode, compile it on
Windows or in Mac, copy some executable files and tell us again how
little is the difference ;)
Is there a substiantial difference, in C++, between text and binary modes?


The same as in C.

Regards.
Jul 19 '05 #10
Christopher Benson-Manica wrote:
Jack Klein <ja*******@spamcop.net> spoke thus:

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.

In C, there is very little difference between opening a file "w" and "wb". Is
there a substiantial difference, in C++, between text and binary modes?


In both C and C++ the system will translate certain character sequences,
like "\r\n" for instance, to their logical equivilant, like "\n" for
instance. Opening a file in binary does not make this conversion.

As far as I have ever seen, the newline is the only character this
happens to usually. So, if you are on a Unix you won't notice any
difference at all, but if you are on Mac < 10 or DOS based then you will
notice a difference.

NR

Jul 19 '05 #11
J. Campbell wrote:
Kevin Goodsell wrote:
You could open the output file as text, write the text
part, close it, open it again as binary, and write the binary part.
-Kevin

This seemed reasonable. It works fine. However, not quite as I
expected. I have some questions about the following:


I hope someone else will reply, because I don't know enough about
streams to give you a definite answer.

fstream out(filename, ios::modex|ios::modey|ios::modez);
write(char_pointer, n_bytes);

specifically, why does:

ofstream out(fileout.c_str(), ios::out|ios::binary|ios::ate);
//overwrites
write(char_pointer, n_bytes); ^^^^^^^^
The ios::out is redundant, I think. The ios::ate should move the
insertion point (bad term for it, but you know what I mean) to the end
of the file when it's opened. But as far as I can see, none of these
flags prevents the file from being truncated to 0 length. The only way I
see to create that behavior (without ios::app) would be to open for
input and output.

ofstream out(fileout.c_str(), ios::out|ios::binary|ios::app);
//appends
write(char_pointer, n_bytes); ^^^^^^^^
This also causes the insertion point to be moved to the end of the file
on every write, so writing in the middle is not possible.

ofstream out(fileout.c_str(), ios::in|ios::out|ios::binary|ios::ate);
//appends
write(char_pointer, n_bytes); ^^^^^^^ ^^^^^^^^
Yes, I think this is the only way to get a file that is writable at any
location without truncating it or using ios::app. But I'm not sure if it
makes sense to have an ofstream do input. You might have to use plain
fstream. Hopefully someone else will advise further.

I don't understand this behavior. I would think that all 3 constructs
should append. Why does the first one, that opens the file with the
put pointer pointing "at the end" overwrite pre-esisting data in the
file??


Because nothing in the flags tells it *not* to truncate the file to 0
length, which is normal behavior with ios::out. At least, that seems to
be the reason as far as I can tell.

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

Jul 19 '05 #12
On Tue, 16 Sep 2003 12:57:17 +0000 (UTC), Christopher Benson-Manica
<at***@nospam.cyberspace.org> wrote in comp.lang.c++:
Jack Klein <ja*******@spamcop.net> spoke thus:
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.


In C, there is very little difference between opening a file "w" and "wb". Is
there a substiantial difference, in C++, between text and binary modes?


Can you cite a reference from any version of the C standard to support
your statement? I think not.

--
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
Jul 19 '05 #13
On 16 Sep 2003 05:42:48 GMT, Gianni Mariani <gi*******@mariani.ws>
wrote in comp.lang.c++:
J. Campbell wrote:
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;
}

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".


Well, no, not really. "\r\n" in MS-DOS binary files is indeed
equivalent to '\n' in MS-DOS/Windows text files, but not equivalent to
std::endl, which is '\n' followed by a flush.

--
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
Jul 19 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: new | last post by:
Question: What would be the best way to add carrage returns to a record, and would my method create alot of overhead and wasted space. What would be the best method to minimize overhead and wasted...
1
by: Frank | last post by:
Dumb question. Thanks to help from this group I have crteated text file resulting from an xml transformed by an xsl file. I need to imbed a carrage return to separate the element values. None of...
2
by: PengYu.UT | last post by:
I have the following sample program, which can convert function object with 1 argument into function object with 2 arguments. It can also do + between function object of the same type. The last...
3
by: David N. | last post by:
Hi All, I spent too much time on trying to get the CrLf into a string, which contains embedded SQL statements that can be executed by the SQLClient.SqlCommand. Note that these SQL statements...
2
by: sean | last post by:
Can anyone tell me how to add a carriage return to a string using vb.net? I need to add two strings with a carrage return together and post to an email message body ie: This gives me an...
5
by: Jay | last post by:
Recieving data from an IRC server the controlchars.newline works to seperate some pieces of data into their own lines. However it doesnt always work and some whole pices of dat are LEFT OUT of the...
2
by: Contro | last post by:
Hi guys! I was wondering if you could help me. Basically, I'm wanting to do a sort of auto complete in an access text box, with the help of visual basic, where the user will put in, for...
3
by: kikazaru | last post by:
Is it possible to return covariant types for virtual methods inherited from a base class using virtual inheritance? I've constructed an example below, which has the following structure: Shape...
46
by: Bill Cunningham | last post by:
I have heard that return -1 is not portable. So what would be the answer to portability in this case? What about exit(0) and exit (-1) or exit (1)? Or would it be best to stick with C's macros,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.