【发布时间】:2012-03-25 06:02:45
【问题描述】:
我正在创建一个 makefile 创建器,但我在 sprintf 上遇到了这个错误,最奇怪的是我在出现错误的一个之前有多个 sprintf,它们工作正常。
代码如下:
if ( WIFEXITED(stat) ){
if ( WEXITSTATUS(stat) ) {
if ( cFiles == 0 && cFolders == 0 ) {
Crear(path);
}
cFolders = 1;
TEMP = malloc( sizeof(char)*( strlen(direntp->d_name) + 25 ) );
if ( TEMP == NULL ) {
perror("Malloc Error: ");
exit(1);
}
if ( sprintf(TEMP, "\n%s/%s.a: force\n\t$(MAKE) -C %s\n",direntp->d_name, direntp->d_name, direntp->d_name) < 0 ) {
perror("Sprintf Error: ");
exit(1);
}
write(STDOUT_FILENO,TEMP,strlen(TEMP));
f.name = malloc( sizeof(char)*( strlen(direntp->d_name)*2 + 3 ) );
if ( f.name = NULL ) {
perror("Malloc Error: ");
exit(1);
}
//This is the one with the problem!!!
if ( sprintf(f.name, "%s/%s.a", direntp->d_name, direntp->d_name) < 0 ) {
perror("Sprintf Error: ");
exit(1);
}
l = AddToList(l,&f);
}
}
【问题讨论】:
-
我最好的猜测是其中一个字符串不是以空值结尾的。当我第一次学习 C 时,我遇到过几次这个问题。
标签: c segmentation-fault printf