【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 ...
随机推荐
- java中filter的用法
filter过滤器主要使用于前台向后台传递数据是的过滤操作.程度很简单就不说明了,直接给几个已经写好的代码: 一.使浏览器不缓存页面的过滤器 Java代码 import javax.servlet ...
- 【ZJOI2017 Round1练习&BZOJ4765】D1T3 普通计算姬(主席树,分块)
题意: 思路:分块 使用树状数组维护sum[i]的前缀和 使用主席树维护root到u的路径上点的编号出现的个数 每次操作如果是修改就加入队列 如果是询问,考虑块内操作对询问的影响,每次在x点加上y会使 ...
- tiles
参考博客:https://blog.csdn.net/aosica321/article/details/68948915 https://blog.csdn.net/it_faquir/articl ...
- 牛客网暑期ACM多校训练营(第三场)J 多边形与圆相交的面积
链接:https://www.nowcoder.com/acm/contest/141/J 题目描述 Eddy has graduated from college. Currently, he is ...
- codechef Polo the Penguin and the Tree
一般xor 的题目都是用trie解决. 那这道题是在树上的trie; 首先:从root==1,遍历树得到1到所有节点的xor 值. 然后对于每个点我们把其插入二进制树中. 对于每一个点查找其二进值异或 ...
- [Bzoj1069][Scoi2007]最大土地面积(凸包)(旋转卡壳)
1069: [SCOI2007]最大土地面积 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 3629 Solved: 1432[Submit][Sta ...
- POJ 1860【求解是否存在权值为正的环 屌丝做的第一道权值需要计算的题 想喊一声SPFA万岁】
题意: 有n种钱币,m个钱币兑换点,小明一开始有第n种钱币数量为w. 每个兑换点可以将两种不同的钱币相互兑换,但是兑换前要先收取一定的费用,然后按照比例兑换. 问小明是否可以经过一系列的兑换之后能够将 ...
- JDBC调用存储过程,进参出参
今天做了一个数据表拷贝的功能,用到了存储过程,就写了一个java中用jdbc调用存储过程的代码,成功的实现了功能,晚上跑回家记录下 Connection conn = ConnectionUtil.g ...
- tomcat配置访问项目时不需要加项目名称
原文:http://blog.csdn.net/coolcoffee168/article/details/52582770 java web部署后,访问项目的时候,需要在地址中添加项目名称,那么如何 ...
- SQLAlchemy的group_by和order_by的区别
1.官网解释: group_by(*criterion) apply one or more GROUP BY criterion to the query and return the newly ...