C语言 文件操作9--fgetc()和fputc()
生活随笔
收集整理的這篇文章主要介紹了
C语言 文件操作9--fgetc()和fputc()
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
//fgetc()和fputc()#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>//文本寫文件
int writeWord(const char *path,const char *pword){int ERRO_MSG = 0;int i = 0;if (path == NULL){ERRO_MSG = 1;printf("path==NULL erro msg:%d\n", ERRO_MSG);return ERRO_MSG;}if (pword == NULL){ERRO_MSG = 2;printf("pword==NULL erro msg:%d\n", ERRO_MSG);return ERRO_MSG;}//定義文件指針FILE *fpw=NULL;//打開文件fpw = fopen(path, "w");//w 打開只寫文件,若文件存在,則文件長(zhǎng)度清零,即文件內(nèi)容會(huì)消失,若文件不存在則建立該文件//判斷文件是否打開成功if (fpw==NULL){ERRO_MSG = 1;printf("文件打開失敗 fpw==NULL erro msg:%d\n", ERRO_MSG);return ERRO_MSG;}//開始寫文件for (i = 0; i < (int)strlen(pword)+1; i++){fputc(pword[i], fpw);}//fputs(pword, fpw);//關(guān)閉文件if (fpw!=NULL){fclose(fpw);}return ERRO_MSG;
}//文件讀文件
int readtxt(const char *path,char *pout)//二級(jí)指針做輸出
{int ERRO_MSG = 0;if (path==NULL){ERRO_MSG = 1;printf("path==NULL erro msg:%d\n", ERRO_MSG);return ERRO_MSG;}if (pout == NULL){ERRO_MSG = 2;printf("pout==NULL erro msg:%d\n", ERRO_MSG);return ERRO_MSG;}//定義文件指針FILE *pfr = NULL;//打開文件pfr = fopen(path, "r");if (pfr==NULL){ERRO_MSG = 3;printf("pfr==NULL erro msg:%d,文件路徑:%s\n", ERRO_MSG, path);return ERRO_MSG;}//開始讀文件int index = 0;//讀文件while (!feof(pfr)){//feof()如果文件結(jié)束,則返回非0值,否則返回0//memset(pout, 0, sizeof(char)*200);pout[index++] = fgetc(pfr);}pout[index] = '\0';return ERRO_MSG;
}void main(){//定義文件路徑char *path = "E:\\Test\\CwordTest\\";//只適用于windowchar *path1 = "E:/Test/CwordTest/a1.txt";writeWord(path1, "asfasdfasdgafdsgadf\r\nasdfadsadf\r\ndsafgshfetgrhet");//1.定義文件緩存數(shù)組char bufarr[200] = { 0 };readtxt(path1, bufarr);printf("%s\n", bufarr);system("pause");
}
?
轉(zhuǎn)載于:https://www.cnblogs.com/zhanggaofeng/p/5450065.html
總結(jié)
以上是生活随笔為你收集整理的C语言 文件操作9--fgetc()和fputc()的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: iQOO Z7s 5G 手机渲染图曝光:
- 下一篇: 摩托车过户 摩托车怎么过户