472,971 Members | 1,824 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,971 software developers and data experts.

Unable to correctly concatenate a string variable

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
5 2765
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
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
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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Len Burman | last post by:
I have some code in a form which inputs a friends email address and when you submit it sends the message to the person at the address. I want to be able to email to the variable name and also a...
10
by: Jack | last post by:
Hi, I cannot get the date format correctly in dynamic sql statement, after trying various ways of handling it. I need some help with the date format in the following dynamic sql statement. Any...
5
by: Henrik | last post by:
The problem is (using MS Access 2003) I am unable to retrieve long strings (255 chars) from calculated fields through a recordset. The data takes the trip in three phases: 1. A custom public...
3
by: nandan | last post by:
Yes. My client has a proxy server. That much i know. In my application I make two kinds of calls that are affected by the proxy server I finally got the webservice call working. But my...
13
by: sinbad | last post by:
hi, how to concatenate a "hash defined" constant value to another "hash defined" constant string. For example #define ABC 100 #define MYSTR "The value of ABC is" Now i need a string that...
10
by: Aaron Hoffman | last post by:
Hello, I'm hoping someone might be able to offer some guidance to my problem. I have one query in MS Access which consists of 2 tables joined by a SEQUENCE_ID. By joining the two tables I am...
1
by: Tony Bansten | last post by:
Assume I want to concatenate a literal text value and a variable value for a function parameter then the following does NOT work: objWorkbook.SaveAs("D:\work\v1_" + Filename) The variable...
3
by: sgmbuk | last post by:
I have no clue why this is happening. The intended behaviour is: 1. Passing strings of numbers using the numbered buttons (1..0). 2. Concatenate the strings above into one and converting it...
53
by: souporpower | last post by:
Hello All I am trying to activate a link using Jquery. Here is my code; <html> <head> <script type="text/javascript" src="../../resources/js/ jquery-1.2.6.js"</script> <script...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.