Re: [闲聊] 我可以再问一个写程式的问题吗

楼主: kerycheng (kk)   2023-11-01 14:28:05
※ 引述《Wardyal (记得签到B站漫画)》之铭言:
: 为什么我
: FILE *fp;
: int tmp = 1;
: fopen(fp, "test.txt", "w");
: fprintf(fp, "%s\n", "apple");
: fprintf(fp, "%d", tmp);
: fclose(fp);
: 都会在fprintf %s 那行Segmentation Fault阿
: 就算后来换成
: char *str = "apple";
: 也是一样 好怪
可是瓦屌
如果我没记错的话fopen只有给两个input
FILE *fopen(const char *filename, const char *mode)
你塞三个input给它不就爆开了
应该要写成
fp = fopen("test.txt", "w");
if (fp != NULL) {
fprintf(fp, "%s\n", "apple");
fprintf(fp, "%d", tmp);
fclose(fp);
}
这样吧
作者: wu10200512 (廷廷)   2023-11-01 14:29:00
用ifstream啦ifstream赞
作者: Wardyal (Wardyal)   2023-11-01 14:41:00
你很棒

Links booklink

Contact Us: admin [ a t ] ucptt.com