0
我有一個虛擬函數的char *的getFileName()只返回一個指向字符數組:C瞭解與指針文件字符串作爲名字
char * getFileName() {
char buff[11] = "index.html";
char *p;
p = buff;
printf ("name of the file: %s\n", p);
return p;
}
在我的情況下,這個指針的名字是
int main() {
char *fp;
FILE *file;
fp = getFileName();
int c;
file = fopen(fp, "r");
if (file) {
while ((c = getc(file)) != EOF) {
putchar(c);
}
fclose(file);
}
return (0);
}
但是,我不能打開使用指針值作爲名稱的文件,雖然當我打印FP,我如果得到正確的名稱:文件我想從主打開和讀取的名字文件:index.html。任何建議,我可能會錯過?謝謝:)