Write a program to read a text file and produce some statistical analysis of the file.

Source Code

Brief explanation is provided after the source code.

#include <stdio.h>
#include <time.h>
#include <sys/stat.h>

int main( int argc, char ** argv ) {
	const char * fname = "student_marks";
	struct stat fstat;

	if( stat(fname, &fstat) == 0 ) {
		fprintf(stdout, "Name: %s, UID: %u, GID: %u, Size: %llu\n",
				fname, fstat.st_uid, fstat.st_gid, fstat.st_size);
		// file modification time is in seconds since midnite 1970-01-01 UTC
		const static int bufSize = 128;
		char buf[bufSize];
		struct tm mtime = *gmtime(&fstat.st_mtime);
		strftime(buf, bufSize, "%Y-%m-%d %H:%M:%S %Z", &mtime);
		printf("Last modified: %s\n", buf);
	} else {
		perror("Couldn't stat file");
	return 0;

When you compile and execute the above program it produces the following result on Linux:

Name: student_marks, UID: 1000, GID: 1000, Size: 582
Last modified: 2015-04-29 17:53:18 GMT

Brief Explanation

  • The file used for this program is called "student_marks" and is assigned to the variable fname which is a pointer of type character. Make sure this file is available in the directory where the code is being compiled.
  • A variable fstat of type struct declared.
  • The system call stat takes a filename and returns all of the information in the inode for that file, or -1 if there is an error. That is,
char *name;
struct stat stbuf;
int stat(char *, struct stat *);

stat(name, &stbuf);

fills the structure stbuf with the inode information for the file name. The structure describing the value returned by stat is in <sys/stat.h>, and typically looks like this:

struct stat	/* inode information returned by stat */
	dev_t	st_dev;	/* device of inode */
	ino_t	st_ino;	/* inode number */
	short	st_mode;	/* mode bits */
	short	st_nlink;	/* number of links to file */
	short	st_uid;	/* owners user id */
	short	st_gid;		/* owners group id */
	dev_t	st_rdev;	/* for special files */
	off_t	st_size;	/* file size in characters */
	time_t	st_atime;	/* time last accessed */
	time_t	st_mtime;	/* time last modified */
	time_t	st_ctime;	/* time originally created */
  • Using the fprintf, information about the file are printed to the stdout (filename, owners user id, owners group id, file size in characters).
  • A buffer buf of size 128 is created.
  • struct tm *gmtime(const time_t *tp)
    • gmtime converts the calendar time *tp into Coordinated Universal Time (UTC). It returns NULL if UTC is not available.
  • size_t strftime(char *s, size_t smax, const char *fmt, const struct tm *tp)
    • strftime formats date and time information from *tp into s according to fmt, which is analogous to a printf format. Ordinary characters (including the terminating '\0') are copied into s. Each %c is replaced as described below, using values appropriate for the local environment. No more than smax characters are placed into s. strftime returns the number of characters, excluding the '\0', or zero if more than smax characters were produced.
%a	abbreviated weekday name.
%A	full weekday name.
%b	abbreviated month name.
%B	full month name.
%c	local date and time representation.
%d	day of the month ( 01-31 ).
%H	hour (24-hour clock) (00-23) .
%I 	hour (12-hour clock) (01-12) .
%j	day of the year (001-366) .
%m	month (01-12) .
%M	minute (00-59) .
%p	local equivalent of AM or PM.
  • If there was an error, function perror prints the error to the stderr
  • void perror(const char *s)
    • perror(s) prints s and an implementation-defined error message corresponding to the integer in errno, as if by
      fprintf(stderr, "%s: %s\n", s, "error message");

Add comment

Security code