473,703 Members | 2,341 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

locating a carriage return in C++

How can I identify a carriage return in C++?
\r, \f, \0, \n, \t does not work. I have also tried !isprint(ch), iscntrl(ch), isspace(ch), etc....with no luck!
I even poked around in the MSDN and found some code that MS claims will save a file in unix format and I cut and pasted into my program(and made changes to suit):
.....
char ch;
char temp[MAX_PATH]="\0";

//Open the file for reading in binarymode.
ifstream fp_read(filenam e.c_str(), ios_base::in
| ios_base::binar y);

if(!fp_read)
cout << "could not open " << filename.c_str( ) << endl;
else
cout << "opened for read: " << filename.c_str( ) << endl;

sprintf(temp, "%s.temp", filename.c_str( ));
//Create a temporary file for writing in the binary mode. This
//file will be created in the same directory as the input file.
ofstream fp_write(temp, ios_base::out
| ios_base::trunc
| ios_base::binar y);

if(!fp_write)
cout << "could not open fp_write" << endl;
else
cout << "opened fp_write " << endl;

while(fp_read.e of() != true)
{
fp_read.get(ch) ;
//Check for CR (carriage return)
if((int)ch == 0x0D)
continue;
if (!fp_read.eof() )fp_write.put(c h);
}

fp_read.close() ;
fp_write.close( );
//Delete the existing input file.
remove(filename .c_str());
//Rename the temporary file to the input file.
rename(temp, filename.c_str( ));
//Delete the temporary file.
remove(temp);
.....
This does not work either.
Is there a sequence of characters that can be used to compare to a MS carriage return?
Thanks
Laura

Nov 15 '05 #1
6 17592
"Laura D" <an*******@disc ussions.microso ft.com> wrote:
How can I identify a carriage return in C++?


What do you mean?

A carriage return can be specified with '\r' or (char)13.

On Windows, end of line indicators are carriage returns followed by
line feeds ("\r\n").

On *nix, end of line indicators are just line feeds - '\n' or
(char)10.
Nov 15 '05 #2
I have collected strings from a comma separated file generated by Excel and stored them in a deque<deque<str ing>> FileInStringTok ens. When I compare these strings one by one to "\r\n", I get nothing. But when I print FileInStringTok ens to a output file, the carriage returns are in fact there

Nov 15 '05 #3
"Laura D" <an*******@disc ussions.microso ft.com> wrote:
I have collected strings from a comma separated file generated by Excel and stored them in a deque<deque<str ing>> FileInStringTok ens. When I compare these strings one by one to "\r\n", I get nothing. But when I print FileInStringTok ens to a output file, the carriage returns are in fact there?


Perhaps, then, what you're using to read from the file converts all
CR+LFs to either single LFs or single CRs, and converts them back on
writing to the file.

You could verify this by debugging of course...
Nov 15 '05 #4
I'll keep looking
Thanks.
Nov 15 '05 #5
More importantly, why are we discussing C++ in a C# NG?

BW

"Laura D" <an*******@disc ussions.microso ft.com> wrote in message
news:22******** *************** ***********@mic rosoft.com...
How can I identify a carriage return in C++?
\r, \f, \0, \n, \t does not work. I have also tried !isprint(ch), iscntrl(ch), isspace(ch), etc....with no luck! I even poked around in the MSDN and found some code that MS claims will save a file in unix format and I cut and pasted into my program(and made
changes to suit): ...
char ch;
char temp[MAX_PATH]="\0";

//Open the file for reading in binarymode.
ifstream fp_read(filenam e.c_str(), ios_base::in
| ios_base::binar y);

if(!fp_read)
cout << "could not open " << filename.c_str( ) << endl;
else
cout << "opened for read: " << filename.c_str( ) << endl;

sprintf(temp, "%s.temp", filename.c_str( ));
//Create a temporary file for writing in the binary mode. This
//file will be created in the same directory as the input file.
ofstream fp_write(temp, ios_base::out
| ios_base::trunc
| ios_base::binar y);

if(!fp_write)
cout << "could not open fp_write" << endl;
else
cout << "opened fp_write " << endl;

while(fp_read.e of() != true)
{
fp_read.get(ch) ;
//Check for CR (carriage return)
if((int)ch == 0x0D)
continue;
if (!fp_read.eof() )fp_write.put(c h);
}

fp_read.close() ;
fp_write.close( );
//Delete the existing input file.
remove(filename .c_str());
//Rename the temporary file to the input file.
rename(temp, filename.c_str( ));
//Delete the temporary file.
remove(temp);
...
This does not work either.
Is there a sequence of characters that can be used to compare to a MS carriage return? Thanks
Laura

Nov 15 '05 #6
> Is there a sequence of characters that can be used to compare to a MS
carriage return?

There is no such thing a a Microsoft carriage return :-).

Skimming your code I notice you are opening the file as binary. All you may
be looking for is opening it as text and use a ReadLine kind of method to
read lines one by one. Have another look at that file open method and its
options.

And this is the C# group, not C++.

Martin.
Nov 15 '05 #7

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

Similar topics

3
9311
by: Canes_Rock | last post by:
The information posted at: http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&threadm=slrnarv28n.e4j.TuxTrax%40fortress.tuxnet&rnum=1&prev=/groups%3Fq%3Dsuppress%2Bcarriage%2Breturn%2Bgroup:comp.lang.python.*%26hl%3Den%26lr%3D%26ie%3DUTF-8%26group%3Dcomp.lang.python.*%26selm%3Dslrnarv28n.e4j.TuxTrax%2540fortress.tuxnet%26rnum%3D1 seemed to provide a solution to eliminating the line feed and causing a carriage return for the text displayed...
2
4127
by: Andrew Chanter | last post by:
I have a VBA function that returns a string including "vbcr" (VB Carriage Return) to seperate a list into multiple rows, eg Item1 & vbcr & Item2 & vbcr & Item3 This works as planned in the immediate window, producing a list where a Carriage Return follows each item. But when I output the result into either an Access query or form, instead of carriage returns I get little square symbols between each of the items. Is anyone able to tell...
1
4591
by: chrissmith_76_Fed_Up_With_Spam | last post by:
Hello all, I am using Access 2002, with file format of Access 2000, and am experiencing a problem. I have a subform that is shown in datasheet view for users to edit data direct to a table. When a user presses the Enter (or Return) key a carriage return is made in the cell, rather than the cursor moving to the next field, which is
2
2948
by: eagleofjade | last post by:
I am trying to import data from a Word document into an Access table with VBA. The Word document is a form which has various fields. One of the fields is a field for notes. In some cases, this note field contains carriage returns. When I import a note field that has carriage returns, what shows up in the Access field are vertical black lines where the carriage return should be.
2
6151
by: David Cho | last post by:
I am using this expression \d+(,\s*\d+)* to allow only numbers and commas. But if there are carriage returns mixed, it is not validated. Is tehre a way to ignore all carriage returns? I am a RE newbie. Thanks.
11
8458
by: TheRain | last post by:
Hi, I am trying to append a carriage return to my string using the string builder class, but when I do this the string ends up containing "13". I tried this multiple ways like so sb->Append('\r'); and also
3
3379
by: Dinsdale | last post by:
I have an xml file that is read into an object using serialization. One of the objects has a string field called delimeter that I want to contain a carriage return. Instead of trying to include the carriage return, I used "\r" thinking that when it was read back into the object it would be interpreted as a carriage return. Instead, I am getting the string literal instead of the escape sequence. Does anybody have a slick way of telling it...
11
12595
by: evenlater | last post by:
My db allows the user to send email via CDO. The body of the email is determined in code. I have built an email form with To, CC and Subject lines and a large text box for the body of the message so the user can edit the default email message body before sending the message. But when I populate the large text box (txtBody), I can't get it to include the carriage returns. I've tried vbNewLine, vbCrLf, Chr(10) and Chr(13). I've got the...
0
8759
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8669
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9251
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8963
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3125
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2069
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.