Solved

concatenating character to string.

Posted on 2002-03-20
13
250 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +2
13 Comments
 
LVL 2

Accepted Solution

by:
jonnin earned 50 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

636 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