【415】C语言文件读写
A program can open and close, and read from, and write to, a file that is defined by the user
This is generally done when you have
- large volumes of stored data, or
- complex data (such as structs) or
- non-printable data
These don't happen often. Nevertheless, for the sake of completeness, here is a program that
reads a number from a file input.txt
writes the count from 1 to that number to the file output.txt
it is user-friendly
: it tells the user that an output file has been created
// files.c
// read a number 'num' from a file input.txt
// write a count from 1 to 'num' to the file OUT #define IN "input.txt"
#define OUT "output.txt" #include <stdio.h>
#include <stdlib.h> #define NUMDIG 6 // size of numerical strings that are output int main(void) {
FILE *fpi, *fpo; // these are file pointers
char s[NUMDIG]; fpi = fopen(IN, "r");
if (fpi == NULL) { // an important check
fprintf(stderr, "Can't open %s\n", IN);
return EXIT_FAILURE;
}
else {
int num;
if (fscanf(fpi, "%d", &num) != 1) { // an important check
fprintf(stderr, "No number found in %s\n", IN);
return EXIT_FAILURE;
}
else {
fclose(fpi); // don't need the input file anymore
fpo = fopen(OUT, "w");
if (fpo == NULL) { // an important check
fprintf(stderr, "Can't create %s!\n", OUT);
return EXIT_FAILURE;
}
else { // got input and got an output file
fprintf(fpo, "%s", "Counts\n");
for (int i=1; i<=num; i++) {
sprintf(s, "%d", i);
fprintf(fpo, "%s\n", s);
}
fclose(fpo);
printf("file %s created\n", OUT);
return EXIT_SUCCESS;
}
}
}
}
input.txt
10
output.txt
prompt$ dcc files.c
prompt$ ./a.out
file output.txt created
prompt$ more output.txt
Counts
1
2
3
4
5
6
7
8
9
10
11
12
13
【415】C语言文件读写的更多相关文章
- c语言文件读写操作总结
C语言文件读写操作总结 C语言文件操作 一.标准文件的读写 1.文件的打开 fopen() 文件的打开操作表示将给用户指定的文件在内存分配一个FILE结构区,并将该结构的指针返回给用户程序,以后用户程 ...
- 3,C语言文件读写
这两天看到一个关于文件读写的题目,索性就把相关内容总结下. C语言文件读写,无非是几个读写函数的应用,fopen(),fread(),fwrite()等,下面简单介绍下. 一.fopen() 函数原型 ...
- C语言文件读写命令fprintf和fscanf
以向文件中读取和写入二维数组为例. 以下是fprintf的使用:向文件中写入10*10的二维数组,数组元素为1~100之间的随机数. #include <stdlib.h> #includ ...
- C语言文件读写操作
C语言实现文件读写,注意区分几个方法: 写入: fwrite() //个人认为这个最好,可是实现写入任何数据类型,任何长度 fputs() //写入一个字符串,字符串长度不能太长,具体的长度未知,但估 ...
- C++常用工具库(C语言文件读写,日志库,格式化字符串, 获取可执行文件所在绝对路径等)
前言 自己常用的工具库, C++ 和C语言实现 使用cmake维护的项目 持续更新..... 提供使用范例, 详见example文件夹 windows使用的VS通过了的编译. Linux(Ubuntu ...
- C语言文件读写(结构体文件)
有时候,我们需要将输入的数据存储起来,这时候就需要用到文件,对于C语言而言,文件的读写有多种方式,下面主要是结构体文件的读写,例如student.dat(第一列是学号,第二列是姓名) xiaoming ...
- C语言文件读写
1.用fopen打开文件 该函数的原型为FILE *fopen(const char *filename, const char *mode),第一个参数是文件名,第二个参数是打开文件的模式. 打开文 ...
- [知识复习] C语言文件读写
文件打开 fopen() 返回FILE* 对象,如果打开失败返回NULL,错误代码存入errno中 FILE *fopen( const char * filename, const char * m ...
- C 语言 文件读写
在ANSI C中,对文件的操作分为两种方式,即流式文件操作和I/O文件操作,下面就分别介绍之.一.流式文件操作 这种方式的文件操作有一个重要的结构FILE,FILE在stdio.h中定义如下:type ...
随机推荐
- python学习之-- 面向对象
面向对象(简写:OOP) 面向对象编程定义:利用类和对象来创建各种模型,来实现对真实世界的描述. 优点:使程序更容易理解和维护以及扩展代码. 类定义:用来描述具有相同的属性和方法的对象的集合.(简单讲 ...
- 免费SSL申请
https://letsencrypt.org/ https://letsencrypt.org/docs/client-options/ ACMESharp (.NET, PowerShell) w ...
- DELPHI跨平台的临界替代者
在WINDOWS里面使用临界来保护多线程需要访问的共享对象,现在,DELPHI有了新的跨平台临界保护者--System.TMonitor 代码演示如下: FConnections := TObject ...
- 【Nginx】定时器事件
转自:烟雨江南 Nginx事件管理主要是网络事件和定时器事件.下面介绍定时器事件管理,即超时管理. 为什么进行超时管理? Nginx有必要对可能发生超时的事件 进行统一管理,并在事件超时时作出相应的处 ...
- 【c++】C++中const用法总结
1. const常量,如const int max = 100; 优点:const常量有数据类型,而宏常量没有数据类型.编译器可以对前者进行类型安全检查,而对后者只进行字符替换,没有类型安全 ...
- Friefox清除旧的网页缓存
Ctrl + F5 适用于调试网页编码时,不断以旧设置显示页面
- [Bash] Search for Text with `grep`
In this lesson, we’ll use grep to find text patterns. We’ll also go over some of the flags that grep ...
- hi3531 SDK已编译文件系统制作jffs2文件系统镜像并解决这个问题 .
一, 安装SDK 1.Hi3531 SDK包位置 在"Hi3531_V100R001***/01.software/board"文件夹下,您能够看到一个 Hi3531_SDK_Vx ...
- oracle授权、表备份、用户管理
用户管理 创建用户: create user 用户名 identified by 密码; 修改用户密码: alter user 用户名 identified by 密码; 激活用户: alter us ...
- 【Mongodb教程 第十三课 】PHP mongodb 的增删改查使用
<pre> <?php #phpinfo();die; #其他链接方式 #$conn=new Mongo(); #连接本地主机,默认端口. #$conn=new Mongo(&quo ...