为了账号安全,请及时绑定邮箱和手机立即绑定

想要“hello!”赋给str[]数组,然后输出,结果输出来的全是null,求解为什么?

想要“hello!”赋给str[]数组,然后输出,结果输出来的全是null,求解为什么?

PHP C
繁星淼淼 2023-03-15 11:16:43
#include <stdio.h> main(){ FILE *fp;char *p;int i=0;char str[10];fp=fopen("C:\\Users\\Administrator\\Desktop\\c程序设计\\text.txt","w");fputs("hello!",fp);fp=fopen("C:\\Users\\Administrator\\Desktop\\c程序设计\\text.txt","r");fgets(str,5,fp);for(i;i<10;i++)printf("str=%s",str[i]);getchar();}
查看完整描述

2 回答

?
阿晨1998

TA贡献2037条经验 获得超6个赞

fopen连续执行两次,第二次open的文件是空的。
第一次先close一下再open。

查看完整回答
反对 回复 2023-03-18
?
慕标琳琳

TA贡献1830条经验 获得超9个赞

#include <stdio.h> int main(void){FILE *fp;    char str[10]={0};    if((fp=fopen("D:/123/text.txt","w+"))==NULL)// 文件必须已经存在{printf("ERROR!\n");exit(1);}    fputs("hello!",fp);fseek(fp,0,SEEK_SET); // 把当前指针移到文件开始处fgets(str,7,fp);    printf("str=%s",str);fclose(fp);return 0;}

查看完整回答
反对 回复 2023-03-18
  • 2 回答
  • 0 关注
  • 105 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号