#include<string.h>
#include<stdlib.h>
int main()
{
char a[20]={0};
FILE *fp;
fp=fopen("a.txt","a");//rb,rb+且均为作出修改//wb,wb+都不行,不能输入且清除了文件信息,准备输出到文件//注意在程序中的"a"文件在此处必须用a.txt引用
fscanf(fp,"%s",a);
printf("%s\n",a);
return 0 ;
}
#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;
int 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))==NULL){
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,30);//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
}
//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
if((fp2=open("note_recv_a.txt",O_RDWR|O_CREAT))==NULL){
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",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(fp1);
close(fp2);
/*
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 ;
}