文件的时间属性--〉在线等ing

ishe 2003-09-02 09:35:47
我编程序要得到文件的属性,只有时间属性不知道该怎么办。
我是想,记录该属性到一个链表中(链表我已经做好,就差得到这个属性),
还要写到一个文件中。

我已经用
printf( " %-12s %.24s %9ld\n",
gettogether_file.name, ctime( &( gettogether_file.time_write ) ), gettogether_file.size );
printf("\n\n");
已经可以从屏幕输出,其中gettogether_file是我定义的 结构体(如下)
struct _finddata_t gettogether_file;
全部程序如下:(其中把链表信息写入文件部分,还没有实现)


#include <stdio.h>
#include <iostream.h>
#include <io.h>
#include <process.h>
#include <wchar.h>
#include <time.h>
#include <malloc.h>
#include <string.h>

typedef struct F_Information
{
//char FiIn_reserved[2];/*DOS保留*/
char FiIn_attrib; /*查找到的属性*/
long FiIn_ftime; /*文件时间*/
int FiIn_fdate; /*文件日期*/
long FiIn_fsize; /*文件大小*/
char FiIn_name[13]; /*文件名要足够长*/
struct F_Information *next;

/////maybe others to be added////

}F_Information,*FileInformation;

void main()
{ struct _finddata_t gettogether_file;
long hFile;
char fname[10];
int n,i;

int sys;//属性
int rdo;
int hid;
int arc;
FILE *fp;
if((fp=fopen("newfile","a"))==NULL)//向此文件中写入
{
printf("cannot open outfile");
exit(0);
}


F_Information *L,*p,*h;


L=(FileInformation)malloc(sizeof(F_Information));
L->next=NULL; //先建立一个带表头结点的单链表

printf("Enter the the number of the files\n");
scanf("%d",&n);
printf("Enter the files' names\n\n");

for(i=0;i<n;i++)
{
scanf("%s",&fname);
/* Find first file in current directory */
if( (hFile = _findfirst( fname, &gettogether_file )) == -1L )
printf( "Cann't find the No. %d files in current directory!\n",i+1);
else
{
printf( "Listing of files to be got together\n\n" );
printf( "\nRDO HID SYS ARC FILE DATE %25c SIZE\n", ' ' );
printf( "--- --- --- --- ---- ---- %25c ----\n", ' ' );
printf( ( gettogether_file.attrib & _A_RDONLY ) ? " Y " : " N " );//与运算,取出某一位!!
printf( ( gettogether_file.attrib & _A_SYSTEM ) ? " Y " : " N " );
printf( ( gettogether_file.attrib & _A_HIDDEN ) ? " Y " : " N " );
printf( ( gettogether_file.attrib & _A_ARCH ) ? " Y " : " N " );
printf( " %-12s %.24s %9ld\n",
gettogether_file.name, ctime( &( gettogether_file.time_write ) ), gettogether_file.size );
printf("\n\n");
fprintf(" %-12s %.24s %9ld\n",
gettogether_file.name, ctime( &( gettogether_file.time_write ) ), gettogether_file.size);
p=(FileInformation)malloc(sizeof(F_Information)); //生成新结点
/////////////////////////////下面写入信息!!!!!!!
strcpy(p->FiIn_name,gettogether_file.name);
p->FiIn_fsize=gettogether_file.size;

p->FiIn_ftime=gettogether_file.time_write; //???????????????????????????
//strcpy(p->FiIn_ftime,gettogether_file.time);//???????????????????????????????
p->FiIn_attrib=gettogether_file.attrib;
//////may be others to be added///////

p->next=L->next;L->next=p; //插入到表头

_findclose( hFile );
}


}


h=L;//以下验证我的链表
h=h->next;
cout<<endl<<endl<<endl<<"LinkList Information";
for(i=0;i<n;i++)
{

cout<<endl<<endl;
cout<<"name "<<h->FiIn_name<<endl<<endl;
//cout<<"name "<<fname<<endl<<endl;
cout<<"size "<<h->FiIn_fsize<<endl<<endl;
cout<<"time "<<h->FiIn_ftime<<endl<<endl;
cout<<"attrib "<<h->FiIn_attrib<<endl<<endl;
/////////////may be others to be added/////////////////
sys=((h->FiIn_attrib & _A_SYSTEM) ? 1 : 0);
rdo=((h->FiIn_attrib & _A_RDONLY) ? 1 : 0);
hid=((h->FiIn_attrib & _A_HIDDEN) ? 1 : 0);
arc=((h->FiIn_attrib & _A_ARCH) ? 1 : 0);
/*if(sys)*/ cout<<"sys"<<sys<<endl<<endl;
/*if(rdo)*/ cout<<"rdo"<<rdo<<endl<<endl;
/*if(hid)*/ cout<<"hid"<<hid<<endl<<endl;
/*if(arc)*/ cout<<"arc"<<arc<<endl<<endl;



h=h->next;


}


}
...全文
22 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
nustchenhf 2003-09-04
  • 打赏
  • 举报
回复
#include <windows.h>

void main()
{
HANDLE hFile;
FILETIME ftCreate, ftLastAccess, ftLastWrite, ftLocal;
SYSTEMTIME st;

char buf[80];

// Open the file.

hFile = CreateFile( "test.c",
GENERIC_READ,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL );

// Get the file time (in UTC) and convert to local time.

GetFileTime( hFile, &ftCreate, &ftLastAccess, &ftLastWrite );
FileTimeToLocalFileTime( &ftLastWrite, &ftLocal );

// Display the time, as a test.

FileTimeToSystemTime( &ftLocal, &st );
GetTimeFormat( LOCALE_USER_DEFAULT, 0, &st, NULL, buf, sizeof(buf) );
MessageBox( NULL, buf, " FILE TIME", MB_OK );
}
Wolf0403 2003-09-03
  • 打赏
  • 举报
回复
LPFILETIME lpCreationTime = (LPFILETIME)malloc(sizeof(FILETIME));
LPSYSTEMTIME lpSystemTime = (LPSYSTEMTIME)malloc(sizeof(LPSYSTEMTIME));
//...
if (GetFileTime(hFile /*文件句柄*/,lpCreationTime, /*剩下两个 LPFILETIME 指针*/)
FileTimeToSystemTime(lpCreationTime, lpSystemTime);
得到 lpSystemTime 是一个指向 SystemTime 结构的指针。SystemTime 结构如下:
typedef struct _SYSTEMTIME { WORD wYear; WORD wMonth; WORD wDayOfWeek; WORD wDay; WORD wHour; WORD wMinute; WORD wSecond; WORD wMilliseconds;
} SYSTEMTIME, *PSYSTEMTIME;
Members
wYear
Current year. The year must be greater than 1601.

Windows Server 2003, Windows XP: The year cannot be greater than 30827.


wMonth
Current month; January = 1, February = 2, and so on.
wDayOfWeek
Current day of the week; Sunday = 0, Monday = 1, and so on.
wDay
Current day of the month.
wHour
Current hour.
wMinute
Current minute.
wSecond
Current second.
wMilliseconds
Current millisecond.
Remarks
ishe 2003-09-02
  • 打赏
  • 举报
回复
明早上,接着等。
ishe 2003-09-02
  • 打赏
  • 举报
回复
我也有找到这个函数,但msdn里没有例子,参数又一大堆,我不会用
能给个例子,或者讲一下用法吗?
ishe 2003-09-02
  • 打赏
  • 举报
回复
再说一句,程序太长
“//以下验证我的链表”
这条注释下面是我的一个验证,已经完成,可以不用看了。
halibut 2003-09-02
  • 打赏
  • 举报
回复
API:
BOOL GetFileTime(

HANDLE hFile, // identifies the file
LPFILETIME lpCreationTime, // address of creation time
LPFILETIME lpLastAccessTime, // address of last access time
LPFILETIME lpLastWriteTime // address of last write time
);
ishe 2003-09-02
  • 打赏
  • 举报
回复
其中语句:
fprintf(" %-12s %.24s %9ld\n",
gettogether_file.name, ctime( &( gettogether_file.time_write ) ), gettogether_file.size);

我是想,既然能用printf从屏幕输出,那能不能用fprintf输出到文件呢,结果不行,好像是参数的错误,还忘了删掉,就发上来了。

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧