栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

文件I/O作业

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

文件I/O作业

day1:

1.

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


2.

#include 
int 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 #include 
  2 #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博客

#include 
  2 #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 }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/605230.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号