文件I/O作业

day1:

1.

#include <stdio.h>
int main()
{
FILE *fp;
if (fp=fopen("1.txt","r")==NULL)
  {
   perror("fail to fopen");
   return -1;
  }
fclose(fp);
return 0;
}


2.

#include <stdio.h>
int main()
{
FILE *fp;
if (fp=fopen("1.txt","r")==NULL)
  {
   printf("faile to fopen:%s\n",strerror(errno));
   return -1;
  }
fclose(fp);
return 0;
}

day2:使用标准i/o写两个学生结构体数据到文件

#include <stdio.h>
#include <string.h>
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 #include <time.h>
  2 #include <stdio.h>
  3 #include <unistd.h>
  4 #include <string.h>
  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:%02d\n",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:%02d\n",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博客

#include <dirent.h>
  2 #include <stdio.h>
  3 #include <sys/types.h>
  4 #include <sys/stat.h>
  5 #include <unistd.h>
  6 #include <time.h>
  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:%02d\n",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 }

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值