Solved

concatenating character to string.

Posted on 2002-03-20
13
229 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
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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

777 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