#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#define LINE_LENGTH 11 // 10 characters + newline
// Function to generate a random uppercase character
char generateRandomChar() {
return 'A' + (rand() % 26); // Random letter from A to Z
}
// Function to generate a random line of text
void generateRandomLine(char *line) {
for (int i = 0; i < 10; i++) {
line[i] = generateRandomChar();
}
line[10] = '\n'; // Newline character
line[11] = '\0'; // Null-terminate the string
}
int main() {
char studentID[20];
int numLines, midpoint;
HANDLE hFile;
DWORD bytesWritten, bytesRead;
LARGE_INTEGER liDistanceToMove;
char line[LINE_LENGTH];
char buffer[1024]; // Buffer to read the file content
// Seed the random number generator
srand((unsigned int)time(NULL));
// Input Student ID
printf("Enter your Student ID: ");
scanf("%s", studentID);
// Extract the last two digits of the student ID
int len = strlen(studentID);
numLines = (studentID[len-2] - '0') * 10 + (studentID[len-1] - '0');
// Create or open the file
hFile = CreateFile("student_text.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
printf("Error creating file: %d\n", GetLastError());
return 1;
}
// Write random lines to the file
for (int i = 0; i < numLines; i++) {
generateRandomLine(line);
if (!WriteFile(hFile, line, LINE_LENGTH, &bytesWritten, NULL)) {
printf("Error writing to file: %d\n", GetLastError());
CloseHandle(hFile);
return 1;
}
}
// Get the size of the file
DWORD fileSize = GetFileSize(hFile, NULL);
if (fileSize == INVALID_FILE_SIZE) {
printf("Error getting file size: %d\n", GetLastError());
CloseHandle(hFile);
return 1;
}
// Calculate the midpoint line
midpoint = numLines / 2;
// Move the file pointer to the start of the midpoint line
liDistanceToMove.QuadPart = midpoint * LINE_LENGTH;
liDistanceToMove.LowPart = SetFilePointerEx(hFile, liDistanceToMove, NULL, FILE_BEGIN);
if (liDistanceToMove.LowPart == INVALID_SET_FILE_POINTER) {
printf("Error setting file pointer: %d\n", GetLastError());
CloseHandle(hFile);
return 1;
}
// Read and display the remaining content from the midpoint line
SetFilePointer(hFile, liDistanceToMove.LowPart, NULL, FILE_BEGIN);
while (ReadFile(hFile, buffer, sizeof(buffer) - 1, &bytesRead, NULL) && bytesRead > 0) {
buffer[bytesRead] = '\0'; // Null-terminate the buffer
printf("%s", buffer);
}
// Display the file size
printf("\nFile size: %lu bytes\n", fileSize);
// Close the file
CloseHandle(hFile);
return 0;
}