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
我如何将一个字符转换成c中的字符串,我现在使用的是返回一个字符的c = fgetc(fp)。但是我需要一个字符串在strcpy中使用。
9 个解决方案
#1
6
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.
您可以使用循环中的第二行来使用其他字符串操作,您希望继续使用char作为字符串。
#2
5
Using fgetc(fp)
only to be able to call strcpy(buffer,c);
doesn't seem right.
使用fgetc(fp)只能调用strcpy(缓冲区,c);似乎不正确。
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
注意,这依赖于您将读取小于MAX_SIZE_OF_MY_BUFFER字符的事实。
#3
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';
#4
1
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,那么你可以这样做:
...
strcpy( ... , (char[2]) { (char) c, '\0' } );
...
The (char[2]) { (char) c, '\0' }
part will temporarily generate null-terminated string out of a character c
.
(char[2]) {(char) c, '\0'}部分将临时地从字符c中生成以null结尾的字符串。
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.
通过这种方式,您可以避免为已经掌握在您手中的东西创建新的变量,前提是您只需要一个单字符字符串。
#5
1
I use this to convert char to string (an example) :
我使用它将char转换为string(示例):
char c = 'A';
char str1[2] = {c , '\0'};
char str2[5] = "";
strcpy(str2,str1);
#6
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:
这是一个老问题,但我认为没有一个答案能真正符合OP的问题。他所要做的就是:
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数组/ c-string。实际上,所有参数都不是char或char。它们都是char指针:
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
\0
within its remaining elements (starting withsrc
), to mark the end of the c-string that should be copied.
dest :
A non-const char pointer
src :
A const char pointer
#7
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.
在c中没有字符串数据类型,使用字符数组来存储和操作该值。将变量c更改为一个字符数组,并在循环中使用它来获取值。
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.
另一种方法是使用指针并动态分配内存和赋值。
#8
0
//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
#9
0
Here is a working exemple :
这里有一个工作例子:
printf("-%s-", (char[2]){'A', 0});
This will display -A-
这将显示——
#1
6
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.
您可以使用循环中的第二行来使用其他字符串操作,您希望继续使用char作为字符串。
#2
5
Using fgetc(fp)
only to be able to call strcpy(buffer,c);
doesn't seem right.
使用fgetc(fp)只能调用strcpy(缓冲区,c);似乎不正确。
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
注意,这依赖于您将读取小于MAX_SIZE_OF_MY_BUFFER字符的事实。
#3
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';
#4
1
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,那么你可以这样做:
...
strcpy( ... , (char[2]) { (char) c, '\0' } );
...
The (char[2]) { (char) c, '\0' }
part will temporarily generate null-terminated string out of a character c
.
(char[2]) {(char) c, '\0'}部分将临时地从字符c中生成以null结尾的字符串。
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.
通过这种方式,您可以避免为已经掌握在您手中的东西创建新的变量,前提是您只需要一个单字符字符串。
#5
1
I use this to convert char to string (an example) :
我使用它将char转换为string(示例):
char c = 'A';
char str1[2] = {c , '\0'};
char str2[5] = "";
strcpy(str2,str1);
#6
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:
这是一个老问题,但我认为没有一个答案能真正符合OP的问题。他所要做的就是:
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数组/ c-string。实际上,所有参数都不是char或char。它们都是char指针:
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
\0
within its remaining elements (starting withsrc
), to mark the end of the c-string that should be copied.
dest :
A non-const char pointer
src :
A const char pointer
#7
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.
在c中没有字符串数据类型,使用字符数组来存储和操作该值。将变量c更改为一个字符数组,并在循环中使用它来获取值。
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.
另一种方法是使用指针并动态分配内存和赋值。
#8
0
//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
#9
0
Here is a working exemple :
这里有一个工作例子:
printf("-%s-", (char[2]){'A', 0});
This will display -A-
这将显示——