?? work1.c
字號:
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAXSIZE 101
main()
{
//使用標(biāo)準(zhǔn)函數(shù)
printf("Now is stantard:\n");
int i,j,fd,size,len,buf[101];
for(i=1;i<=100;i++) buf[i-1]=i; //生成buff緩沖
int buf_r[MAXSIZE];
//open創(chuàng)建文件
if((fd=open("tmp.c",O_CREAT | O_TRUNC | O_RDWR,0666))<0) {
perror("");
exit(1);
}
//write向文件中寫入內(nèi)容
if((fd=write(buf,fd,101)<0) {
perror("");
exit(1);
}
lseek(fd,49,0); //更改當(dāng)前指針位置,自原點(diǎn)偏移49,即第50個字節(jié)處
for(j=0;j<MAXSIZE;j++) buf_r[j]=0;
read(fd,buf_r,1);
printf("\nThe 50th byte is:%d \n",buf_r); //讀取該字節(jié)處內(nèi)容并打印
lseek(fd,99,0); //更改當(dāng)前指針位置,自原點(diǎn)偏移99,即第100個字節(jié)處
for(j=0;j<MAXSIZE;j++) buf_r[j]=0;
read(fd,buf_r,1);
printf("\nThe 100th byte is:%d \n",buf_r); //讀取該字節(jié)處內(nèi)容并打印
//close關(guān)閉文件
if(close(fd)<0){
perror("");
exit(1);
}
//使用IO庫
FILE *fp; //生成FILE指針
char ch;
if((fp=fopen("myfile.txt","w"))==NULL) //fopen創(chuàng)建文件
{
printf("File cannot be opened\n");
exit(1);
}
for(i=0;i<=100;i++) //向文件內(nèi)寫入內(nèi)容
{
if(i!=100)
fputc(i,fp);
else
{
fputc(i,fp);
fputs("\0",fp);
}
}
printf("Now is IO:\n");
fseek(fp,49,SEEK_SET); //更改當(dāng)前指針位置,自原點(diǎn)偏移49,即第50個字節(jié)處
//while((ch=fgetc(fp))!=EOF)
printf("The 50th byte is:%c\n",fgetc(fp));
fseek(fp,99,SEEK_SET); //更改當(dāng)前指針位置,自原點(diǎn)偏移99,即第100個字節(jié)處
//while((ch=fgetc(fp))!=EOF)
printf("The 100th byte is:%c\n",fgetc(fp));
fclose(fp); //fclose關(guān)閉文件
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -