Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 03-15-2002, 03:30 AM   #1
Member (4 bit)
 
Join Date: Mar 2002
Location: London
Posts: 9
Boom c programming

I am a very bad c programmer and can not get the following code to work. Please could someone either assist me on how to make the code work or just do the code an email me at comfort_17@yahoo.com. THe code should check the date of a file and output it.

the structure returned include:

st_atime

Time of last file access.

st_ctime

Time of creation of file.

st_dev

If a device, handle; otherwise 0.

st_mode

Bit mask for file-mode information. The _S_IFCHR bit is set if handle refers to a device. The _S_IFREG bit is set if handle refers to an ordinary file. The read/write bits are set according to the file’s permission mode. _S_IFCHR and other constants are defined in SYS\STAT.H.

st_mtime

Time of last modification of file.

st_nlink

Always 1 on non-NTFS file systems.

st_rdev

If a device, handle; otherwise 0.

st_size

Size of the file in bytes.


/* FSTAT.C: This program uses _fstat to report
* the size of a file named F_STAT.OUT.
*/


#include "io.h"
#include "fcntl.h "
#include "time.h "
#include "sys/types.h "
#include "sys/stat.h "
#include "stdio.h "
#include "stdlib.h "
#include "string.h"

void main( void )
{
struct _stat buf;
int fh, result;
char buffer[] = "A line to output";

if( (fh = _open( "f_stat.out", _O_CREAT | _O_WRONLY |
_O_TRUNC )) == -1 )
_write( fh, buffer, strlen( buffer ) );

/* Get data associated with "fh": */
result = _fstat( fh, &buf );

/* Check if statistics are valid: */
if( result != 0 )
printf( "Bad file handle\n" );
else
{
printf( "File size : %ld\n", buf.st_size );

printf( "Time modified : %s", ctime( &buf.st_ctime ) );
}
_close( fh );
}


Output

File size : 0
Time modified : Tue Mar 21 15:23:08 1995
comfort_17 is offline   Reply With Quote
Old 03-15-2002, 03:38 AM   #2
Member (4 bit)
 
Join Date: Mar 2002
Location: London
Posts: 9
Sort of got the program working but......

COuld someone please tell me how i can alter the code to type in an address of a file and it then outputs the details i require.
comfort_17 is offline   Reply With Quote
Old 03-15-2002, 05:15 AM   #3
Member (5 bit)
 
Join Date: Mar 2001
Posts: 17
Please can you expand on exactly what you mean by "Entering the Address of the File"
DrPC is offline   Reply With Quote
Old 03-15-2002, 05:19 AM   #4
Member (4 bit)
 
Join Date: Mar 2002
Location: London
Posts: 9
alterations

I have now found a way of typing in the path of the file but it overwrites the
original file deleting all its contents. Do you know a way of displaying

TIme created:
Time Modified:

And comparing the two outputs?
comfort_17 is offline   Reply With Quote
Old 03-15-2002, 08:53 AM   #5
Member (5 bit)
 
Join Date: Mar 2001
Posts: 17
Try removing the "| _O_TRUNC" parameter. This parameter destroys the content of the file first.

To output the two times:

printf( "Time created : %s", ctime( &buf.st_ctime ) );
printf( "Time modified : %s", ctime( &buf.st_mtime ) );


To compare the two times you need to use a function called "difftime"
For example,

double time_difference = difftime( &buf.st_mtime, &buf.st_ctime );

if (time_difference > 0)
{
//file has been modified
}
else
{
//file hasn't been modified
}
DrPC is offline   Reply With Quote
Old 03-15-2002, 09:42 AM   #6
Member (4 bit)
 
Join Date: Mar 2002
Location: London
Posts: 9
WRONLY?

THank you very much for your time an dpatience. How do i alter WRONLY to read only?
comfort_17 is offline   Reply With Quote
Old 03-15-2002, 10:19 AM   #7
Member (5 bit)
 
Join Date: Mar 2001
Posts: 17
Change _O_WRONLY for _O_RDONLY
DrPC is offline   Reply With Quote
Old 03-20-2002, 07:10 AM   #8
Member (4 bit)
 
Join Date: Mar 2002
Location: London
Posts: 9
file name?

any chance you know how to set up a scanf that reads in the file typed in by th euser and then displays the details on the file. Then display teh details with the filename on the output

eg.
File ----> demo.txt


FileName= demo.txt
Size= ???
Dates= ????
comfort_17 is offline   Reply With Quote
Old 03-25-2002, 09:52 AM   #9
Member (5 bit)
 
Join Date: Mar 2001
Posts: 17
Give the attached file a try. I think it does what you are asking.
Attached Files
File Type: txt file info.txt (1.3 KB, 68 views)
DrPC is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 07:53 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2