- #include <string.h>
- #include <stdio.h>
- int main(int argc, char* argv[])
- {
- //保存键盘录入的字符串到文件
- char *str;
- printf("请输入要保存的字符串:\n");
- str = (char *)malloc(100*sizeof(char));
- fgets(str,100,stdin);
- FILE *p = fopen("C:\\test.txt","w");
- if(p != NULL){
- for(int i = 0;i<strlen(str);i++){
- fputc(str[i],p);
- };
- }
- int f = fclose(p);
- if(f == 0)
- printf("关闭成功\n");
- else
- printf("关闭失败\n");
- return 0;
- }
复制代码
请问上面代码的错误在哪里,该如何修改? |
|