当前位置:网站首页>Job 7.28 file IO and standard IO
Job 7.28 file IO and standard IO
2022-07-29 07:20:00 【Unknown college student M】
1. Use standards IO complete time.txt
Request to create a time.txt, The format of storage content is as follows :
[1] 2022-07-28 17:15:06
[2] 2022-07-28 17:15:07
[3] 2022-07-28 17:15:08
ctrl + c Exit procedure , Restart the program after a while
[1] 2022-07-28 17:15:06
[2] 2022-07-28 17:15:07
[3] 2022-07-28 17:15:08 <-------------------
[4] 2022-07-28 17:16:31
[5] 2022-07-28 17:16:32
Code
#include <stdio.h>
#include <unistd.h>
#include <time.h>
int main(int argc, const char *argv[])
{
FILE *fp=fopen("./time.txt","a+");
if(NULL==fp)
{
perror("fopen");
return -1;
}
char a;
int count=1;
while((a=fgetc(fp))!=-1)
{
if(a=='\n')
{
count++;
}
}
time_t t;
struct tm *info=NULL;
while(1)
{
t=time(NULL);
info=localtime(&t);
fprintf(fp,"[%02d] %d-%02d-%02d %02d:%02d:%02d\n", \
count,info->tm_year+1900, info->tm_mon+1, info->tm_mday,\
info->tm_hour, info->tm_min, info->tm_sec);
count++;
fflush(fp);
sleep(1);
}
fclose(fp);
return 0;
}
Running results

2. Required documents IO Copy a picture
Code
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define MSG_ERR(msg) do{
\ fprintf(stderr,"line: %d ", __LINE__); \ perror(msg);\ }while(0)
int main(int argc, const char *argv[])
{
int fd1=open("/home/ubuntu/ picture /sllh.jpg",O_RDONLY);
if(fd1<0)
{
MSG_ERR("open");
return -1;
}
int fd2=open("./sllh.jpg",O_WRONLY|O_CREAT|O_TRUNC,0777);
if(fd2<0)
{
MSG_ERR("open");
return -1;
}
char buf[100];
ssize_t c;
while((c=read(fd1,buf,100))>0)
{
write(fd2,buf,c);
}
close(fd1);
close(fd2);
return 0;
}
Running results
take /home/ubuntu/ picture /sllh.jpg Copy to current directory

3. additional : Using standard IO Copy a picture
Code
#include <stdio.h>
#define MSG_ERR(msg) do{
\ fprintf(stderr,"line: %d ", __LINE__); \ perror(msg);\ }while(0)
int main(int argc, const char *argv[])
{
FILE *fp1=fopen("/home/ubuntu/ picture /sllh.jpg","r");
if(fp1<0)
{
MSG_ERR("fopen");
return -1;
}
FILE *fp2=fopen("./sllh.jpg","w+");
if(fp2<0)
{
MSG_ERR("open");
return -1;
}
char buf[100];
size_t c;
while((c=fread(buf,1,100,fp1))>0)
{
fwrite(buf,1,c,fp2);
}
fclose(fp1);
fclose(fp2);
return 0;
}
Running results
take /home/ubuntu/ picture /sllh.jpg Copy to current directory

边栏推荐
猜你喜欢
随机推荐
Spark Learning Notes (VII) -- spark core core programming - RDD serialization / dependency / persistence / partition / accumulator / broadcast variables
解决CSDN因版权不明而无法发布博客的问题
Section 7 - compilation of programs (preprocessing operations) + links
Round avatar of user list and follow small blocks
Vite3.0都发布了,你还能卷得动吗(新特性一览)
Tp6 use protobuf
Personal blog system (with source code)
CMOS芯片制造全工艺流程
CAN&CANFD综合测试分析软件LKMaster与PCAN-Explorer 6分析软件的优势对比
WPF interface layout must know basis
2022-07-28: what is the output of the following go language code? A:AA; B:AB; C:BA; D:BB。 package main import ( “fmt“ ) func main() { f
WPF simple login page completion case
gin 模版
Homebrew brew update doesn't respond for a long time (or stuck in updating homebrew...)
Synchronous / asynchronous, blocking / non blocking and IO
太空射击第17课: Game Over (結束)
5-整合swagger2
Gin template
gcc/g++的使用
暑期总结(二)


![[OpenGL] use of shaders](/img/73/1322afec8add6462ca4b82cb8112d1.png)




