How do I convert a character to a string in C. I'm currently using c = fgetc(fp) which returns a character. But I need a string to be used in strcpy
- 6,239
- 3
- 35
- 44
- 324
- 1
- 3
- 7
-
4Store all character in an array and then store `\0` as a last element of array. That's it. – haccks Mar 24 '14 at 22:33
-
Could you be more specific? Based on you description, you could try something like `fscanf(pf, "%s", &buf)`. – gongzhitaao Mar 24 '14 at 22:34
-
int c; c=fgetc(fp)//get character by character from file and I need to be able to do strcpy(buffer,c); – user1911575 Mar 24 '14 at 22:36
-
why do you need to use it with `strcpy()`, exactly? – Heeryu Mar 24 '14 at 22:45
-
3I'm amazed at how much traffic and debate this simple question has generated. The solution is two lines of code. TWO. I'm upvoting the question simply because he made a bunch of pros fall all over themselves. – Carey Gregory Mar 25 '14 at 01:45
9 Answers
To answer the question without reading too much else into it i would
char str[2] = "\0"; /* gives {\0, \0} */
str[0] = fgetc(fp);
You could use the second line in a loop with what ever other string operations you want to keep using char's as strings.
- 1,029
- 9
- 15
Using fgetc(fp) only to be able to call strcpy(buffer,c); doesn't seem right.
You could simply build this buffer on your own:
char buffer[MAX_SIZE_OF_MY_BUFFER];
int i = 0;
char ch;
while (i < MAX_SIZE_OF_MY_BUFFER - 1 && (ch = fgetc(fp)) != EOF) {
buffer[i++] = ch;
}
buffer[i] = '\0'; // terminating character
Note that this relies on the fact that you will read less than MAX_SIZE_OF_MY_BUFFER characters
You could do many of the given answers, but if you just want to do it to be able to use it with strcpy, then you could do the following:
...
strcpy( ... , (char[2]) { (char) c, '\0' } );
...
The (char[2]) { (char) c, '\0' } part will temporarily generate null-terminated string out of a character c.
This way you could avoid creating new variables for something that you already have in your hands, provided that you'll only need that single-character string just once.
- 2,901
- 1
- 12
- 28
-
2That's the most syntactically obscure way of copying a string I can imagine. Not what I would suggest to a newb. – Carey Gregory Mar 24 '14 at 23:10
-
2@CareyGregory I agree with that, but I think my explanation is short and understandable; making it enough not-as-much-obscure for anyone. – Utkan Gezer Mar 24 '14 at 23:15
-
Excellent suggestion, but now, (MSVC: compiling as C++) it gives: "C4576: a parenthesized type followed by an initializer list is a non-standard explicit type conversion syntax." Explained a bit [here](https://stackoverflow.com/questions/33270731/error-c4576-in-vs2015-enterprise). – Laurie Stearn Jan 26 '19 at 14:02
I use this to convert char to string (an example) :
char c = 'A';
char str1[2] = {c , '\0'};
char str2[5] = "";
strcpy(str2,str1);
- 49
- 3
A code like that should work:
int i = 0;
char string[256], c;
while(i < 256 - 1 && (c = fgetc(fp) != EOF)) //Keep space for the final \0
{
string[i++] = c;
}
string[i] = '\0';
- 619
- 5
- 13
-
2It's better to type `'\0'` rather than `0` since `'\0'` explicitly expresses that "I am assigning the terminating character here". – LihO Mar 24 '14 at 23:06
-
1I think the C standard rather mention 0 instead of '\0', which simply is interpreted as 0, but I guess it depends of your coding style. I did a lot of string manipulation lately, and I got used to 0, as its faster to write ^^ – Taiki Mar 24 '14 at 23:10
//example
char character;//to be scanned
char merge[2];// this is just temporary array to merge with
merge[0] = character;
merge[1] = '\0';
//now you have changed it into a string
- 37
- 5
-
This answer is correct but [code-only answers should be avoided](http://meta.stackoverflow.com/a/303605/4284627). Please edit to add an explanation (you can look at the other answers to see how such an explanation can look). – Donald Duck Dec 23 '16 at 20:29
This is an old question, but I'd say none of the answers really fits the OP's question. All he wanted/needed to do is this:
char c = std::fgetc(fp);
std::strcpy(buffer, &c);
The relevant aspect here is the fact, that the second argument of strcpy() doesn't need to be a char array / c-string. In fact, none of the arguments is a char or char array at all. They are both char pointers:
strcpy(char* dest, const char* src);
- Its value has to be the memory address of an element of a writable char array (with at least one more element after that).
- Its value can be the address of a single char, or of an element in a char array. That array must contain the special character
\0within its remaining elements (starting withsrc), to mark the end of the c-string that should be copied.
dest : A non-const char pointersrc : A const char pointer- 41
- 8
FYI you dont have string datatype in C. Use array of characters to store the value and manipulate it. Change your variable c into an array of characters and use it inside a loop to get values.
char c[10];
int i=0;
while(i!=10)
{
c[i]=fgetc(fp);
i++;
}
The other way to do is to use pointers and allocate memory dynamically and assign values.
- 339
- 3
- 19
-
Make your char array sufficiently large, add '\0' character at the end, to denote the end marker. – Dhananjayan Santhanakrishnan Mar 24 '14 at 22:44