博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
读写文件
阅读量:7059 次
发布时间:2019-06-28

本文共 1588 字,大约阅读时间需要 5 分钟。

我们之前学习的程序,无论是输入数据,还是输出数据,都是对内存的操作。

一旦程序结束,数据也就没了。下次打开又得重新输入输出。
那么怎么样可以让我们的程序可以实现“记忆”呢?
这就需要将程序里面的内容以文件的形式存储到我们的硬盘上,这样子即使断电了,数据仍然在硬盘里面。

 


申请内存和释放内存。
打开文件和关闭文件。
fopen和fclose.
End Of File.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main() {
 //打开一个文件,或者说跟一个文件建立链接。
 //fileopen = fopen(要打开文件,用什么方式打开)
 FILE * l_fp = fopen("./123.txt", "w");//read 和write模式
 char l_arr[] = "我的银行卡是:6222 0205 ....";
 char l_temp = 0;
 if (l_fp == NULL) {
  printf("文件打开失败。\n");
 }
 else {
  printf("打开成功。\n");
  for (size_t i = 0; i < strlen(l_arr); i++) {
   putc(l_arr[i],l_fp);
  }

  fclose(l_fp);

 }

 system("pause");

}

 

#include<stdio.h>

#include<stdlib.h>
#include<string.h>
void main() {
FILE *l_p = fopen("./123", "r");
char l_temp = 0;
if (l_p == NULL) {
printf("无法打开文件");
}
else {
printf("打开成功\n");
while (l_temp != EOF) {
l_temp = getc(l_p);
printf("%c", l_temp);

 

}

fclose(l_p);
}
system("pause");
}
getc=getchar 从标准输入里读取下一个字符

#include<stdio.h>

#include<stdlib.h>
#include<string.h>
void main() {
FILE*l_p = fopen("./储存", "r");
char l_temp = 0;
if (l_p == NULL) {
printf("无法打开文件\n");
}
else {
printf("打开文件成功\n");
l_temp = getc(l_p);
printf("%c\n", l_temp);
l_temp = getc(l_p);
printf("%c\n", l_temp);
l_temp = getc(l_p);
printf("%c\n", l_temp);
}
system("pause");
}

#include<stdio.h>

#include<stdlib.h>
#include<string.h>
void main() {
FILE *l_p = fopen("./123", "r");
char l_temp = 0;
if (l_p == NULL) {
printf("无法打开文件");
}
else {
printf("打开成功\n");
while (l_temp != EOF) {
l_temp = getc(l_p);
printf("%c", l_temp);

}

fclose(l_p);
}
system("pause");
}

 

转载于:https://www.cnblogs.com/xiaodaxiaonao/p/8551451.html

你可能感兴趣的文章
NOIP2015DAY2T2子串
查看>>
5种PHP创建数组的方式
查看>>
24. [Ext JS 4] 实战之Load Mask(加载遮罩)的显示与隐藏
查看>>
【C语言】07-基本语句和运算
查看>>
ajax异步获取提示框数据(鼠标悬浮事件)
查看>>
Android 内存使用hprof文件打开方法
查看>>
android入门一
查看>>
第16条:复合优先于继承
查看>>
[学习笔记]斯特林数
查看>>
oracle 修改表空间文件路径方法
查看>>
一张图理解RACSignal的Subscription过程
查看>>
objectLiteral.js
查看>>
Ext 面向对象程序设计 入门篇
查看>>
Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) 一夜回到小学生
查看>>
8皇后问题
查看>>
update comboBox
查看>>
包装类
查看>>
生成带内嵌图片的二维码
查看>>
Day1--js--你所未见到的他的名字
查看>>
GeoServer style标注中文乱码配置
查看>>