【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 ...
随机推荐
- 51nod 1907(多项式乘法启发式合并)
题目: 分析: 对于一个确定的生成子图,很明显是在一个连通块上走,走完了再跳到另一个连通块上,假设连通块个数为cnt,那么答案一定是$min(a_{cnt-1},a_cnt,..,a_{n-1})$ ...
- SpringMvc架构流程
- RESTful API设计规范收集
说明:其实没有绝对的规范,达到90%即可. 理解RESTful架构:http://www.ruanyifeng.com/blog/2011/09/restful.html RESTful API 设计 ...
- Spring下的@Inject、@Autowired、@Resource注解区别(转)
1.@Inject javax.inject JSR330 (Dependency Injection for Java) 这是JSR330中的规范,通过AutowiredAnnotationBean ...
- c++之虚基类初始化
C++虚基类构造函数下面文章详细介绍C++虚基,所谓C++虚基类:是由最派生类的构造函数通过调用虚基类的构造函数进行初始化的,但前提是要深入理解到底什么是C++虚基类,及他是怎么运行的. 前面讲过,为 ...
- iOS用户体验之-modal上下文
iOS用户体验之-modal上下文 何为模态视图,它的作用时聚焦当前.获得用户的注意,用户仅仅有完毕模态的任务才 退出模态视图.否则你将不能运行app的任务,比如,alert view,model v ...
- Asp.net MVC 简单分页 自做简单分页
Asp.net MVC 简单分页: public static string Pager(int page,int pageSize,int total) { ...
- PHP swfupload图片上传实例
swfupload已经是第二次研究,这次自已整了个简单demo,无奈菜鸟最杯… PHP代码如下: if (isset($_FILES["Filedata"]) || !is_upl ...
- Android之——经常使用手机号码功能
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47374415 有些Android手机中会带有一些经常使用号码的功能,比方订餐电话. ...
- java8--集合(疯狂java讲义3复习笔记)
1.集合分四类:set,map,list,queue 位于java.util包下. 集合类和数组的区别,数组可以保存基本类型的值或者是对象的引用,而集合里只能保存对象的引用. 集合类主要由两个接口派生 ...