[英]Convert Char to String in C
How do I convert a character to a string in C. I'm currently using c = fgetc(fp)<\/code> which returns a character.
如何在 C 中将字符转换为字符串。我目前正在使用
c = fgetc(fp)<\/code>返回一个字符。
But I need a string to be used in strcpy
但我需要在 strcpy 中使用一个字符串
"
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作为字符串来保留其他字符串操作。
Using fgetc(fp)
only to be able to call strcpy(buffer,c);
使用
fgetc(fp)
只能调用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 请注意,这取决于您将读取少于
MAX_SIZE_OF_MY_BUFFER
字符的事实
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. 通过这种方式,您可以避免为手中已有的东西创建新变量,前提是您只需要一次单字符串 。
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';
I use this to convert char to string (an example) : 我用它来将char转换为字符串(例子):
char c = 'A';
char str1[2] = {c , '\0'};
char str2[5] = "";
strcpy(str2,str1);
//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 is an old question, but I'd say none of the answers really fits the OP's question. 这是一个古老的问题,但我认为没有一个答案真的符合OP的问题。 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. 这里的相关方面是这样的事实,即
strcpy()
的第二个参数不需要是char数组/ c-string。 In fact, none of the arguments is a char or char array at all. 实际上,根本没有参数是char或char数组。 They are both char pointers:
它们都是char指针:
strcpy(char* dest, const char* src);
dest :
A non-const char pointer
dest :
非const char指针
src :
A const char pointer
src :
一个const char指针
\\0
within its remaining elements (starting with src
), to mark the end of the c-string that should be copied.
src
开始)中包含特殊字符\\0
,以标记应复制的c字符串的结尾。
Here is a working exemple : 这是一个有效的例子:
printf("-%s-", (char[2]){'A', 0});
This will display -A-
这将显示
-A-
FYI you dont have string datatype in C. Use array of characters to store the value and manipulate it. 仅供你在C中使用字符串数据类型。使用字符数组来存储值并对其进行操作。 Change your variable c into an array of characters and use it inside a loop to get values.
将变量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. 另一种方法是使用指针并动态分配内存并分配值。
Exchange functions of data types in C\/C++ convenience. C\/C++ 方便的数据类型交换函数。 In this repo there are convenience functions of all c++ data types and I wrote them all as in python.
在这个 repo 中有所有 c++ 数据类型的便利函数,我把它们都写在 python 中。
Use This Repo:使用这个回购:
https:\/\/github.com\/azizovrafael\/Simple-CPlusPlus<\/a>
https:\/\/github.com\/azizovrafael\/Simple-CPlusPlus<\/a>
...
int main(){
int n = INT(INPUT("Write Some Text For Console Ex. (Enter: )"));
string str = STR(n);
return 0;
}
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.