1.9
来源:互联网 发布:阿里云备案地址随便写 编辑:程序博客网 时间:2024/05/16 02:02
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<string.h>
#define MAX 100
int read_line(int fd, char *buf, int count)
{
int i;
char ch;
for(i = 0; i < count; i++)
{
if((read(fd,&ch,1)) < 0)
{
perror("read error!\n");
exit(1);
}
if(ch == '\n')
{
buf[i] = '\0';
return i;
}
buf[i] = ch;
}
buf[i - 1] = '\0';
return count;
}
int main()
{
int i;
int fd;
int w_count;
int r_count;
char filename[MAX];
char r_buf[MAX];
char w_buf[MAX];
printf("Please input filename:\n");
scanf("%s",filename);
if((fd = open(filename, O_CREAT | O_RDWR, 0755)) < 0)
{
perror("open error!\n");
exit(1);
}
for(i = 0; i < 3; i++)
{
memset(w_buf,0,sizeof(w_buf));
scanf("%s",w_buf);
if((w_count = write(fd,w_buf,strlen(w_buf))) < 0)
{
perror("write error!\n");
exit(1);
}
else
{
write(fd,"\n",1);
}
}
lseek(fd,0,SEEK_SET);
#if 0
if((r_count = read(fd,r_buf,sizeof(r_buf))) < 0)
{
perror("read error!\n");
exit(1);
}
else
{
r_buf[w_count * 3 + 3] = '\0';
printf("read data: %s\n",r_buf);
}
#endif
for(i = 0; i < 3; i++)
{
memset(r_buf,0,sizeof(r_buf));
read_line(fd,r_buf,100);
printf("read data: %s\n",r_buf);
}
close(fd);
return 0;
}
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<string.h>
#define MAX 100
int read_line(int fd, char *buf, int count)
{
int i;
char ch;
for(i = 0; i < count; i++)
{
if((read(fd,&ch,1)) < 0)
{
perror("read error!\n");
exit(1);
}
if(ch == '\n')
{
buf[i] = '\0';
return i;
}
buf[i] = ch;
}
buf[i - 1] = '\0';
return count;
}
int main()
{
int i;
int fd;
int w_count;
int r_count;
char filename[MAX];
char r_buf[MAX];
char w_buf[MAX];
printf("Please input filename:\n");
scanf("%s",filename);
if((fd = open(filename, O_CREAT | O_RDWR, 0755)) < 0)
{
perror("open error!\n");
exit(1);
}
for(i = 0; i < 3; i++)
{
memset(w_buf,0,sizeof(w_buf));
scanf("%s",w_buf);
if((w_count = write(fd,w_buf,strlen(w_buf))) < 0)
{
perror("write error!\n");
exit(1);
}
else
{
write(fd,"\n",1);
}
}
lseek(fd,0,SEEK_SET);
#if 0
if((r_count = read(fd,r_buf,sizeof(r_buf))) < 0)
{
perror("read error!\n");
exit(1);
}
else
{
r_buf[w_count * 3 + 3] = '\0';
printf("read data: %s\n",r_buf);
}
#endif
for(i = 0; i < 3; i++)
{
memset(r_buf,0,sizeof(r_buf));
read_line(fd,r_buf,100);
printf("read data: %s\n",r_buf);
}
close(fd);
return 0;
}
0 0
- 1.9
- 1.9
- 1.9
- 1.9
- 1.9
- 1.9
- 1.9 小结
- 练习1.9
- 1.9汇报
- Chromium 1.9
- 黄迪明1.9
- 1.9:容器
- 1.9volatile
- 1.9 容器
- 1.9.信号(Signals)
- WinLicense 1.9.7.0 released
- Themida 1.9.7.0 released
- AMFPHP 1.9 教程
- Swift-深浅拷贝与内存管理(高级)
- Makefile(2)
- 成功修改STM32启动程序(自定义的分散加载模式)
- Makefile(3)
- C++虚函数与JAVA中抽象函数比较 接口与抽象类比较
- 1.9
- 玩转Nodejs日志管理log4js
- 今天正式进入csdn
- memset
- Makefile(4)
- HTTP中的GET和POST何时使用
- Makefile(5)
- C++中的64位整数
- SecureCST的基本用法