Linux IO chatter
来源:互联网 发布:淘宝买new3ds 编辑:程序博客网 时间:2024/06/17 22:32
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
/*
struct user{
char name[10];
// int num;
// int age;
char msg_send[80];
char msg_recv[80];
}boya,boyb,*pp,*qq;
*/
int main(void){
int fp1,fp2;
char name_a[20],name_b[20] ;
char buf_recv[30];
char buf_send[30];
// char ch;
// int i;
int m,n;
// pp=boya;
// qq=boyb;
printf("\n please input your name and your friend name \n");
scanf("%s %s",name_a,name_b) ;
//printf("your name is %s", boya->name");
while(1)
{
//open the file recv_b
if((fp1=open("note_recv_b.txt",O_RDWR|O_CREAT|O_APPEND))== 0){
printf("Cannot open file note_recv_a any key exit!");
getchar();
exit(1);
}
//if new message comes
//read message from B
m = read(fp1,buf_recv,sizeof(buf_recv));//read from file note_recv_b
//
if(m > 0)
{
//print message from B
printf("hello %s,your get message from %s: \n ",name_a,name_b);
// print message on screen
write(STDOUT_FILENO,buf_recv,m);//print to screen
close(fp1);
}
else
{
if((fp2=open("note_recv_a.txt",O_RDWR|O_CREAT|O_APPEND))== 0){
printf("Cannot open file note_recv_a any key exit!");
getchar();
exit(1);
}
printf("\n you can send message to your friend %s :\n",name_b);
scanf("%s\n",buf_send);
//n = read(STDIN_FILENO,buf_send,30);//read send msg
//move fp2 back 30 offset
// lseek(fp2,30,SEEK_SET);
write(fp2,buf_send,sizeof(buf_send));//write to note_recv_a
//close file
printf("you have send message to B:%s \n",buf_send);
//fclose(fp2);
close(fp2);
}
//move fp to the end of file
//lseek(fp1,30, SEEK_SET);
//close file
//close(fp1) ;
//open note_recv_a to send message to B
}
/*
for(i=0;i<1;i++,pp++)
scanf("%s %s",boya->name,boya->,);
pp=boya;
fwrite(pp,sizeof(struct user),2,fp);
rewind(fp);
fread(qq,sizeof(struct user),2,fp);
printf("\n\nname\tnumber age addr\n");
for(i=0;i<2;i++,qq++)
printf("%s\t%5d%7d %s\n",qq->name,qq->num,qq->age,qq->addr);
fclose(fp);
*/
return 0 ;
}
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
/*
struct user{
char name[10];
// int num;
// int age;
char msg_send[80];
char msg_recv[80];
}boya,boyb,*pp,*qq;
*/
int main(void){
int fp1,fp2;
char name_a[20],name_b[20] ;
char buf_recv[30];
char buf_send[30];
// char ch;
// int i;
int m,n;
// pp=boya;
// qq=boyb;
printf("\n please input your name and your friend name \n");
scanf("%s %s",name_a,name_b) ;
//printf("your name is %s", boya->name");
while(1)
{
//open the file recv_b
if((fp1=open("note_recv_b.txt",O_RDWR|O_CREAT|O_APPEND))== 0){
printf("Cannot open file note_recv_a any key exit!");
getchar();
exit(1);
}
//if new message comes
//read message from B
m = read(fp1,buf_recv,sizeof(buf_recv));//read from file note_recv_b
//
if(m > 0)
{
//print message from B
printf("hello %s,your get message from %s: \n ",name_a,name_b);
// print message on screen
write(STDOUT_FILENO,buf_recv,m);//print to screen
close(fp1);
}
else
{
if((fp2=open("note_recv_a.txt",O_RDWR|O_CREAT|O_APPEND))== 0){
printf("Cannot open file note_recv_a any key exit!");
getchar();
exit(1);
}
printf("\n you can send message to your friend %s :\n",name_b);
scanf("%s\n",buf_send);
//n = read(STDIN_FILENO,buf_send,30);//read send msg
//move fp2 back 30 offset
// lseek(fp2,30,SEEK_SET);
write(fp2,buf_send,sizeof(buf_send));//write to note_recv_a
//close file
printf("you have send message to B:%s \n",buf_send);
//fclose(fp2);
close(fp2);
}
//move fp to the end of file
//lseek(fp1,30, SEEK_SET);
//close file
//close(fp1) ;
//open note_recv_a to send message to B
}
/*
for(i=0;i<1;i++,pp++)
scanf("%s %s",boya->name,boya->,);
pp=boya;
fwrite(pp,sizeof(struct user),2,fp);
rewind(fp);
fread(qq,sizeof(struct user),2,fp);
printf("\n\nname\tnumber age addr\n");
for(i=0;i<2;i++,qq++)
printf("%s\t%5d%7d %s\n",qq->name,qq->num,qq->age,qq->addr);
fclose(fp);
*/
return 0 ;
}
0 0
- Linux IO chatter
- salesforce之chatter
- salesforce chatter 调查的相关内容
- Salesforce chatter:userPhotoUpload Component 使用
- Linux IO
- Linux IO
- 9.比较器 - 振荡(chatter)来自何处?
- linux 下IO过程--linux IO堆栈
- Direct IO in linux
- Linux 异步IO机制
- asynchronous io for linux
- Linux IO多路复用
- IO复用,linux poll
- linux监控多线程io
- Linux异步IO
- IO复用,linux poll
- linux 直接IO机制
- linux监控多线程io
- OCR银行卡辨识SDK
- 求字符长度
- POJ 2187 Beauty Contest (旋转卡壳法)
- 双链表
- OpenCV尺寸调整
- Linux IO chatter
- 排序算法:归并排序
- canvas学习
- JavaFx JFXPanel无法刷新,只能第一次显示的问题
- git 多用户
- hibernate一定要主键
- MySQL的分页查询语句
- 深入了解Struts2返回JSON数据的原理及具体应用范例
- Java之实例变量和类变量