问题描述
大家好
当我这样做时我遇到了一些问题:
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!!
推荐答案
当你指定文件名指针时(假设你是如何正确的)
声明了),对于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革命性的电子邮件客户端:
这基本上是正确的,但是你的
代码有一些严重的问题。您应该在发布之前尝试编译并执行它。
想想:
#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)< 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.
通过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发布 - 无限制 - 未经审查 - 安全使用网新闻== ----
世界排名第一的新闻组服务! 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的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!