获得文件属性的函数调用实例

获取文件属性信息,在终端下能够直接利用ls- l命令,在编程时,用到下面三个函数:

#include<sys/types.h>

#include<sys/stat.h>

#include<unistd.h>

int stat(const char *file_name, struct stat *buf);

函数说明:    通过文件名称filename获取文件信息,并保存在buf所指的结构体stat中

返回值:     运行成功则返回0,失败返回-1,错误代码存于errno

错误代码:

ENOENT         參数file_name指定的文件不存在

ENOTDIR        路径中的文件夹存在但却非真正的文件夹

ELOOP          欲打开的文件有过多符号连接问题,上限为16符号连接

EFAULT         參数buf为无效指针,指向无法存在的内存空间

EACCESS        存取文件时被拒绝

ENOMEM         核心内存不足

ENAMETOOLONG   參数file_name的路径名称太长

int fstat(int filedes, struct stat *buf);

int lstat(const char *file_name, struct stat *buf);

struct stat {

dev_t         st_dev;       //文件的设备编号

ino_t         st_ino;       //节点

mode_t        st_mode;      //文件的类型和存取的权限

nlink_t       st_nlink;     //连到该文件的硬连接数目。刚建立的文件值为1

uid_t         st_uid;       //用户ID

gid_t         st_gid;       //组ID

dev_t         st_rdev;      //(设备类型)若此文件为设备文件,则为其设备编号

off_t         st_size;      //文件字节数(文件大小)

unsigned long st_blksize;   //块大小(文件系统的I/O 缓冲区大小)

unsigned long st_blocks;    //块数

time_t        st_atime;     //最后一次訪问时间

time_t        st_mtime;     //最后一次改动时间

time_t        st_ctime;     //最后一次改变时间(指属性)

};

#include <sys/stat.h>

#include <unistd.h>

mian()

{

struct   stat   buf;

stat   (“/etc/passwd”,&buf);

printf(“/etc/passwd   file   size   =   %d   /n”,buf.st_size);

}

编程实例1:

#include <sys/stat.h>
#include <unistd.h>
mian()
{
struct   stat   buf;
stat   (“/etc/passwd”,&buf);
printf(“/etc/passwd   file   size   =   %d   /n”,buf.st_size);
}

编程实例2:

#include<stdio.h>
#include<unistd.h>
#include<time.h>
#include<sys/types.h>
#include<sys/stat.h>
int main(int argc,char *argv[])
{
struct stat buf;
if(argc!=2)
printf("input the parameter error!\n");
if(stat(argv[1],&buf)==-1)
printf("get the stat is faild!\n");

printf("st_dev:%d\n",buf.st_dev);
printf("st_ino:%d\n",buf.st_ino);
printf("st_mode:%d\n",buf.st_mode);
printf("st_nlink:%d\n",buf.st_nlink);
printf("st_uid:%d\n",buf.st_uid);
printf("st_gid:%d\n",buf.st_gid);
printf("st_rdev:%d\n",buf.st_rdev);
printf("st_size:%d\n",buf.st_size);
printf("st_blksize:%d\n",buf.st_blksize);
printf("st_atime:%s\n",ctime(&buf.st_atime));
printf("st_mtime:%s\n",ctime(&buf.st_mtime));
printf("st_ctime:%s\n",ctime(&buf.st_ctime));

return 0;
}

补充:

unsigned long get_file_size(const char *path)
{
    unsigned long filesize = -1;
    FILE *fp;
    fp = fopen(path, "r");
    if(fp == NULL)
        return filesize;
    fseek(fp, 0L, SEEK_END);
    filesize = ftell(fp);
    fclose(fp);
    return filesize;
}
时间: 2024-08-28 19:24:40

获得文件属性的函数调用实例的相关文章

一个简单的php函数调用实例

需求分析: funcs.php (这个文件,我们定义了一个函数) 1 <?php 2 3 //我们一个计算,+ - * / 的代码集合->函数 4 //1. function 是一个关键字 5 //2. jiSuan 函数名(由程序员取名) 6 //3. $num1,$num2,$oper 是函数的参数列表(形参) 7 function jiSuan($num1,$num2,$oper){ 8 //$res 表示计算的结果 9 $res=0; 10 switch($oper){ 11 12 c

脚本进阶,函数调用实例练习

一.练习:脚本:判定192.168.0.200-192.168.0.254之间哪些主机在线,要求: 1.使用函数来实现一台主机的判定过程: 2.在主程序中调用此函数判定指定范围内的所有主机的在线情况, vim ping.sh #!/bin/bash # PING() { for i in {200..254};do if ping -c 1 -w 1 192.168.0.$i &> /dev/null; then echo "192.168.0.$i is up" els

函数调用实例:学生管理系统(前面的优化版),可以弹出窗口。

1 package classwork308; 2 /* 3 * 学生管理系统 4 */ 5 import javax.swing.JOptionPane; 6 7 public class Method_StudentManage { 8 9 /*****管理账号*****/ 10 public static String[] manager={"a","b"}; 11 /*****管理密码*****/ 12 public static int [] passwo

Linux下c++中的atoi、atol、atoll、atof函数调用实例

本文中调用的四个函数如下: atoi函数:将字符串转化为int类型变量 atol函数:将字符串转化为long类型变量 atoll函数:将字符串转化为long long类型变量 atof函数:将字符串转化为double类型变量 这些函数的转化过程,都是将一个字符串的可读部分取到变量中 遇到不可读的部分,则直接终止读取 调用示例: #include <stdio.h> #include <stdlib.h> #define Seperate(); printf("\n====

c#继承中的函数调用实例

using System; namespace Test { public class Base { public void Print() { Console.WriteLine(Operate(8, 4)); } protected virtual int Operate(int x, int y) { return x + y; } } } namespace Test { public class OnceChild : Base { protected override int Ope

(转)函数调用过程探究

转自:http://www.cnblogs.com/bangerlee/archive/2012/05/22/2508772.html 引言 如何定义函数.调用函数,是每个程序员学习编程的入门课.调用函数(caller)向被调函数(callee)传入参数,被调函数返回结果,看似简单的过程,其实CPU和系统内核在背后做了很多工作.下面我们通过反汇编工具,来看函数调用的底层实现. 基础知识 我们先来看几个概念,这有助于理解后面反汇编的输出结果. 栈(stack) 栈,相信大家都十分熟悉,push/p

PHP自学日志:函数的应用(一)

函数定义的语法格式:function([参数]){语句块} /**函数调用实例*输入数字,与运算符,进行相应的运算. *switch语句格式:switch(值){case "值":表达式 baeak;}*/ 1 function result($num1,$num2,$symbol){ 2 switch ($symbol){ 3 case "+": 4 $result=$num1+$num2; 5 break; 6 case "-": 7 $re

关于文件流的简单操作

2015.1.28星期三 小雪变量可以理解为内存gcc -Wall 打开所有警告 指针数组:注意指针数组是以一个NULL指针结束的: c和指针 P105 给定一个指向以NULL结尾的指针列表的指针strings,在列表中的字符串查找一个特定的字符#include <stdio.h>#define TRUE 1#define FALSE 0 int find_char(char **strings,char value){ char *string; while((string = *strin

C++ 中间statickeyword

static顾名思义是一个静态的含义.在此我想谈谈相关的系统statickeyword角色,当然,这主要是对语言的发展C与C++角色,在信息方面的作用等方面,请另找.在评论中肯定有不恰当,请大胆地抛砖.手软,文中的内容引用了不少网上的资料. static从宏观上讲主要有两种使用方法,一.是面向过程设计:二是面向对象设计.前主要是涉及变量与函数的使用方法.后者呢包括前者使用方法的同一时候.还有在类中的使用方法. 一. 面向过程设计中的static(C语言) 在讲面向过程设计中的static使用方法