情報システム科学3サポートページ(担当 田中哲朗)
4/26課題のプログラムリスト
#include <stdio.h>
extern int errno;
#include <fcntl.h>
main(int ac, char **ag){
char buf[16*1024];
int size;
int ifd, ofd;
if(ac!=3) exit(1);
if((ifd=open(ag[1],O_RDONLY))<0){
fprintf(stderr,"Failed opening file %s\n",ag[1]);
fprintf(stderr,"errno=%d\n",errno);
exit(1);
}
if((ofd=open(ag[2],O_WRONLY|O_CREAT,0666))<0){
fprintf(stderr,"Failed creating file %s\n",ag[2]);
fprintf(stderr,"errno=%d\n",errno);
exit(1);
}
while((size=read(ifd,buf,sizeof(buf)))>0){
write(ofd,buf,size);
}
close(ifd);
close(ofd);
}
5/24課題のプログラムリスト
setupFile.c
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
void setupFile(char *fileName){
char buf[256];
int i,fd;
for(i=0;i<256;i++) buf[i]=i;
if((fd=open(fileName,O_WRONLY|O_CREAT,0600))<0){
fprintf(stderr,"Failed in opening file");
exit(1);
}
if(write(fd,buf,256)<0){
fprintf(stderr,"Failed in writing file");
exit(1);
}
close(fd);
}
int main(int ac,char **ag){
char *fileName="dummy";
if(ac>1) fileName=ag[1];
setupFile(fileName);
return 0;
}
readFile.c
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
void readFile(char *fileName){
char c;
int i,fd;
if((fd=open(fileName,O_RDONLY))<0){
fprintf(stderr,"Failed in opening file");
exit(1);
}
for(i=0;i<256;i++){
int rsize;
if((rsize=read(fd,&c,1))<0){
fprintf(stderr,"Failed in reading file");
exit(1);
}
else if(rsize==0){
fprintf(stderr,"read size=0");
break;
}
printf("%d: %d\n",i,c);
sleep(1);
}
close(fd);
}
int main(int ac,char **ag){
char *fileName="dummy";
if(ac>1) fileName=ag[1];
readFile(fileName);
return 0;
}
clearFile.c
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
void clearFile(char *fileName){
char buf[256];
int i,fd;
for(i=0;i<256;i++) buf[i]=0;
if((fd=open(fileName,O_WRONLY|O_CREAT,0600))<0){
fprintf(stderr,"Failed in opening file");
exit(1);
}
if(write(fd,buf,256)<0){
fprintf(stderr,"Failed in writing file");
exit(1);
}
close(fd);
}
int main(int ac,char **ag){
char *fileName="dummy";
if(ac>1) fileName=ag[1];
clearFile(fileName);
return 0;
}
truncFile.c
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
void truncFile(char *fileName){
char buf[256];
int i,fd;
for(i=0;i<256;i++) buf[i]=i;
if((fd=open(fileName,O_WRONLY|O_TRUNC,0600))<0){
fprintf(stderr,"Failed in opening file");
exit(1);
}
close(fd);
}
int main(int ac,char **ag){
char *fileName="dummy";
if(ac>1) fileName=ag[1];
truncFile(fileName);
return 0;
}
getcFile.c
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
void getcFile(char *fileName){
int i;
FILE *fp;
if((fp=fopen(fileName,"r"))==NULL){
fprintf(stderr,"Failed in opening file");
exit(1);
}
for(i=0;i<256;i++){
int c;
if((c=getc(fp))==EOF){
fprintf(stderr,"EOF\n");
break;
}
printf("%d: %d\n",i,c);
sleep(1);
}
fclose(fp);
}
int main(int ac,char **ag){
char *fileName="dummy";
if(ac>1) fileName=ag[1];
getcFile(fileName);
return 0;
}