C语言文件基本操作
1.用文本方式储存‘1’,‘0’,‘2’存入文件,然后用二进制方式从文件开头读出一个short型数据,并验证结果是否正确
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
short m;
FILE *fp;
if((fp=fopen("C:\\vc\\sample.txt","r"))==NULL)
{
printf("打开文件失败\n");
exit();
}
fputc('',fp);
fputc('',fp);
fputc('',fp);
fclose(fp);
if((fp=fopen("C:\\vc\\sample.txt","r"))==NULL)
{
printf("打开文件失败\n");
exit();
}
fread(&m,,,fp);
fclose(fp);
printf("%d\n",m);
return ;
}
也可以使用fwrite()将3个字符写入文件
#include<stdio.h>
#include<stdlib.h>
int main()
{
short m;
FILE *fp;
if((fp=fopen("C:\\vc\\sample.txt","r+"))==NULL)
{
printf("打开文件失败\n");
exit();
}
char str[]= {'','',''}; ///单字符形式的初始化
fwrite(str,,,fp);
rewind(fp);
fread(&m,,,fp);
fclose(fp);
printf("%d\n",m);
return ;
}
///用“r+",“w+”的方式打开文件写完数据接着再读出来,不需要关闭文件再重新打开
#include<stdio.h>
#include<stdlib.h>
int main()
{
short a;
FILE *fp;
if((fp=fopen("C:\\vc\\sample.txt","w+"))==NULL)
exit();
fprintf(fp,"%d\n",);//长度为7字节,文本方式写入,内存中00110001 00110000
rewind(fp);
a=getw(fp);
printf("%d\n",a);//a的值为12337,short 取两个字节
return ;
}
#include<stdio.h>
#include<stdlib.h>
int main()
{
short a;
FILE *fp;
if((fp=fopen("C:\\vc\\sample.txt","w+"))==NULL)
exit();
putw(,fp);///将整数写入fp指向的文件,二进制写入内存中
///在内存中存为 00110001 00110000(由低位到高位)
rewind(fp);
fscanf(fp,"%d",&a);
printf("%d\n",a);
return ;
}
以下文件的输出结果是(123,45)
#include<stdio.h>
int main()
{
FILE *fp;
int i,k,n;
fp=fopen("C:\\vc\\c.dic","w+");
for(i=; i<; i++)
{
fprintf(fp,"%d",i);///文本方式按format所对应的格式字符串中规定的格式,将输出列表中的每一项输出到fp所对应的文件中
if(i%==)
fprintf(fp,"\n");
}
rewind(fp);///读写转换
fscanf(fp,"%d%d",&k,&n);///读取整型实型数据时,若遇到空格,Tab或者换行键则会将他们当做分隔符读出来丢弃,然后继续读取后面的数据
printf("%d,%d\n",k,n);
fclose(fp);
return ;
}
把a.dic文件中的内容复制到b.dic中
#include<stdio.h>
#include<stdlib.h>
void main()
{
FILE *in,*out;
char ch;
if((in=fopen("C:\\vc\\a.dic","r"))==NULL)
{
printf("cannot open infile\n");
exit();
}
if((out=fopen("C:\\vc\\b.dic","w"))==NULL)
{
printf("cannot open infile\n");
exit();
}///打开两个文件
ch=fgetc(in);
while(!feof(in))
{
fputc(ch,out);
ch=fgetc(in);
}
fclose(in);
fclose(out);
}
文本方式读取一段文字
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp;
char ch;
if((fp=fopen("C:\\vc\\a.dic","r"))==NULL)
{
printf("打开文件失败\n");
exit();
}
while()
{
ch=fgetc(fp);
putchar(ch);
if(ch==EOF)
break;
}
fclose(fp);
return ;
}
改写之后的程序:
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp;
char ch;
if((fp=fopen("C:\\vc\\a.dic","r"))==NULL)
{
printf("打开文件失败\n");
exit();
}
ch=fgetc(fp);
while(!feof(fp))
{
putchar(ch);
ch=fgetc(fp);
}
fclose(fp);
return ;
}
计算该文件内容的字节数
#include<stdio.h>///计算该文件内容的字节数
int main()
{
FILE *fp;
long int n;
fp=fopen("C:\\vc\\b.dic","rb");
fseek(fp,,SEEK_END);///读写位置指针移动到了文件尾
n=ftell(fp);
fclose(fp);
printf("%ld",n);
return ;
}
C语言文件基本操作的更多相关文章
- 文件基本操作 (C语言)
一切皆文件 ---Linux 头文件 <stdio.h> 中定义了文件的相关操作 #include <stdio.h> 文件操作基本流程: 打开:fopen 相关操作 关闭:f ...
- C语言文件操作
C语言文件操作,以下以基本的例子和说明来展开怎么通过C语言来进行文件操作. 操作文件,我们得需要知道什么?当然是路径和文件名. 首先我需要知道我操作的文件在哪里,叫什么名字.在C语言中还存在一个打开方 ...
- c语言文件读写操作总结
C语言文件读写操作总结 C语言文件操作 一.标准文件的读写 1.文件的打开 fopen() 文件的打开操作表示将给用户指定的文件在内存分配一个FILE结构区,并将该结构的指针返回给用户程序,以后用户程 ...
- java之文件基本操作
java之文件基本操作 1 使用 BufferedReader 在控制台读取字符 public static void readChar() throws IOException{ char c; I ...
- go语言文件操作,这期资料比较详细( 欢迎加入go语言群: 218160862 )
go语言文件操作,这期资料比较详细 欢迎加入go语言群: go语言深圳群 golang深圳 218160862 点击加入 文件操作 func Open(name string) (file *File ...
- android多国语言文件夹文件汇总
android多国语言文件夹文件汇总如下: 中文(中国):values-zh-rCN 中文(台湾):values-zh-rTW 中文(香港):values-zh-rHK 英语(美国):values-e ...
- 不可或缺 Windows Native (10) - C 语言: 文件
[源码下载] 不可或缺 Windows Native (10) - C 语言: 文件 作者:webabcd 介绍不可或缺 Windows Native 之 C 语言 文件 示例cFile.h #ifn ...
- C语言---文件
1. 需要了解的概念 包括:数据流.缓冲区.文件类型.文件存取方式 1.1 数据流: 指程序与数据的交互是以流的形式进行的.进行C语言文件的存取时,都会先进行“打开文件”操作,这个操作就是在打开数据流 ...
- CUBRID学习笔记 29 web管理中文语言文件 CUBRID教程
网站的中文语言文件部分 http://files.cnblogs.com/files/wang2650/Messages.7z
随机推荐
- Visual Studio中添加API断点
如:添加 PostMessageA 断点 {,,USER32.DLL}_PostMessageA@16 //判断为WM_CLOSE消息*(int*)(esp + 8) == 0x0010
- MySQL学习【第三篇用户管理】
一.用户管理 1.给mysql用户设密码以及删除用户 1.给mysql的root用户设置密码 [root@db02 scripts]# mysqladmin -uroot -p password '1 ...
- pom.xml文件报MavenArchiver错误 org.apache.maven.archiver.MavenArchiver.getManifest(org.apache.maven.project.MavenProject, org.apache.maven.archiver.MavenArchiveConfiguration)
第一种方式 war项目 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId> ...
- Mongodb从库配置
1. 先以master方式启动mongodb 2. 导入主库的数据文件:/data/mongodb-3.0.12/bin/mongorestore /data/tmp/mongodbbak/ 3. 关 ...
- Go搭建一个博客系统
go语言环境就不用多说了,版本肯定越高越好,这里用go1.10 先放着
- 20145234黄斐《信息安全系统设计基础》第八周(Linux下vim相关命令)
Linux下vim相关命令 在编辑程序时经常使用vim,所以记住一些常用的指令还是很有必要的 文件命令 vim file 打开单个文件vim file vim file1 file2 file3 .. ...
- 北京Uber优步司机奖励政策(1月4日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 机器学习实战:KNN代码报错“AttributeError: 'dict' object has no attribute 'iteritems'”
报错代码: sortedClassCount = sorted(classCount.iteritems(), key=operator.itemgetter(1), reverse=True) 解决 ...
- CakePHP 查询总结
返回 $this->Post->buildQuery(); 返回: Array ( [conditions] => [fields] => [joins] => Arra ...
- Andorid自定义attr的各种坑
本文来自网易云社区 作者:孙有军 在开发Andorid应用程序中,经常会自定义View来实现各种各样炫酷的效果,在实现这吊炸天效果的同时,我们往往会定义很多attr属性,这样就可以在XML中配置我们想 ...