strcat,strcpy,sprintf的问题 [英] Problem with strcat, strcpy,sprintf

查看:74
本文介绍了strcat,strcpy,sprintf的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好


当我这样做时我遇到了一些问题:


void copy(char * filename)

{

char * log_file;

log_file = filename;

strcat(log_file," -log.txt" );


// .......

}


假设filename =" myfile.dat"


我在期待:

log_file =" myfile.dat-log.txt"

这个工作正常....


问题是我需要保留文件名作为原始名称,但

而不是我有:

filename =" myfile.dat-log.txt"


我该如何避免这种情况,并保留原来的名字???


谢谢!!

Hi all

I have certain problem when I''m doing this:

void copy(char *filename)
{
char *log_file;
log_file=filename;
strcat(log_file,"-log.txt");

//.......
}

suppose that filename="myfile.dat"

I''m expecting:
log_file="myfile.dat-log.txt"
and this work fine....

the problem is that I need to remain filename as the original name but
instead i have:
filename="myfile.dat-log.txt"

How can I do to avoid this, and preserve the original name???

Thanks!!

推荐答案

2005年10月28日星期五17:09:43 -0400,< di*************@gmail.com>写道:
On Fri, 28 Oct 2005 17:09:43 -0400, <di*************@gmail.com> wrote:
void copy(char * filename)
{* * char * log_file;
log_file = filename;
strcat(log_file, " -log.txt");

// .......


假设filename =" myfile.dat"

我在期待:
log_file =" myfile.dat-log.txt"
这个工作正常....

问题是我需要保留文件名作为原始名称但是
而不是我有:
filename =" myfile.dat-log.txt"

我该怎么做避免这种情况,并保留原来的名字???
void copy(char *filename)
{
char *log_file;
log_file=filename;
strcat(log_file,"-log.txt");

//.......
}

suppose that filename="myfile.dat"

I''m expecting:
log_file="myfile.dat-log.txt"
and this work fine....

the problem is that I need to remain filename as the original name but
instead i have:
filename="myfile.dat-log.txt"

How can I do to avoid this, and preserve the original name???




当你指定文件名指针时(假设你是如何正确的)
声明了),对于log_file,然后你告诉log_file指向内存中文件名相同的空间。如果你使用strcat,你就会把指针插入到函数中,并将它连接到

那个点。由于filename和log_file都指向内存中相同的

空间,因此将写入相同的空间。


你想要做的是创建文件名指向的内存副本,

并将log_file分配给副本。也就是说,你必须在内存中有两个不同的
空格,这样你就可以将filename指向的空间复制到log_file所指向的空间
,然后你可以在不改变文件名空间的情况下改变log_file指向的空间
,因为

filename指向另一个空格。有意义吗?


想想:


#include< string.h>

#define STRGSIZE 50


void main(无效)

{

char * log_file;

char filename [] = test;


log_file = malloc(STRGSIZE);


strncat(log_file," -log.txt",STRGSIZE) ;

printf(" filename:%s \ nlog_file:%s \ n",filename,log_file);

}


- Arctic


使用Opera革命性的电子邮件客户端: http://www.opera.com/mail/


" ; Arctic Fidelity < SP ** @ sacrificumdeo.net>写道:
"Arctic Fidelity" <sp**@sacrificumdeo.net> writes:
2005年10月28日星期五17:09:43 -0400,< di ************* @ gmail.com>写道:
On Fri, 28 Oct 2005 17:09:43 -0400, <di*************@gmail.com> wrote:
void copy(char * filename)
{* * char * log_file;
log_file = filename;
strcat(log_file, " -log.txt");

// .......


假设filename =" myfile.dat"

我在期待:
log_file =" myfile.dat-log.txt"
这个工作正常....

问题是我需要保留文件名作为原始名称但是
而不是我有:
filename =" myfile.dat-log.txt"

我该怎么做避免这种情况,并保留原始名称???
当你指定文件名的指针(假设你是如何正确地宣布那个),到log_file,然后你告诉log_file
指向内存中文件名相同的空间。如果您使用
strcat,那么您将该指针输入该函数,并且它将信息连接到该点。由于文件名和
log_file都指向内存中的相同空间,因此将写入相同的空间。

您要做的是创建一个
filename指向的内存,并将log_file分配给副本。也就是说,你必须在内存中有两个不同的空格,这样你就可以将文件名所指向的空间复制到log_file指向的空间,然后你可以改变它log_file指向的空格而不更改文件名的空间,因为filename指向另一个
空间。有意义吗?
void copy(char *filename)
{
char *log_file;
log_file=filename;
strcat(log_file,"-log.txt");

//.......
}

suppose that filename="myfile.dat"

I''m expecting:
log_file="myfile.dat-log.txt"
and this work fine....

the problem is that I need to remain filename as the original name but
instead i have:
filename="myfile.dat-log.txt"

How can I do to avoid this, and preserve the original name???
When you assign the pointer of filename (assuming that''s how you
properly declared that), to log_file, then you''re telling log_file to
point to the same space in memory that filename is. If you use
strcat, you''re feeding that pointer into the function, and it is
concatenating information to that point. Since both filename and
log_file are pointing to the same space in memory, the same space is
going to be written.

What you want to do is create a copy of the memory pointed to by
filename, and assign log_file to the copy. That is, you have to have
two different spaces in memory, so that you can copy the space
pointed to by filename to the space pointed to by log_file, and then
you can change the space pointed to by log_file without changing the
space of filename, because filename is pointing to another
space. Make sense?




这基本上是正确的,但是你的

代码有一些严重的问题。您应该在发布之前尝试编译并执行它。

想想:

#include< string.h>


由于你使用printf(),你还需要一个#include< stdio.h>。

因为你使用了malloc() ,你还需要一个#include< stdlib.h>。

#define STRGSIZE 50


为什么50,特别是因为你可以算实际需要多少空间



无效主要(无效)


不,不,不,不,没有。


main()返回int,而不是void。

{
char * log_file;
char filename [] =" ; test;

log_file = malloc(STRGSIZE);


始终检查malloc()的结果;如果失败,它将返回一个

空指针。通常,你唯一可以回应的就是中止

程序,但这比盲目地继续更好。


你正在尝试连接两个字符串。你知道每个

的长度,因此你确切知道

连接需要多少空间。


此时,log_file指向一个50字节的未初始化块。

无法保证此块包含有效字符串,因此

将其传递给strncat( )调用未定义的行为。


log_file的值应该是test-log.txt,但你永远不会将b $ b复制值试验"到log_file。

strncat(log_file," -log.txt",STRGSIZE);
printf(" filename:%s \ nlog_file:%s \ n",filename ,log_file);
}



That''s basically correct, but there are some serious problems in your
code. You should try compiling and executing it before posting.
Think:

#include <string.h>
Since you use printf(), you also need a "#include <stdio.h>".
Since you use malloc(), you also need a "#include <stdlib.h>".
#define STRGSIZE 50
Why 50, especially since you can figure out exactly how much space is
actually needed?
void main(void)
No, no, no, no, no.

main() returns int, not void.
{
char *log_file;
char filename[] = "test";

log_file = malloc(STRGSIZE);
Always check the result of malloc(); if it fails, it will return a
null pointer. Often the only thing you can do in response is to abort
the program, but it''s better than continuing blindly.

You''re trying to concatenate two strings. You know the length of each
of them, therefore you know exactly how much space you need for the
concatenation.

At this point, log_file points to an uninitialized block of 50 bytes.
There''s no guarantee that this block contains a valid string, so
passing it to strncat() invokes undefined behavior.

The value of log_file is supposed to be "test-log.txt", but you never
copy the value "test" into log_file.
strncat(log_file, "-log.txt", STRGSIZE);
printf("filename: %s\nlog_file: %s\n", filename, log_file);
}




最后,你应该有一个return 0;在你的主要

函数结束时。它在C99中不是必需的,但它不会受到伤害,并且它被认为是好的风格。


试试这个:


#include< string.h>

#include< stdio.h>

#include< stdlib.h> ;


int main(无效)

{

const char * filename =" test";

const char * suffix =" -log.txt" ;;

char * log_file;

size_t log_file_len = strlen(filename)+ strlen(suffix)+ 1 ;


log_file = malloc(log_file_len);

if(log_file == NULL){

fprintf(stderr," malloc failed\\\
");

退出(EXIT_FAILURE);

}


strcpy(log_file,filename);

strcat(log_file,suffix);


printf(" filename = \"%s \" \ n",filename);

printf(" suffix = \"%s \" \ n",suffix);

printf(" log_file = \" %s \" \ n",log_file);


返回0;

}


请注意,原始问题假定一个函数将

文件名作为参数;既不是你的程序也不是我修改过的版本

就是这样做的。可能该函数应该使用char *参数

并返回char *结果。返回一个动态大小的字符串可能会很复杂;要么你必须假设一个最大尺寸,要么

来电者必须分配空间(如果来电者

不知道会有多少空间,这可能会很困难需要),或者函数必须分配空间(使调用者负责解除分配

)。我将暂时保留代码,但原始的

海报应该随意提出后续问题。


-

Keith Thompson(The_Other_Keith) ks***@mib.org < http://www.ghoti .net / ~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



Finally, you should have a "return 0;" at the end of your main
function. It''s not required in C99, but it can''t hurt, and it''s
considered good style.

Try this:

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
const char *filename = "test";
const char *suffix = "-log.txt";
char *log_file;
size_t log_file_len = strlen(filename) + strlen(suffix) + 1;

log_file = malloc(log_file_len);
if (log_file == NULL) {
fprintf(stderr, "malloc failed\n");
exit(EXIT_FAILURE);
}

strcpy(log_file, filename);
strcat(log_file, suffix);

printf("filename = \"%s\"\n", filename);
printf("suffix = \"%s\"\n", suffix);
printf("log_file = \"%s\"\n", log_file);

return 0;
}

Note that the original question assumed a function that takes the
filename as an argument; neither your program nor my modified version
of it does this. Probably the function should take a char* argument
and return a char* result. Returning a dynamically sized string can
be complicated; either you have to assume a maximum size, or the
caller has to allocate the space (which can be difficult if the caller
doesn''t know how much space will be required), or the function has to
allocate the space (making the caller responsible for deallocating
it). I''m going to leave the code as it is for now, but the original
poster should feel free to ask followup questions.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


2005年10月28日14:09:43 -0700,在comp.lang.c,
di ************* @ gmail.com 写道:
On 28 Oct 2005 14:09:43 -0700, in comp.lang.c ,
di*************@gmail.com wrote:
大家好

当我这样做时我有一些问题:


你可能有一对......

void copy(char * filename)
{* * char * log_file;
log_file = filename;


这会将log_file指向与文件名相同的位置。


请记住,在C中,=不是复制运算符,它的赋值是

运营商。对于指针类型,这会将指针设置为指向

相同的位置。它是/不/复制内容。

strcat(log_file," -log.txt");


然后你试着追加它。换句话说,你附加了

/原始字符串/,而不是它的副本


顺便说一下,文件名是否足够大到存储8个额外字符?

更好地确保,否则会崩溃。

假设filename =" myfile.dat"


如果你把它定义为

char * filename =" myfile.dat" ;;

那么它不仅太小了但是不可修改。

我在期待:
log_file =" myfile.dat-log.txt"
这个工作正常....

问题是我需要保留文件名作为原始名称但是
而不是我有:
filename =" myfile.dat-log.txt"

我该怎么做才能避免这种情况,并保留原来的名字???
Hi all

I have certain problem when I''m doing this:
you probably have a couple...
void copy(char *filename)
{
char *log_file;
log_file=filename;
This points log_file to the same place as filename.

Remember that in C, = is not the copy operator, its the assignment
operator. For pointer types, this sets the pointers to point to the
same place. It does /not/ copy the contents.
strcat(log_file,"-log.txt");
Then you try to append to it. In other words, you''re appending to the
/original string/, not a copy of it

By the way, is filename large enough to store 8 extra characters?
Better make sure, or this will crash.
suppose that filename="myfile.dat"
if you defined it as
char *filename = "myfile.dat";
then its not only too small, but nonmodifiable.
I''m expecting:
log_file="myfile.dat-log.txt"
and this work fine....

the problem is that I need to remain filename as the original name but
instead i have:
filename="myfile.dat-log.txt"

How can I do to avoid this, and preserve the original name???




通过strcpy函数或
之一将文件名复制到一个新变量
它的朋友。

-

Mark McIntyre

CLC FAQ< http://www.eskimo.com/~ scs / C-faq / top.html>

CLC自述文件:< http://www.ungerhu.com/jxh/clc.welcome.txt>


---- ==通过Newsfeeds.Com发布 - 无限制 - 未经审查 - 安全使用网新闻== ----
http://www.newsfeeds。 com 世界排名第一的新闻组服务! 120,000多个新闻组

---- =东海岸和西海岸服务器农场 - 通过加密实现全隐私= ----



Copy the filename to a new variable via the strcpy function or one of
its friends.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----


这篇关于strcat,strcpy,sprintf的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆