Solved

concatenating character to string.

Posted on 2002-03-20
13
243 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Handle Exceptions during instantiation 28 428
I could not build boost code, 10 106
White board coding practice 3 93
How to increment counter variables in ANSI  C (Not C# or C++). 9 39
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
  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 …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

740 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