day1:
1.
#includeint main() { FILE *fp; if (fp=fopen("1.txt","r")==NULL) { perror("fail to fopen"); return -1; } fclose(fp); return 0; }
2.
#includeint main() { FILE *fp; if (fp=fopen("1.txt","r")==NULL) { printf("faile to fopen:%sn",strerror(errno)); return -1; } fclose(fp); return 0; }
day2:使用标准i/o写两个学生结构体数据到文件
#include#include typedef struct{ char name[20]; int age; }stutent; int main(void) { FILE* fp = fopen("./struct.txt","w+"); if(fp==NULL) return -1; stutent stutent1 = {"lili",13}; stutent stutent2 = {"ming",14}; if((fwrite(&student1,sizeof(student),1,fp)) <0){ perror("fwrite"); } if((fwrite(&student2,sizeof(studentl),1,fp)) <0){ perror("fwrite"); } fclose(fp); return 0; }
day3:(35条消息) C实现 每隔1s向time.txt文件输出系统时间(C I/O函数)_文字篇章-CSDN博客
1 #include2 #include 3 #include 4 #include 5 6 int main(int argc,char *arfv[]){ 7 FILE *fp; 8 time_t ctime; 9 struct tm *ctimestr; 10 int linecount = 0; 11 char buf[32]; 12 fp=fopen("test.txt","a+"); 13 if(fp==NULL){ 14 perror("fopen"); 15 return 0; 16 } 17 //calculate test.txt line 18 while(fgets(buf,32,fp)==NULL){ 19 if(buf[strlen(buf)-1] == 'n'){ 20 linecount++; 21 } 22 } 23 while(1){ 24 ctime=time(NULL); 25 ctimestr=localtime(&ctime); 26 printf("%04d-%02d-%02d %02d:%02d:%02dn",ctimestr->tm_year+1900,ctimestr->tm_mon+1,ctimestr->tm_mday,ctimestr->tm_hour,ctimestr->tm_min,ctimestr->tm_sec); 27 fprintf(fp,"%d,%04d-%02d-%02d %02d:%02d:%02dn",linecount,ctimestr->tm_year+1900,ctimestr->tm_mon+1,ctimestr->tm_mday,ctimestr->tm_hour,ctimestr->tm_min,ctimestr->tm_sec); 28 29 fflush(fp); 30 sleep(1); 31 } 32 fclose(fp); 33 } ~
day5:Linux系统下,如何获取一个文件夹内所有的内容,并且打印出文件大小和最后修改日期,并用tree的方式打印_小菜鸡的博客-CSDN博客
#include2 #include 3 #include 4 #include 5 #include 6 #include 7 8 int main(int argc,char **argv){ 9 char file_path[100] = {0}; 10 printf("请输入你要查看的目录:n"); 11 scanf("%s", file_path); 12 13 DIR *dp; 14 struct dirent *dt=NULL; 15 16 struct stat buf; 17 int ret; 18 ret=stat(file_path, &buf) ; 19 if (ret == -1) 20 { 21 printf("目录不存在n"); 22 return -1; 23 } 24 if (!S_ISDIR(buf.st_mode)) 25 { 26 printf("输入的目录有错误!n"); 27 return -1; 28 } 29 dp=opendir(file_path); 30 if(dp<0){ 31 perror("orendir"); 32 return 0; 33 } 34 while((dt=readdir(dp))!=NULL){ 35 ret=stat(dt->d_name,&buf); 36 if(ret<0){ 37 perror("stat"); 38 return 0; 39 } 40 struct tm *t; 41 t=localtime(&buf.st_ctime); 42 printf("name:%8s size:%09d time: %04d-%02d-%02d %02d:%02dn",dt->d_name, (int)buf.st_size,t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min); 43 } 44 closedir(dp); 45 }



