Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

concatenating character to string.

Posted on 2002-03-20
13
Medium Priority
?
254 Views
Last Modified: 2010-04-02
Hi all
i wanna create word by adding characters one by one.
my code:

char c;
char *word;

word="";
c=fgetc(fp);
strcat(word,c);  // ERROR

could u help me in this problem?
0
Comment
Question by:MEshtay
  • 4
  • 4
  • 2
  • +2
13 Comments
 
LVL 2

Accepted Solution

by:
jonnin earned 200 total points
ID: 6882337
one way:
char ch[2];
ch[0] = fgetc(fp);
ch[1] = 0;

strcat(str,ch);

or:

str[place] = fgetc(fp);
str[place+1] = 0;



0
 
LVL 1

Expert Comment

by:pzpn
ID: 6882407
Would this be anything to do with the fact the word is not NULL terminated?

Just a suggestion....
0
 
LVL 86

Expert Comment

by:jkr
ID: 6882415
char c;
char *word;

word="";
c=fgetc(fp);
strncat(word,&c,1);  // NO ERROR

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 86

Expert Comment

by:jkr
ID: 6882439
Ooops, correction - it has to be

#define MAX_WORD 255
char c;
char word[MAX_WORD]="";

c=fgetc(fp);
strncat(word,&c,1);  // NO ERROR

If you have a constant string literal like

word="";

you CANNOT concatenate anything without a crash...
0
 

Expert Comment

by:boneTKE
ID: 6882592
The underlying problem with your original example is that no memory has been allocated to your char* word.

If you define word as jkr did, char word[255], you will have 255 bytes allocated to word.

Alternately, you could get the memory dynamically,
char* word = (char*) malloc(sizeof(char) * 255);

Good luck.
0
 
LVL 2

Expert Comment

by:jonnin
ID: 6882604
yes, its for the 0 (null) terminated string. I forgot you can add a length to strcat, that is cleaner. Haven't used it in a while....

Reading directly into the string in a loop then ending it with the zero when done removes the need to call strcat at all. Anyway, any of these solutions will work for you...  

0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6884331
You need to add a few random characters to the final command:

char word[100]="";
c=fgetc(fp);

strncat(word,&c,1);  // n & ,1 <---- NO ERROR

-- Dan
0
 
LVL 1

Expert Comment

by:pzpn
ID: 6884924
jonnin - is it not strncat that takes a length. i.e. number of bytes to copy?
0
 
LVL 2

Expert Comment

by:jonnin
ID: 6885276
I looked it up, and:

the only strcat I can find in my reference materials is
strcat(dest,src); //stops copying at the 0/null char

so either there is an enhanced one that takes a length or it's compiling and ignoring the extra parameter. This would crash unless the memory violated happened to belong to the app AND a null char was quickly found (much of ram is often 0, no surpise here).


So I'm sticking to my orig post, manually null terminate or set up the string functions to do so. And allocate memory for the thing, or make array, as was mentioned by boneTke.








0
 
LVL 1

Expert Comment

by:pzpn
ID: 6885343
jonnin - I totally agree that the character array has to be properly initialised and be null terminated, I was just saying that the string concatenation function that takes a length (or size in bytes) is called strncat (note the 'n').
0
 
LVL 2

Expert Comment

by:jonnin
ID: 6885656
Ah. I can't see well and missed the n.  In all the posts, I missed it... VERY Sorry for the confusion!

Yes, which means strNcat will work fine... which is why I didn't remember strcat taking a length (it didn't!)...
feel free to forget I said anything =)




0
 
LVL 1

Expert Comment

by:pzpn
ID: 6886805
Happens to the best of us mate ;)
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6887049
The trick is to insert random letters into the code at random places.   Sometimes symbols, such as & and [ can come in handy too.  That little dot above the comma has been useful in some assignments.  When it compiles without error, you can turn it in and get an A.

-- Dan
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question