1个回答
展开全部
fwrite、fprintf、fputc等写文件函数都可以完成,而专用的字符串文件写入函数fputs更方便一些。举例如下(文件建立在当前目录下,名为123.txt):
//#include "stdafx.h"//If the vc++6.0, with this line.#include "stdio.h"#include "string.h"#include "stdlib.h"int main(void){ char s[70]; FILE *fp; if((fp=fopen("123.txt","w"))==NULL){ printf("Open the file failure...\n"); exit(0); } while(1){ printf("Input a string...\ns="); if(gets(s),strlen(s)<70) break; printf("Too long, redo: "); } fputs(s,fp); fclose(fp); printf("\n"); return 0;}
//#include "stdafx.h"//If the vc++6.0, with this line.#include "stdio.h"#include "string.h"#include "stdlib.h"int main(void){ char s[70]; FILE *fp; if((fp=fopen("123.txt","w"))==NULL){ printf("Open the file failure...\n"); exit(0); } while(1){ printf("Input a string...\ns="); if(gets(s),strlen(s)<70) break; printf("Too long, redo: "); } fputs(s,fp); fclose(fp); printf("\n"); return 0;}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询