27

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

gongzhitaao
  • 6,566
  • 3
  • 36
  • 44
user1911575
  • 424
  • 1
  • 4
  • 7
  • 5
    Store 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
  • 3
    I'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 Answers9

20

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 whatever other string operations you want to keep using chars as strings.

Michael M.
  • 10,486
  • 9
  • 18
  • 34
Adam
  • 1,059
  • 9
  • 15
8

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.

Utkan Gezer
  • 3,009
  • 2
  • 16
  • 29
  • 3
    That'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
7

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

Taiki
  • 629
  • 5
  • 13
LihO
  • 41,190
  • 11
  • 99
  • 167
5

I use this to convert char to string (an example) :

char c = 'A';
char str1[2] = {c , '\0'};
char str2[5] = "";
strcpy(str2,str1);
2

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';
Taiki
  • 629
  • 5
  • 13
  • 2
    It'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
  • 2
    I 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
1
//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
  • 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
1

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);

dest : A non-const char pointer
Its value has to be the memory address of an element of a writable char array (with at least one more element after that).
src : A const char pointer
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 \0 within its remaining elements (starting with src), to mark the end of the c-string that should be copied.

Floriel
  • 41
  • 8
1

Here is a working exemple :

printf("-%s-", (char[2]){'A', 0});

This will display -A-

John
  • 376
  • 2
  • 9
0

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.