在C使用的strcat [英] Using strcat in C
问题描述
好了,所以我有以下的code这将一个字符串追加到另一个在C#中,请注意,这只是一个示例,所以让在C#替代字符串concatination方法不是所必要的,这只是为了简化示例
Okay so I have the following Code which appends a string to another in C#, note that this is Just an example, so giving alternative string concatination methods in C# is not nessesary, this is just to simplify the example.
string Data = "";
Data +="\n\nHTTP/1.1 " + Status_code;
Data += "\nContent-Type: " + Content_Type;
Data += "\nServer: PT06";
Data += "\nContent-Length: " + Content_Lengt;
Data += "\nDate: " + Date;
Data += "\n" + HTML;
现在我想要做同样的事情在C和我试图做到这一点通过以下方式
Now I'd like to do the exact same thing in C and I'm trying to do this the following way
time_t rawtime;
time ( &rawtime );
char *message = "\n\nHTTP/1.1 ";
message = strcat(message, Status_code);
message = strcat(message, "\nContent-Type: ");
message = strcat(message, Content_Type);
message = strcat(message, "\nServer: PT06");
message = strcat(message, "\nContent-Length: ");
message = strcat(message, Content_Lengt);
message = strcat(message, "\nDate: ");
message = strcat(message, ctime(&rawtime));
message = strcat(message, "\n");
message = strcat(message, HTML);
现在,这给了我一个分部的错,我知道为什么,我访问和存储器读,我不应该。但问题是,如何解决这个问题呢?我可以使用文件string.h和就去做,我在C#一样的工作方式?
Now, this gives me a Segment fault, I know why, I access and read on memory that i shouldn't. But the question is, how do i solve it? Could I use string.h and just do it the same way that I did in C#?
推荐答案
修改
char *message = "\n\nHTTP/1.1 ";
到
char message[1024];
strcpy(message,"\n\nHTTP/1.1 ");
和你应该确定,到1023总消息长度。
and you should be ok, up to a total message length of 1023.
编辑:(按MJY的评论)。以这种方式使用strcat的是获得缓冲区溢出的一个好方法。你可以很容易地写出一个小型函数检查传入串另外的缓冲器和长度来克服这个问题,或者在一个动态缓冲器使用的realloc的大小。海事组织,责任是程序员,检查他们使用,如* sprintf的* S和其他C字符串函数正确的缓冲区大小。我假设正在使用下,用C ++性能方面的原因,因此STL不是一个选项。
(as per mjy's comment). Using strcat in this fashion is a great way of getting buffer overflows. You could readily write a small function that checks the size of the buffer and length of incoming string addition to overcome this, or use realloc on a dynamic buffer. IMO, the onus is on the programmer to check correct buffer sizes where they are used, as with *sprintf*s and other C strings functions. I assume that C is being used over C++ for performance reasons, and hence STL is not an option.
编辑:按照从菲利普的评论,基于固定大小的char缓冲区的简单strcat的实施要求:
As per request from Filip's comment, a simple strcat implementation based on a fixed size char buffer:
char buffer[MAXSIZE] = "";
int mystrcat(char *addition)
{
if (strlen(buffer) + strlen(addition) + sizeof(char) >= MaxSize)
return(FAILED);
strcat(buffer,addition);
return(OK);
}
使用动态分配的:
Using dynamic allocation:
char *buffer = NULL;
int mystrcat(char *addition)
{
buffer = realloc(buffer, strlen(buffer) + strlen(addition) + sizeof(char));
if (!buffer)
return(FAIL);
strcat(buffer, addition);
return(OK);
}
在这种情况下,你必须手动释放你的缓冲区,当你用它完成。 (由析构函数在C ++中等值处理)
In this case you have to free your buffer manually when you are finished with it. (Handled by destructors in C++ equivalents)
附录(大同):
好吧,因为你没有真正的说明为什么你必须创建信息[1024]
,在这儿呢。
Okay, since you didn't actually explain why you had to create message[1024]
, here it is.
使用的char * x =你好,实际字节('H','E','L','L','O',0)(上年末为null)都存放在一个区域存储器从变量分离(并很可能只读)和变量x被设为指向它。空之后,有可能是别的东西很重要。所以你不能在所有附加到这一点。
With char *x = "hello", the actual bytes ('h','e','l','l','o',0) (null on the end) are stored in an area of memory separate from the variables (and quite possibly read-only) and the variable x is set to point to it. After the null, there's probably something else very important. So you can't append to that at all.
使用字符X [1024];的strcpy(X,你好);
,你第一次分配1K OM内存,这是完全致力于为x。然后复制你好了进去,并仍然有相当多的空间在年底以追加更多的字符串。直到追加超过1K多元允许更多的你会不会惹上麻烦。
With char x[1024]; strcpy(x,"hello");
, you first allocate 1K om memory which is totally dedicated to x. Then you copy "hello" into it, and still leave quite a bit of space at the end for appending more strings. You won't get into trouble until you append more than the 1K-odd allowed.
末增编(大同):
这篇关于在C使用的strcat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!