C语言字符串读写函数

来源:互联网 发布:淘宝联盟2016旧版 编辑:程序博客网 时间:2024/05/21 13:57
#include<stdio.h>#include<stdlib.h>void main(){    FILE *fp;    fp=fopen("./tmp/text2.txt","r");    if(!fp)    {         printf("文件打开失败!\n");         exit(1);    }    char buff[200];//    fgets(char *s,int n,FILE *sp):读取一行,或者n-1个字符到字符数组s中,//并自动在读到的字符串末位加结束符“\0”,否则返回NULL表示到文件末位了//    fputs(const char* s,FILE *fp):将字符串s输出到fp指向的文件,并且不会输出字符串的结束符“\0”//          如果成功,位置指针后移,函数返回一个非负整数,否则返回EOF    while((fgets(buff,200,fp))!=NULL)    {        printf("%s",buff);    }    fclose(fp);//关闭文件}

./tmp/text2.txt 中内容:./tmp/text2.txt

运行结果:

who are you?