Problem with String Append or Cout

O

olson_ord

Hi,
I am not so new to C++, but I have not used it much. I was trying to
append a string at the end of a previous string. If I just do this in a
test program that is 3 lines long i.e. define the two strings and
append one to the other this works. However in my actual program I
cannot get this to work.
A shorter version of my code where I still cannot get the appending to
work is as follows.

--------------------- main.cpp --------------------------

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

int main()
{
string iscas_file_name = "c17.bench";
ifstream iscas_file (iscas_file_name.c_str());
if(!iscas_file)
{
cout<< endl << "Error opening the file: " << iscas_file_name<<
" for loading.";
}

string line;
while (getline(iscas_file,line,'\n'))
{
string temp = "** ";
line.append(temp);
cout<<line<<endl;
}

return 0;
}

-------------------- End of main.cpp ---------------------------

-------------------------- c17.bench ---------------------------
# c17
# 5 inputs
# 2 outputs
# 0 inverter
# 6 gates ( 6 NANDs )
----------------End of c17.bench ------------------------

--------- Output Obtained ----------------
** 17
** inputs
** outputs
** inverter
** gates ( 6 NANDs )
--------- End of Output Obtained ----------------

If I pipe this output to a file - I receive the correct output -
but if I 'cout' on the terminal its wrong. (I am using
'Konsole' on KDE.) So what am I doing wrong?

I am sorry if this question is obvious - but its new to me.
Thanks a lot.
O.O.
 
V

Victor Bazarov

I am not so new to C++, but I have not used it much. I was trying to
append a string at the end of a previous string. If I just do this in a
test program that is 3 lines long i.e. define the two strings and
append one to the other this works. However in my actual program I
cannot get this to work.
A shorter version of my code where I still cannot get the appending to
work is as follows.

--------------------- main.cpp --------------------------

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

int main()
{
string iscas_file_name = "c17.bench";
ifstream iscas_file (iscas_file_name.c_str());
if(!iscas_file)
{
cout<< endl << "Error opening the file: " << iscas_file_name<<
" for loading.";
}

string line;
while (getline(iscas_file,line,'\n'))
{
string temp = "** ";
line.append(temp);
cout<<line<<endl;
}

return 0;
}

-------------------- End of main.cpp ---------------------------

-------------------------- c17.bench ---------------------------
# c17
# 5 inputs
# 2 outputs
# 0 inverter
# 6 gates ( 6 NANDs )
----------------End of c17.bench ------------------------

--------- Output Obtained ----------------
** 17
** inputs
** outputs
** inverter
** gates ( 6 NANDs )
--------- End of Output Obtained ----------------

If I pipe this output to a file - I receive the correct output -
but if I 'cout' on the terminal its wrong. (I am using
'Konsole' on KDE.) So what am I doing wrong?

I am sorry if this question is obvious - but its new to me.

There is nothing obvious about it, but you can be running into some
special behaviour of your console when your program reads the \r char
from the file and outputs it to the console -- the characters it output
just before get written over. Try

cout << "This text goes first" << '\r' << " and then this\n";

and see the difference on your screen and in the file (if you "pipe it
to" a file).

V
 
D

Dietmar Kuehl

while (getline(iscas_file,line,'\n'))
{
string temp = "** ";
line.append(temp);
cout<<line<<endl;
}
-------------------------- c17.bench ---------------------------
# c17
# 5 inputs
# 2 outputs
# 0 inverter
# 6 gates ( 6 NANDs )
----------------End of c17.bench ------------------------

--------- Output Obtained ----------------
** 17
** inputs
** outputs
** inverter
** gates ( 6 NANDs )
--------- End of Output Obtained ----------------

This looks very much as if you have a file following the Windows
conventions for end of line (i.e. using "\r\n" as line end). In
this case, the output is formatted strangely due to the carriage
return character ('\r') at the end of the line. To verify this
theory, you should closely inspect the actual output e.g. using
a tool like od(1):

program <args> | od -c | less

This will show each individual character including its code. You
should see literal '\r' character in front of the asterisks.
 
O

olson_ord

Thank you, Victor and Dietmar for responding to this post. As both of
you guessed correctly the problem here is with the '\r'.

I tried Dietmar's suggestion of using "od -c" and I then piped
this output to a file so that you guys can take a look. Here's what I
get


0000000 # c 1 7 \r * * \n # 5 i n
0000020 p u t s \r * * \n # 2 o u t
0000040 p u t s \r * * \n # 0 i n v
0000060 e r t e r \r * * \n # 6 g a
0000100 t e s ( 6 N A N D s ) \r
0000120 * * \n
0000124


This means that when I append a string to another string there is a
default '\r' added in between. Is this expected behavior. (The code of
my program is in my original post. I have not changed anything here
with the exception of piping the output through "od -c". )

I should mention that I am working on Linux for compiling and running
my program. (I am nothing of a Linux geek - probably not even much of
a beginner.)

Thanks a lot for your help
O.O.
 
O

olson_ord

I am sorry to have overlooked Dietmar's comment in his post that the
possible problem may be in my input file - even though I am working
on Linux. It turns out that my file was indeed using "\r\n" as line end
- and this turned out to be the problem.

Using something like

line.erase(line.find('\r', 1));

somewhere inside the while() loop above fixes all of my problem.

Thank you guys for your help, and I am sorry that this was not a C++
problem after all.
Regards,
O.O.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,273
Latest member
DamonShoem

Latest Threads

Top