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

Unable to correctly concatenate a string variable

P: n/a
I have been to recreate a problem that I am having with strings with
the trivial code snippet given below. In the trivial code example, I
am reading five lines from a data file, each line having only one word.
I try to concatenate each word read into a string variable, separated
by a delimiter character. Even though I am able to read the words from
the file correctly, I am not able to correctly concatenate the string.
I was expecting the output to be a concatenated string containing all
the words, separated by the delimiter character, but that is not what
is happening.

I know that I am doing something silly. Any help will be appreciated.

Song

Code snipped follows

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

#define WORD_SEPARATOR ":"
#define DATAFILE "./data.dat"

//////////////////////////////////////////////////////////////
//
//
//////////////////////////////////////////////////////////////
bool
readRandomLine(const string fileName, string& line)
{
bool ret = false;

ifstream ifs(fileName.c_str());
if(!ifs)
{
cerr << "Cannot open file " << fileName << " ....ignoring" << endl;
return ret;
}

string token;
unsigned long nlCount = 0;
while(getline(ifs, token))
nlCount++;

if(nlCount)
{
ifs.clear();
ifs.seekg(0);

int randLineNum = rand()%nlCount + 1;

for(unsigned long i = 1; (i <= randLineNum) && getline(ifs, token);
i++);

if(ifs)
{
line = token;
ret = true;
}
}

ifs.close();
return ret;
}
//////////////////////////////////////////////////////////////
//
//
//////////////////////////////////////////////////////////////
bool
getWord(string& word, string fileName)
{
bool status = true;

status = readRandomLine(fileName, word);
return status;
}

//////////////////////////////////////////////////////////////
//
//
//////////////////////////////////////////////////////////////
void
fillString(string& val)
{
for(int i = 0; i < 5; i++)
{
string nextWord;
if(!getWord(nextWord, DATAFILE))
break;
else
{
cout << "Fetched word " << nextWord << endl;
val += nextWord;
val += WORD_SEPARATOR;
}
}

cout << endl;
}
//////////////////////////////////////////////////////////////
//
//
//////////////////////////////////////////////////////////////
main()
{
srand(time(NULL));
rand();

string valStr;
fillString(valStr);

cout << "The value string is: " << endl
<< valStr << endl;

return 0;
}

/////////////////////// Data File "./data.dat" ///////////////////
oak
object
objection
objective
obligation
observation
observer
obstacle
occasion
occupation
occurrence
ocean
Octavia
Octavio
odds
of

Sep 1 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Generic Usenet Account wrote:
I have been to recreate a problem that I am having with strings with
the trivial code snippet given below. In the trivial code example, I
am reading five lines from a data file, each line having only one
word. I try to concatenate each word read into a string variable,
separated by a delimiter character. Even though I am able to read
the words from the file correctly, I am not able to correctly
concatenate the string. I was expecting the output to be a
concatenated string containing all the words, separated by the
delimiter character, but that is not what is happening.
What *is* happening?
[...]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 1 '06 #2

P: n/a
Generic Usenet Account wrote:
I know that I am doing something silly. Any help will be appreciated.
Compiles and runs fine for me. What are you experiencing?

Jens
Sep 2 '06 #3

P: n/a
Hi,

I think you should have linked your data file in your message instead of
including it.

Are you using a windows file on an linux/unix system perhaps? Then there
will be a carriage return at the end of each word (i.e. if you output it
normaly only the last word will show completely with parts of previous words
after it.

Give more info or the format of your input file.

--
Regards, Ron AF Greve

http://moonlit.xs4all.nl

"Generic Usenet Account" <us****@sta.samsung.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
>I have been to recreate a problem that I am having with strings with
the trivial code snippet given below. In the trivial code example, I
am reading five lines from a data file, each line having only one word.
I try to concatenate each word read into a string variable, separated
by a delimiter character. Even though I am able to read the words from
the file correctly, I am not able to correctly concatenate the string.
I was expecting the output to be a concatenated string containing all
the words, separated by the delimiter character, but that is not what
is happening.

I know that I am doing something silly. Any help will be appreciated.

Song

Code snipped follows

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

#define WORD_SEPARATOR ":"
#define DATAFILE "./data.dat"

//////////////////////////////////////////////////////////////
//
//
//////////////////////////////////////////////////////////////
bool
readRandomLine(const string fileName, string& line)
{
bool ret = false;

ifstream ifs(fileName.c_str());
if(!ifs)
{
cerr << "Cannot open file " << fileName << " ....ignoring" << endl;
return ret;
}

string token;
unsigned long nlCount = 0;
while(getline(ifs, token))
nlCount++;

if(nlCount)
{
ifs.clear();
ifs.seekg(0);

int randLineNum = rand()%nlCount + 1;

for(unsigned long i = 1; (i <= randLineNum) && getline(ifs, token);
i++);

if(ifs)
{
line = token;
ret = true;
}
}

ifs.close();
return ret;
}
//////////////////////////////////////////////////////////////
//
//
//////////////////////////////////////////////////////////////
bool
getWord(string& word, string fileName)
{
bool status = true;

status = readRandomLine(fileName, word);
return status;
}

//////////////////////////////////////////////////////////////
//
//
//////////////////////////////////////////////////////////////
void
fillString(string& val)
{
for(int i = 0; i < 5; i++)
{
string nextWord;
if(!getWord(nextWord, DATAFILE))
break;
else
{
cout << "Fetched word " << nextWord << endl;
val += nextWord;
val += WORD_SEPARATOR;
}
}

cout << endl;
}
//////////////////////////////////////////////////////////////
//
//
//////////////////////////////////////////////////////////////
main()
{
srand(time(NULL));
rand();

string valStr;
fillString(valStr);

cout << "The value string is: " << endl
<< valStr << endl;

return 0;
}

/////////////////////// Data File "./data.dat" ///////////////////
oak
object
objection
objective
obligation
observation
observer
obstacle
occasion
occupation
occurrence
ocean
Octavia
Octavio
odds
of

Sep 2 '06 #4

P: n/a
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Jens Theisen wrote:
Generic Usenet Account wrote:
I know that I am doing something silly. Any help will be appreciated.

Compiles and runs fine for me. What are you experiencing?

Jens
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Victor Bazarov wrote:
What *is* happening?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Moonlit wrote:
Hi,

I think you should have linked your data file in your message instead of
including it.

Are you using a windows file on an linux/unix system perhaps? Then there
will be a carriage return at the end of each word (i.e. if you output it
normaly only the last word will show completely with parts of previous words
after it.

Give more info or the format of your input file.

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

For your benefit, I am providing the expected output and the actual
output. Kindly realize that what you get may be slightly different
from what I got since I am reading five lines (each having one word)
randomly from the file.

Thanks,
Song
Expected output:
================
Fetched word obligation
Fetched word occasion
Fetched word occurrence
Fetched word ocean
Fetched word Octavio

The value string is:
obligation:occasion:occurrence:ocean:Octavio

Actual output:
==============
Fetched word obligation
Fetched word occasion
Fetched word occurrence
Fetched word ocean
Fetched word Octavio

The value string is:
:Octavionce

Sep 2 '06 #5

P: n/a
Hi,

Ok, that is exactly what I said. The last part of occurence and the first
part Octavio. The whole string is there but because the carriage return
jumps back to the beginning of the line you only see part of the words and
the last word completely

occurrence
Octavio
solution. After reading Line from your file remove the carriage return:

#include <algorithm>

Line.erase( remove_if( Line.begin(), Line.end(), bind2nd( equal_to<char>(),
'\r' ) ), Line.end() );
--
Regards, Ron AF Greve

http://moonlit.xs4all.nl

<us****@sta.samsung.comwrote in message
news:11*********************@74g2000cwt.googlegrou ps.com...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Jens Theisen wrote:
>Generic Usenet Account wrote:
I know that I am doing something silly. Any help will be appreciated.

Compiles and runs fine for me. What are you experiencing?

Jens

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Victor Bazarov wrote:
>What *is* happening?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Moonlit wrote:
>Hi,

I think you should have linked your data file in your message instead of
including it.

Are you using a windows file on an linux/unix system perhaps? Then there
will be a carriage return at the end of each word (i.e. if you output it
normaly only the last word will show completely with parts of previous
words
after it.

Give more info or the format of your input file.

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

For your benefit, I am providing the expected output and the actual
output. Kindly realize that what you get may be slightly different
from what I got since I am reading five lines (each having one word)
randomly from the file.

Thanks,
Song
Expected output:
================
Fetched word obligation
Fetched word occasion
Fetched word occurrence
Fetched word ocean
Fetched word Octavio

The value string is:
obligation:occasion:occurrence:ocean:Octavio

Actual output:
==============
Fetched word obligation
Fetched word occasion
Fetched word occurrence
Fetched word ocean
Fetched word Octavio

The value string is:
:Octavionce

Sep 2 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.