qq_45697473 2020-11-07 10:35 采纳率: 100%
浏览 613
已采纳

c语言中关于用fputc输入,打开文件后有乱码怎么解决?

#include <stdio.h>
int main(void){
	FILE *fp = NULL;
	fp = fopen("test.txt","w");
	if(fp==NULL){
		perror("fopen error");
		return -1;
	}
	char ch='a';
	while('a'<='z'){
		int ret=fputc(ch,fp);
		if(ret==-1){
			perror("fputc error");
			return -1;
		}
		ch++;
	}
	return 0;
} 

这个输入内容有,但是后面跟了一长串乱码,搞不懂,求大佬解决一下,谢谢各位啦

  • 写回答

2条回答 默认 最新

  • jeyawn 2020-11-07 11:17
    关注
    #include <stdio.h>
    int main(void){
    	FILE *fp = NULL;
    	fp = fopen("test.txt","w");
    	if(fp==NULL){
    		perror("fopen error");
    		return -1;
    	}
    	char ch='a';
    	while(ch<='z'){ //这里写错了,正确应该是这个,否则是无限循环了
    		int ret=fputc(ch,fp);
    		if(ret==-1){
    			perror("fputc error");
    			return -1;
    		}
    		ch++;
    	}
    	return 0;
    } 

     

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?