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语言文件读写的更多相关文章

  1. c语言文件读写操作总结

    C语言文件读写操作总结 C语言文件操作 一.标准文件的读写 1.文件的打开 fopen() 文件的打开操作表示将给用户指定的文件在内存分配一个FILE结构区,并将该结构的指针返回给用户程序,以后用户程 ...

  2. 3,C语言文件读写

    这两天看到一个关于文件读写的题目,索性就把相关内容总结下. C语言文件读写,无非是几个读写函数的应用,fopen(),fread(),fwrite()等,下面简单介绍下. 一.fopen() 函数原型 ...

  3. C语言文件读写命令fprintf和fscanf

    以向文件中读取和写入二维数组为例. 以下是fprintf的使用:向文件中写入10*10的二维数组,数组元素为1~100之间的随机数. #include <stdlib.h> #includ ...

  4. C语言文件读写操作

    C语言实现文件读写,注意区分几个方法: 写入: fwrite() //个人认为这个最好,可是实现写入任何数据类型,任何长度 fputs() //写入一个字符串,字符串长度不能太长,具体的长度未知,但估 ...

  5. C++常用工具库(C语言文件读写,日志库,格式化字符串, 获取可执行文件所在绝对路径等)

    前言 自己常用的工具库, C++ 和C语言实现 使用cmake维护的项目 持续更新..... 提供使用范例, 详见example文件夹 windows使用的VS通过了的编译. Linux(Ubuntu ...

  6. C语言文件读写(结构体文件)

    有时候,我们需要将输入的数据存储起来,这时候就需要用到文件,对于C语言而言,文件的读写有多种方式,下面主要是结构体文件的读写,例如student.dat(第一列是学号,第二列是姓名) xiaoming ...

  7. C语言文件读写

    1.用fopen打开文件 该函数的原型为FILE *fopen(const char *filename, const char *mode),第一个参数是文件名,第二个参数是打开文件的模式. 打开文 ...

  8. [知识复习] C语言文件读写

    文件打开 fopen() 返回FILE* 对象,如果打开失败返回NULL,错误代码存入errno中 FILE *fopen( const char * filename, const char * m ...

  9. C 语言 文件读写

    在ANSI C中,对文件的操作分为两种方式,即流式文件操作和I/O文件操作,下面就分别介绍之.一.流式文件操作 这种方式的文件操作有一个重要的结构FILE,FILE在stdio.h中定义如下:type ...

随机推荐

  1. /etc/fstab readyonly 解决办法

    阿里云主机切换地区强制升级后,“新的磁盘盘符识别为vdb1,但是在/etc/fstab中记录的挂载信息还是旧的xvdb1,导致磁盘挂载失败” 机子启动出错了. 按提示 输入 root的密码,进入以Re ...

  2. SQL Prompt 5.1使用

    SQL Prompt 5.1教程 1.下载 自行下载安装文件.本人是从http://www.cr173.com/下载的. 2.安装 安装没什么特别的,不用说了 3.注意一下破解和配置 按里面的read ...

  3. Python基础教程笔记——第5章:条件,循环和其他语句

    5.1 print和import的更多信息 1. print()3.0之后print不再是语句,而是函数, >>> print('udg',12,13)   udg 12 13 &g ...

  4. iOS React Native 环境的搭建

    react native 的官网:http://reactnative.cn/docs/0.47/getting-started.html#content  --iOS如何搭建mac版的环境 1.配置 ...

  5. python学习之-- redis模块基本介绍

    数据缓存系统: 1:mongodb:是直接持久化,直接存储于硬盘的缓存系统 2:redis: 半持久化,存储于内存和硬盘 3:memcache:数据只能存储在内存里的缓存系统关于memcache 学习 ...

  6. Android 学习路线图(转载自https://blog.csdn.net/lixuce1234/article/details/77947405)

    程序设计 一.java (a)基本语法(如继承.异常.引用.泛型等) Java核心技术 卷I(适合入门) 进阶 Effective Java中文版(如何写好的Java代码) Java解惑 (介绍烂Ja ...

  7. com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown error 1054

    com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown error 1054 这个错误困扰了我一个下午  插入数据总是错误 ...

  8. java下XML与JSON互相转换的Utils类

    原文:http://heipark.iteye.com/blog/1394844 需要json-lib-2.1-jdk15.jar和xom-1.2.5.jar,maven pom.xml如下: < ...

  9. Solidworks drwdot文件如何打开,如何制作Solidworks工程图模板

    1 直接把这个文件拖放进Solidworks窗口   2 文件-保存图纸格式,另存为模板(slddrt文件)   3 搜索"Solidworks工程图如何使用,替换图纸格式模板文件.doc& ...

  10. Office WORD WPS如何取消拼写检查

    1 审阅-修订-修订选项-拼写,全部取消勾选.