一个基础的文件读写(复习)
来源:互联网 发布:毕加索美工钢笔推荐 编辑:程序博客网 时间:2024/06/06 02:15
//一个基础的文件读写,(由于上传原因,代码的格式不太好,仅供参考,代码编写测试均成功,仅供参考)
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main(int argc,char *argv[])
{
int fd=-1;
int ret=-1;
char buf[100]={0};
char writebuf[20]="I LOVE Linux";
//第一步:打开一个文件
if(fd<0)
{
else
printf("fd=%d.\n",fd);
//第二步:读写文件
/*
//读文件
ret= read(fd,buf,6);
if(ret<0)
{
else
printf("文件读取成功,文件读取字节数为%d.\n",ret);
printf("读取的内容为:[%s].\n",buf);
*/
//写文件
ret=write(fd,writebuf,strlen(writebuf));
if(ret<0)
{
else
printf("文件写成功,写入了%d个字节.\n",ret);
//第三步:关闭文件
close(fd);
return 0;
}
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main(int argc,char *argv[])
{
int fd=-1;
int ret=-1;
char buf[100]={0};
char writebuf[20]="I LOVE Linux";
//第一步:打开一个文件
fd=open("a.txt",O_RDWR);
if(fd<0)
{
perror("文件打开错误.\n");
return(-1);
}else
printf("fd=%d.\n",fd);
//第二步:读写文件
/*
//读文件
ret= read(fd,buf,6);
if(ret<0)
{
printf("文件读取失败.\n");
_exit(-1);
}else
printf("文件读取成功,文件读取字节数为%d.\n",ret);
printf("读取的内容为:[%s].\n",buf);
*/
//写文件
ret=write(fd,writebuf,strlen(writebuf));
if(ret<0)
{
printf("写文件失败.\n");
_exit(-1);
}else
printf("文件写成功,写入了%d个字节.\n",ret);
//第三步:关闭文件
close(fd);
return 0;
}
0 0
- 一个基础的文件读写(复习)
- 文件读写的一个例子
- ios 读写文件-复习篇
- linux C复习:文件操作(创建、打开、读写)
- NOIP复赛复习(三)文件读写与数论模板
- NOIP复赛复习(三)文件读写与数论模板
- java基础 字符文件的读写
- delphi ini文件的基础读写。
- C++复习:一个小例子复习基础
- 一个BMP文件读写的程序
- Javascript 读写文件的一个例子
- 一个读写csv文件的C#类
- ini文件读写的一个实例
- 一个文件读写的简易例子
- 一个读写csv文件的C#类
- c语言文件读写的一个例子
- Python读写文件的一个问题
- 一个关于文件读写的小程序
- Python-线程后篇生肉
- js返回上一页并刷新
- css继承、层叠、特殊
- poj 1873 The Fortified Forest
- Universal-Image-Loader源码阅读(20)-memory/impl/LRULimitedMemoryCache
- 一个基础的文件读写(复习)
- JAVA并发编程学习笔记之AQS源码分析(共享与互斥)(r)
- [Python]网络爬虫(六):一个简单的百度贴吧的小爬虫
- linux下修改mysql的root密码
- Servlet实现文件上传,可多文件上传
- 浮动元素问题
- PCI Express
- javascript 事件
- MyCat快速入门指南