我正在make一个makefile的创建者,但是我被困在sprintf上的这个错误中,最奇怪的是我有多个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时,我遇到了这个问题。 – forivall 2012-03-25 06:07:20