473,378 Members | 1,555 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,378 software developers and data experts.

Problem with String Append or Cout

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.

Mar 2 '06 #1
4 2589
ol*******@yahoo.it wrote:
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
--
Please remove capital As from my address when replying by mail
Mar 2 '06 #2
ol*******@yahoo.it wrote:
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.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Mar 2 '06 #3
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.

Mar 2 '06 #4
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.

Mar 2 '06 #5

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

Similar topics

4
by: JKop | last post by:
The following isn't doing what I thought it would do: #include <iostream> int main() { int k = 56; std::string blah(" ");
7
by: entropy123 | last post by:
My problem is with the following test code: #include<string>; std::string TestBuff; TestBuff.append("A + B <==> C + D"); std::cout << "TestBuff.length():" << TestBuff.length() << endl;...
23
by: metaosp | last post by:
Hi, in the following code: string foo("foo"); foo.resize(10); foo.append("bar"); cout << foo << endl; // output is "foo" I expect the output to be "foobar", although "bar" is also
7
by: junw2000 | last post by:
In the following code: std::string s1; s1 = "ABCDE"; std::cout<<s1<<std::endl; s1 = '\0'; //LINE1 std::cout<<s1<<std::endl; //LINE2 At LINE1, I add a NULL terminator...
13
by: arnuld | last post by:
/* C++ Primer 4/e * section 3.2 - String Standard Library * exercise 3.10 * STATEMENT * write a programme to strip the punctation from the string. */ #include <iostream> #include...
0
by: ragnarok8990 | last post by:
//BLOOD BANK MANAGEMENT SYSTEM// #include<iostream.h> #include<conio.h> #include<fstream.h> #include<string.h> #include<stdio.h> #include<process.h> fstream file; fstream file1;
10
by: strife | last post by:
Hey everyone, I was making a program for a class, and I ran into a problem that is driving me crazy. I first suspected Vista, and since I am not home I cannot verify if it just my Vista...
5
by: recordlovelife | last post by:
So i have written a code to encode a string of a select amount of letters into huffman code. #include <iostream> #include <string> using namespace std; string Huffman(char letter);...
2
by: dynamo | last post by:
this is a basic linklist,there seems to be a runtime error when i run the program(the main function) i suspect it has something to do with my del function but i see nothing wrong can you help.Thanx...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.