Solved

concatenating character to string.

Posted on 2002-03-20
13
210 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 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
char c;
char *word;

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

0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
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
Comment Utility
jonnin - is it not strncat that takes a length. i.e. number of bytes to copy?
0
 
LVL 2

Expert Comment

by:jonnin
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Happens to the best of us mate ;)
0
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

771 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now