文件操作

  • fopen与fclose
  • fread与fwrite
  • fseek
  • fputs与fgets
  • fscanf与fprintf
fopen与fclose
#include<stdio.h>

int main() {
FILE *fp;
char c;
fp = fopen("1.txt", "r");
if (NULL == fp) {
perror("fopen"); //perror()函数打印str(字符串)和一个相应的执行定义的错误消息到全局变量errno中.
}
while ((c = fgetc(fp)) != EOF) {
putchar(c);
}
fclose(fp);
return 0;
}
fread与fwrite

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
FILE *fp;
char buf[128] = { 0 };
int i = 0x12340a78;
int ret;
fp = fopen("1.txt", "rb+");
if (NULL == fp)
{
perror("fopen");
}
//打印文件内容
//while(memset(buf,0,sizeof(buf)),(ret=fread(buf,sizeof(char),sizeof(buf)-1,fp))>0)
//{
// printf("%s",buf);
//} //写入文件
strcpy(buf, "hello\nworld");
fwrite(buf, sizeof(char), strlen(buf), fp); //fwrite的返回值是写的对象的数量
/*ret=fwrite(&i,sizeof(int),1,fp);
i=0;*/
/*fread(&i,sizeof(int),1,fp);
printf("i=%x\n",i);*/
fclose(fp); return 0;
}
fseek

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
FILE *fp;
int ret;
char buf[128]={0};
fp=fopen("1.txt","r+");
if(NULL==fp)
{
perror("fopen");
}
ret=fseek(fp,6,SEEK_SET); //成功返回0,失败返回非零
ret=fread(buf,sizeof(char),2,fp);
printf("buf=%s\n",buf);
fseek(fp,0,SEEK_CUR);
ret=fwrite("you are how",sizeof(char),11,fp);
fclose(fp);
return 0;
}
fputs与fgets

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
FILE *fp;
int ret;
char buf[128];
fp=fopen("1.txt","r+");
if(NULL==fp)
{
perror("fopen");
}
while(fgets(buf,sizeof(buf),stdin))
{
fputs(buf,fp); //等价于printf("%s\n",buf);
}
return 0;
}
fscanf与fprintf

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
int num;
char name[20];
float score;
}Stu;
int main()
{
int num=20;
float score=98.5;
Stu sArr[3]={1001,"zhangfei",66.5,1007,"liubei",96.5,1005,"guanyu",97.3};
int ret,i;
FILE *fp=fopen("file.txt","r+");
if(NULL==fp)
{
perror("fopen");
}
//ret=fprintf(fp,"%d %5.2f\n",num,score);
//num=0;
//score=0;
//ret=fscanf(fp,"%d%f",&num,&score);
for(i=0;i<3;i++)
{
ret=fprintf(fp,"%d %s %5.2f\n",sArr[i].num,sArr[i].name,sArr[i].score);
}
fseek(fp,0,SEEK_SET);
memset(sArr,0,sizeof(sArr));
for(i=0;i<3;i++)
{
ret=fscanf(fp,"%d%s%f",&sArr[i].num,sArr[i].name,&sArr[i].score);
}
fclose(fp);
return 0;
}

C语言强化——文件的更多相关文章

  1. BMP头文件格式以及C语言读取头文件【转】

    BMP头文件格式以及C语言读取头文件[转] (2011-12-24 22:59:17) 转载▼ 标签: 杂谈 分类: 各个领域的知识 BMP图像文件由三部分组成:位图文件头数据结构,它包含BMP图像文 ...

  2. c语言头文件中定义全局变量的问题

    c语言头文件中定义全局变量的问题 (转http://www.cnblogs.com/Sorean/) 先说一下,全局变量只能定义在 函数里面,任意函数,其他函数在使用的时候用extern声明.千万不要 ...

  3. C 语言 .h文件的作用

    C语言头文件的作用 最近在工作当中遇到了一点小问题,关于C语言头文件的应用问题,主要还是关于全局变量的定义和声明问题.学习C语言已经有好几年了,工作使用也近半年了,但是对于这部分的东西的确还没有深入的 ...

  4. 嵌入式C语言头文件的建立与使用

    如何正确编写 C 语言头文件和与之相关联的 c 源程序文件,这首先就要了解它们的各自功能. 要理解 C 文件与头文件(即.h)有什么不同之处,首先需要弄明白编译器的工作过程. 一般说来编译器会做以下几 ...

  5. C语言基础文件读写操作

    整理了一份C语言的文件读写件操作代码,测试时打开相应的注释即可. #include <stdio.h> #include <stdlib.h> #include <uni ...

  6. 51单片机C语言学习笔记6:51单片机C语言头文件及其使用

    很多初学单片机者往往对C51的头文件感到很神秘,而为什么要那样写,甚至有的初学者喜欢问,P1口的P为什么要大写,不大写行不行呢?其实这个是在头文件中用sfr定义的,现在定义好了的是这样的 sfr P1 ...

  7. C语言头文件

    最近在工作当中遇到了一点小问题,关于C语言头文件的应用问题,主要还是关于全局变量的定义和声明问题.学习C语言已经有好几年了,工作使用也近半年了,但是对于这部分的东西的确还没有深入的思考过.概念上还是比 ...

  8. C语言判断文件夹或者文件是否存在的方法【转】

     C语言判断文件夹或者文件是否存在的方法   方法一:access函数判断文件夹或者文件是否存在 函数原型: int access(const char *filename, int mode); 所 ...

  9. C语言程序设计--文件操作

    前言 这里尝试与Python对别的方法来学习C语言的文件操作,毕竟我是Pythoner. 文件打开与关闭 Python #因为是和C语言比对,所以不使用with filename = "/e ...

随机推荐

  1. python与系统做交互常用的模块和使用方法

    1.使用os模块与系统做简单命令的交互 >>>import os >>>os.popen('pwd') <open file 'pwd', mode 'r' ...

  2. 芯灵思SinA33开发板怎样安装虚拟机

    芯灵思SinA33开发板怎样安装虚拟机 今天入手一块芯灵思的开发板,型号为SIN-A33,用的是全志的A33芯片,与其它开发板不同的是, 芯灵思开发板手册上用来搭建开发环境的linux系统是cento ...

  3. C# to IL 4 Keywords and Operators(关键字和操作符)

    Code that is placed after the return statement never gets executed. In the first programgiven below, ...

  4. MINA线程模型

    一.三种工作线程: (一) Acceptor  thread: 该线程的作用是接收客户端的连接,并将客户端的连接导入到IOProcessor线程模型中.Acceptor thread在调用了Accep ...

  5. taro 开发注意点

    taro 开发注意点: 注意点 原因 如果要支持 React Native 端,必须采用 Flex 布局,并且样式选择器仅支持类选择器,且不支持 组合器 Taro RN 端是基于 Expo,因此不支持 ...

  6. React 中的 Component、PureComponent、无状态组件 之间的比较

    React 中的 Component.PureComponent.无状态组件之间的比较 table th:first-of-type { width: 150px; } 组件类型 说明 React.c ...

  7. python3+Flask 链接MySQL 时,提示“No module named MYSQLdb”

    python3+flask 链接Mysql时提示“No module named MYSQLdb” 解决: pip install mysqlclient

  8. 静态Map类型变量赋初始值

    private static Map<String,String> sysTypeList = new HashMap<String, String>(); static { ...

  9. 红外协议之NEC协议

    NEC协议载波:38khz 其逻辑1与逻辑0的表示如图所示: 逻辑1为2.25ms,脉冲时间560us:逻辑0为1.12ms,脉冲时间560us.所以我们根据脉冲时间长短来解码.推荐载波占空比为1/3 ...

  10. Visual Studio 2010 出现关于ActivityLog.xml错误的解决方案

    在用VS编写程序是第一次会跳出“Visual Studio has encountered an exception.This may be caused by an extension. You c ...