By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,165 Members | 849 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,165 IT Pros & Developers. It's quick & easy.

locating a carriage return in C++

P: n/a
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(filename.c_str(), ios_base::in
| ios_base::binary);

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::binary);

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

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

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
Share this Question
Share on Google+
6 Replies


P: n/a
"Laura D" <an*******@discussions.microsoft.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

P: n/a
I have collected strings from a comma separated file generated by Excel and stored them in a deque<deque<string>> FileInStringTokens. When I compare these strings one by one to "\r\n", I get nothing. But when I print FileInStringTokens to a output file, the carriage returns are in fact there

Nov 15 '05 #3

P: n/a
"Laura D" <an*******@discussions.microsoft.com> wrote:
I have collected strings from a comma separated file generated by Excel and stored them in a deque<deque<string>> FileInStringTokens. When I compare these strings one by one to "\r\n", I get nothing. But when I print FileInStringTokens 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

P: n/a
I'll keep looking
Thanks.
Nov 15 '05 #5

P: n/a
More importantly, why are we discussing C++ in a C# NG?

BW

"Laura D" <an*******@discussions.microsoft.com> wrote in message
news:22**********************************@microsof t.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(filename.c_str(), ios_base::in
| ios_base::binary);

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::binary);

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

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

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

P: n/a
> 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 discussion thread is closed

Replies have been disabled for this discussion.