fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <windows.h>
  5.  
  6. #define LINE_LENGTH 11 // 10 characters + newline
  7.  
  8. // Function to generate a random uppercase character
  9. char generateRandomChar() {
  10. return 'A' + (rand() % 26); // Random letter from A to Z
  11. }
  12.  
  13. // Function to generate a random line of text
  14. void generateRandomLine(char *line) {
  15. for (int i = 0; i < 10; i++) {
  16. line[i] = generateRandomChar();
  17. }
  18. line[10] = '\n'; // Newline character
  19. line[11] = '\0'; // Null-terminate the string
  20. }
  21.  
  22. int main() {
  23. char studentID[20];
  24. int numLines, midpoint;
  25. HANDLE hFile;
  26. DWORD bytesWritten, bytesRead;
  27. LARGE_INTEGER liDistanceToMove;
  28. char line[LINE_LENGTH];
  29. char buffer[1024]; // Buffer to read the file content
  30.  
  31. // Seed the random number generator
  32. srand((unsigned int)time(NULL));
  33.  
  34. // Input Student ID
  35. printf("Enter your Student ID: ");
  36. scanf("%s", studentID);
  37.  
  38. // Extract the last two digits of the student ID
  39. int len = strlen(studentID);
  40. numLines = (studentID[len-2] - '0') * 10 + (studentID[len-1] - '0');
  41.  
  42. // Create or open the file
  43. hFile = CreateFile("student_text.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  44. if (hFile == INVALID_HANDLE_VALUE) {
  45. printf("Error creating file: %d\n", GetLastError());
  46. return 1;
  47. }
  48.  
  49. // Write random lines to the file
  50. for (int i = 0; i < numLines; i++) {
  51. generateRandomLine(line);
  52. if (!WriteFile(hFile, line, LINE_LENGTH, &bytesWritten, NULL)) {
  53. printf("Error writing to file: %d\n", GetLastError());
  54. CloseHandle(hFile);
  55. return 1;
  56. }
  57. }
  58.  
  59. // Get the size of the file
  60. DWORD fileSize = GetFileSize(hFile, NULL);
  61. if (fileSize == INVALID_FILE_SIZE) {
  62. printf("Error getting file size: %d\n", GetLastError());
  63. CloseHandle(hFile);
  64. return 1;
  65. }
  66.  
  67. // Calculate the midpoint line
  68. midpoint = numLines / 2;
  69.  
  70. // Move the file pointer to the start of the midpoint line
  71. liDistanceToMove.QuadPart = midpoint * LINE_LENGTH;
  72. liDistanceToMove.LowPart = SetFilePointerEx(hFile, liDistanceToMove, NULL, FILE_BEGIN);
  73. if (liDistanceToMove.LowPart == INVALID_SET_FILE_POINTER) {
  74. printf("Error setting file pointer: %d\n", GetLastError());
  75. CloseHandle(hFile);
  76. return 1;
  77. }
  78.  
  79. // Read and display the remaining content from the midpoint line
  80. SetFilePointer(hFile, liDistanceToMove.LowPart, NULL, FILE_BEGIN);
  81. while (ReadFile(hFile, buffer, sizeof(buffer) - 1, &bytesRead, NULL) && bytesRead > 0) {
  82. buffer[bytesRead] = '\0'; // Null-terminate the buffer
  83. printf("%s", buffer);
  84. }
  85.  
  86. // Display the file size
  87. printf("\nFile size: %lu bytes\n", fileSize);
  88.  
  89. // Close the file
  90. CloseHandle(hFile);
  91.  
  92. return 0;
  93. }
  94.  
Success #stdin #stdout 0.03s 25752KB
stdin
Standard input is empty
stdout
#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;
}