Task 073: .C File Format
Task 073: .C File Format
The .C file format is a plain text file containing source code written in the C or C++ programming language, adhering to the syntax and semantics defined by the ISO/IEC 9899 standard for C or the ISO/IEC 14882 standard for C++. It does not possess a binary structure or fixed headers, as it is essentially a text-based format encoded typically in ASCII or UTF-8.
The properties of this file format intrinsic to its file system are as follows:
- File name (including the .C extension).
- File size in bytes.
- Creation timestamp.
- Last modification timestamp.
- Last access timestamp.
- File permissions (e.g., read, write, execute for owner, group, and others).
- Owner user identifier.
- Group identifier.
- Inode number (on Unix-like file systems).
- File type indicator (typically denoting a regular file).
Two direct download links for files of format .C are:
- https://raw.githubusercontent.com/gouravthakur39/beginners-C-program-examples/master/hello.c (a simple "Hello World" example in C, using .c as equivalent to .C in case-insensitive contexts).
- https://raw.githubusercontent.com/kokke/tiny-AES-c/master/aes.c (an AES encryption implementation in C, using .c as equivalent to .C in case-insensitive contexts).
The following is an HTML page with embedded JavaScript that enables a user to drag and drop a .C file, subsequently displaying the listed properties on the screen:
Drag and drop a .C file here
- The following is a Python class that can open a .C file, read its metadata properties, write to the file if needed, and print the properties to the console:
import os
import stat
import time
import pwd
import grp
class CFileHandler:
def __init__(self, filepath):
self.filepath = filepath
def open_and_read_properties(self):
if not self.filepath.endswith('.C'):
raise ValueError("File must have .C extension")
stats = os.stat(self.filepath)
return stats
def print_properties(self):
try:
stats = self.open_and_read_properties()
print(f"File name: {os.path.basename(self.filepath)}")
print(f"File size in bytes: {stats.st_size}")
print(f"Creation timestamp: {time.ctime(stats.st_ctime)}")
print(f"Last modification timestamp: {time.ctime(stats.st_mtime)}")
print(f"Last access timestamp: {time.ctime(stats.st_atime)}")
permissions = stat.filemode(stats.st_mode)
print(f"File permissions: {permissions}")
print(f"Owner user identifier: {stats.st_uid} ({pwd.getpwuid(stats.st_uid).pw_name})")
print(f"Group identifier: {stats.st_gid} ({grp.getgrgid(stats.st_gid).gr_name})")
print(f"Inode number: {stats.st_ino}")
print(f"File type indicator: Regular file" if stat.S_ISREG(stats.st_mode) else "Other")
except Exception as e:
print(f"Error: {e}")
def write_to_file(self, content):
with open(self.filepath, 'w') as f:
f.write(content)
# Example usage:
# handler = CFileHandler('example.C')
# handler.print_properties()
# handler.write_to_file('New content')
- The following is a Java class that can open a .C file, read its metadata properties, write to the file if needed, and print the properties to the console:
import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.*;
import java.util.Date;
public class CFileHandler {
private final Path filepath;
public CFileHandler(String filepath) {
this.filepath = Paths.get(filepath);
if (!filepath.endsWith(".C")) {
throw new IllegalArgumentException("File must have .C extension");
}
}
public BasicFileAttributes openAndReadProperties() throws IOException {
return Files.readAttributes(filepath, BasicFileAttributes.class);
}
public void printProperties() {
try {
BasicFileAttributes attrs = openAndReadProperties();
System.out.println("File name: " + filepath.getFileName());
System.out.println("File size in bytes: " + attrs.size());
System.out.println("Creation timestamp: " + new Date(attrs.creationTime().toMillis()));
System.out.println("Last modification timestamp: " + new Date(attrs.lastModifiedTime().toMillis()));
System.out.println("Last access timestamp: " + new Date(attrs.lastAccessTime().toMillis()));
PosixFileAttributes posixAttrs = Files.readAttributes(filepath, PosixFileAttributes.class);
System.out.println("File permissions: " + posixAttrs.permissions());
System.out.println("Owner user identifier: " + posixAttrs.owner());
System.out.println("Group identifier: " + posixAttrs.group());
// Inode not directly available in standard Java API; requires platform-specific extensions
System.out.println("Inode number: Not available in standard Java API");
System.out.println("File type indicator: " + (attrs.isRegularFile() ? "Regular file" : "Other"));
} catch (IOException e) {
System.err.println("Error: " + e.getMessage());
}
}
public void writeToFile(String content) throws IOException {
Files.writeString(filepath, content);
}
// Example usage:
// public static void main(String[] args) throws IOException {
// CFileHandler handler = new CFileHandler("example.C");
// handler.printProperties();
// handler.writeToFile("New content");
// }
}
- The following is a JavaScript class that can open a .C file (using Node.js for file system access), read its metadata properties, write to the file if needed, and print the properties to the console:
const fs = require('fs');
const path = require('path');
class CFileHandler {
constructor(filepath) {
this.filepath = filepath;
if (!this.filepath.endsWith('.C')) {
throw new Error('File must have .C extension');
}
}
openAndReadProperties() {
return fs.statSync(this.filepath);
}
printProperties() {
try {
const stats = this.openAndReadProperties();
console.log(`File name: ${path.basename(this.filepath)}`);
console.log(`File size in bytes: ${stats.size}`);
console.log(`Creation timestamp: ${stats.birthtime}`);
console.log(`Last modification timestamp: ${stats.mtime}`);
console.log(`Last access timestamp: ${stats.atime}`);
console.log(`File permissions: ${stats.mode.toString(8)}`);
console.log(`Owner user identifier: ${stats.uid}`);
console.log(`Group identifier: ${stats.gid}`);
console.log(`Inode number: ${stats.ino}`);
console.log(`File type indicator: ${stats.isFile() ? 'Regular file' : 'Other'}`);
} catch (e) {
console.error(`Error: ${e.message}`);
}
}
writeToFile(content) {
fs.writeFileSync(this.filepath, content);
}
}
// Example usage:
// const handler = new CFileHandler('example.C');
// handler.printProperties();
// handler.writeToFile('New content');
- The following is a C implementation (using a struct to simulate a class) that can open a .C file, read its metadata properties, write to the file if needed, and print the properties to the console:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>
#include <unistd.h>
typedef struct {
char *filepath;
} CFileHandler;
CFileHandler* createCFileHandler(const char *filepath) {
if (strstr(filepath, ".C") == NULL) {
fprintf(stderr, "File must have .C extension\n");
exit(1);
}
CFileHandler *handler = (CFileHandler*)malloc(sizeof(CFileHandler));
handler->filepath = strdup(filepath);
return handler;
}
void destroyCFileHandler(CFileHandler *handler) {
free(handler->filepath);
free(handler);
}
struct stat openAndReadProperties(CFileHandler *handler) {
struct stat stats;
if (stat(handler->filepath, &stats) != 0) {
perror("Error reading file stats");
exit(1);
}
return stats;
}
void printProperties(CFileHandler *handler) {
struct stat stats = openAndReadProperties(handler);
char *filename = strrchr(handler->filepath, '/');
filename = filename ? filename + 1 : handler->filepath;
printf("File name: %s\n", filename);
printf("File size in bytes: %ld\n", (long)stats.st_size);
printf("Creation timestamp: %s", ctime(&stats.st_ctime));
printf("Last modification timestamp: %s", ctime(&stats.st_mtime));
printf("Last access timestamp: %s", ctime(&stats.st_atime));
printf("File permissions: %o\n", stats.st_mode & 0777);
printf("Owner user identifier: %d (%s)\n", stats.st_uid, getpwuid(stats.st_uid)->pw_name);
printf("Group identifier: %d (%s)\n", stats.st_gid, getgrgid(stats.st_gid)->gr_name);
printf("Inode number: %ld\n", (long)stats.st_ino);
printf("File type indicator: %s\n", S_ISREG(stats.st_mode) ? "Regular file" : "Other");
}
void writeToFile(CFileHandler *handler, const char *content) {
FILE *f = fopen(handler->filepath, "w");
if (f == NULL) {
perror("Error writing to file");
exit(1);
}
fprintf(f, "%s", content);
fclose(f);
}
// Example usage:
// int main() {
// CFileHandler *handler = createCFileHandler("example.C");
// printProperties(handler);
// writeToFile(handler, "New content");
// destroyCFileHandler(handler);
// return 0;
// }