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;}
温馨提示:答案为网友推荐,仅供参考