locating a carriage return in C++

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
Laura D said:
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.
 
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
 
Laura D said:
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...
 
More importantly, why are we discussing C++ in a C# NG?

BW



Laura D said:
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
 
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.
 
Back
Top