当前位置:网站首页>C语言文件——字符串的方式读出与写入
C语言文件——字符串的方式读出与写入
2022-06-09 06:49:00 【SweetCode】
C语言文件——字符串的方式读出与写入
conclusion:
- the steps of wirte into file
| process |
|---|
| 1.定义文件路径和文件操作 |
| 2.定义文件指针以及存储变量 |
| 3.打开文件(无出错处理) |
| 4.读入所需的内容(输入-1代表结束) |
| 5.关闭文件 |
- the follow code you can validate above contents directly.
//C语言按字符串写入文件
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define maxn 1024
int main(){
//C语言按字符串写入文件
/* steps: 1.定义文件路径和文件操作 2.定义文件指针以及存储变量 3.打开文件(无出错处理) 4.读入所需的内容(输入-1代表结束) 5.关闭文件 */
//1.
char *str=(char*)malloc(sizeof(char)*maxn);
char *source="test2.txt";
//2.
char *op="w+t";
FILE *fp=NULL;
//3.
fp=fopen(source,op);
//4.
puts("请输入信息:");
while(1){
if(strcmp(gets(str),"-1")==0) break;
strcat(str,"\n");
fputs(str,fp);
}
if(ferror(fp)) puts("文件写入错误!");
//5.
fclose(fp);
return 0;
}
- the steps of read from file
| process |
|---|
| 1.定义文件路径和文件操作 |
| 2.定义文件指针以及存储变量 |
| 3.打开文件(出错处理) |
| 4.读出文件的内容 |
| 5.关闭文件 |
- hurry to make it come true by yourself and validate it !
- follows are code
//C语言字符串读出或写入文件
#include<stdio.h>
#include<stdlib.h>
#define maxn 1024
int main(){
//读出文件
/* steps: 1.定义文件路径和文件操作 2.定义文件指针以及存储变量 3.打开文件(出错处理) 4.读出文件的内容 5.关闭文件 */
//1.
char *str=(char*)malloc(sizeof(char)*maxn);
char *source="test.txt";
//2.
char *op="r+t";
FILE *fp=NULL;
//3.
fp=fopen(source,op);
if(fp==NULL){
puts("文件打开失败!");
exit(0);
}
//4.
while(fgets(str,maxn,fp)!=NULL){
puts(str);
}
if(ferror(fp)) {
puts("文件读取过程发生错误!");
}
//5.
fclose(fp);
return 0;
}
- Please remember that every night you have stayied up and the path you have passed are always glittering !
Cheer on ! The student who spare no effort on preparing for the examination for graduate school !
2022/6/4
边栏推荐
- 【系统分析师之路】第十八章 复盘系统安全分析与设计(加密与解密)
- openGauss 数据库操作步骤
- 【系统分析师之路】第十五章 复盘数据库系统(关系数据库应用)
- [SDU project training level 2019] router link: who will execute to or @click first
- 穆格伺服阀如何存放?简单的几个方法
- 外设驱动库开发笔记42:DAC8552 DAC驱动
- UML series articles (23) advanced behavior -- process and thread
- make install 安装的库,如何卸载
- How to store the MOOG servo valve? Simple methods
- Fourier transform signal processing
猜你喜欢

Yolov4 analysis | Part 2: training your own data set with yolov4 (super detailed full version)

BSP tailoring of Quanzhi platform (7) rootfs tailoring -- tailoring of user tools and Libraries & rootfs compression

fastadmin 自定义导出的excl表名称+时间

UML系列文章(27)體系結構建模---部署

Intranet control: nodemcu (esp8266) and Arduino serial port communication

Database final exam outline

openGauss 数据库操作步骤

Excl two column data comparison is realized by VBA, for example, whether the data of column a has appeared in column B

209. minimum length subarray

Error (119013): Current license file does not support the EP4CE10F17C8 device解决方法
随机推荐
Database final exam outline
Mendeley 等文献管理工具在word中插入参考文献的报错解决
Kotlin 's Null safety
Fourier transform signal processing
Camtasia 2022 release update function introduction
Clickhouse2 fragment 2 replica high availability cluster setup and chproxy proxy configuration
Idea如何查看保存代码的路径
蘑菇街發布2022財年財報:下半年虧損同比收窄50%
UML series (24) advanced behavior - time and space
UML series article (22) advanced behavior -- state machine
关于用户消息的推送
Can I use machine learning programming without knowing mathematics?
Raspberry pie installation opencv - pro test available
UML Series (27) Architecture Modeling - Deployment
Jump from one pit to another
287. finding duplicates
BSP tailoring of Quanzhi platform (7) rootfs tailoring -- tailoring of user tools and Libraries & rootfs compression
常用类——String类概述
BSP tailoring (2) attachment 1 General setup configuration description
The Sandbox 和 Ikonia 达成合作,在元宇宙中还原足球传奇人物皮尔洛的壁画