Chinaunix首页 | 论坛 | 博客

OS

  • 博客访问: 2280409
  • 博文数量: 691
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2660
  • 用 户 组: 普通用户
  • 注册时间: 2014-04-05 12:49
个人简介

不浮躁

文章分类

全部博文(691)

文章存档

2019年(1)

2017年(12)

2016年(99)

2015年(207)

2014年(372)

分类: 嵌入式

2015-07-26 21:53:23

#include
#define MAX 20
int main()
{
 char line[MAX];
 while (fgets(line, MAX, stdin) != NULL && line[0] != '\n')
 {
  fputs("result: ", stdout);
  getchar();
  getchar();
  fputs(line, stdout);
 }
 return 0;
}

#include
int main(int argc, char *argv[])
{
 FILE *fpr, *fpw;
 char ch;
 if (argc != 3)
 {
  printf("argument is error\n");
  
  return -1;
 }
 if ((fpr = fopen(argv[1], "r")) == NULL)
 {
  printf("open source is error\n");
  return -1;
 }
 printf("file number is %d\n", fpr->_fileno);
 if ((fpw = fopen(argv[2], "w")) == NULL)
 {
  printf("open target is error\n");
  return -1;
 }
 ch = getc(fpr);
 while (ch != EOF)
 {
  fputc(ch, fpw);
  ch = getc(fpr);
 }
 return 0;
}

#include
#define MAX 100
int main(int argc, char *argv[])
{
 char buf[MAX];
 FILE *fpr, *fpw;
 char ch;
 if (argc != 3)
 {
  printf("argument is error\n");
  
  return -1;
 }
 if ((fpr = fopen(argv[1], "r")) == NULL)
 {
  printf("open source is error\n");
  return -1;
 }
// printf("readfile number is %d\n", fpr->_fileno);
 if ((fpw = fopen(argv[2], "w")) == NULL)
 {
  printf("open target is error\n");
  return -1;
 }
 while (fgets(buf, MAX, fpr) != NULL)
 {
  fputs(buf, fpw);
 }
 return 0;
}

在已写时间的文件中继续追加时间记录
 
#include 
#include 
#include 
#include
#define BUFF 50
int main()
{
 FILE *fp;
 int count = 0;
 char buf[BUFF];
 time_t t;
 struct tm *ts;
 if ((fp = fopen("time.txt", "r+")) == NULL)
 {
  perror("open error");
  return -1;
 }
 while (fgets(buf, BUFF, fp) != NULL)
 {
  count++;
 }
 while(1)
 {
  time(&t);
  ts = localtime(&t);
  fprintf(fp, "%d,%d-%d-%d %d:%d:%d\n", ++count, ts->tm_year+1900, ts->tm_mon+1, ts->tm_mday, ts->tm_hour, ts->tm_min, ts->tm_sec);
  fflush(fp);
  sleep(1);
 }
 return 0;
}

原文地址:用文件IO操作实现ls命令 作者:xutianxi


#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include

#define N 256

void DisplayFile(struct stat buf, char fname[])
{
 int n;
 struct tm *tp;

 switch (buf.st_mode & S_IFMT)
 {
 case S_IFREG:
  printf("-");
  break;
 case S_IFDIR:
  printf("d");
  break;
 case S_IFSOCK:
  printf("s");
  break;
 case S_IFLNK:
  printf("l");
  break;
 case S_IFBLK:
  printf("b");
  break;
 case S_IFCHR:
  printf("c");
  break;
 case S_IFIFO:
  printf("p");
  break;
 }

 for (n=8; n>=0; n--)
 {
  if (buf.st_mode & (1<   {
   switch (n % 3)
   {
   case 2 :
    printf("r");
    break;
   case 1 :
    printf("w");
    break;
   case 0:
    printf("x");
    break;
   }
  }
  else
  {
   printf("-");
  }
 }

 printf(" %u ", buf.st_nlink);
 printf("%s ", getpwuid(buf.st_uid)->pw_name);
 printf("%s ", getgrgid(buf.st_gid)->gr_name);
 printf("%ld", buf.st_size);
 tp = localtime(&buf.st_mtime);
 printf("  %d-%d-%d %d:%d ", tp->tm_year+1900, tp->tm_mon+1,
                             tp->tm_mday, tp->tm_hour, tp->tm_min); 

 printf("%s\n", fname);

 return ;
}

void DisplayDir(char fname[])
{
 DIR *mydir;
 struct dirent *myitem;
 char dirname[N];
 struct stat buf;

 if ((mydir = opendir(fname)) == NULL)
 {
  perror("fail to opendir");
  return;
 }
 while ((myitem = readdir(mydir)) != NULL)
 {
  if (myitem->d_name[0] == '.') continue;
  strcpy(dirname, fname);
  strcat(dirname, "/");
  strcat(dirname, myitem->d_name);
  //printf("%s\n", dirname);
  stat(dirname, &buf);
  DisplayFile(buf, myitem->d_name);
 }
 closedir(mydir);
}

int main(int argc, char *argv[])
{
 struct stat buf;
 char fname[N];

 if (argc < 2)
 {
  printf("Usage : %s \n", argv[0]);
  return -1;
 }

 if (stat(argv[1], &buf) < 0)
 {
  perror("fail to stat");
  return -1;
 }

 strcpy(fname, argv[1]);
 if ( S_ISDIR(buf.st_mode) )
 {
    DisplayDir(fname); 
 }
 else
 {
  DisplayFile(buf, fname);
 }

 return 0;
}

阅读(1194) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~