fork download
  1. #include <stdio.h>
  2. #include <string.h> // Included for strcpy function
  3.  
  4. #define MAX_FILES 100
  5. #define BLOCK_SIZE 1024 // Size of each block in bytes
  6.  
  7. // Structure to represent a file
  8. typedef struct {
  9. char name[50];
  10. int size; // Size of the file in blocks
  11. } File;
  12.  
  13. // Array to store files
  14. File files[MAX_FILES];
  15.  
  16. // Function to allocate space for a new file sequentially
  17. int allocateSequential(char name[], int fileSize) {
  18. int i = 0;
  19. int totalSize = 0;
  20.  
  21. // Calculate the total size of existing files
  22. for (i = 0; i < MAX_FILES && files[i].size != 0; i++) {
  23. totalSize += files[i].size;
  24. }
  25.  
  26. // Check if there's enough space for the new file
  27. if (totalSize + fileSize > MAX_FILES * BLOCK_SIZE) {
  28. printf("Not enough space for file allocation.\n");
  29. return -1;
  30. }
  31.  
  32. // Find the first available position for the new file
  33. for (i = 0; i < MAX_FILES && files[i].size != 0; i++);
  34.  
  35. // Store the file information
  36. strcpy(files[i].name, name);
  37. files[i].size = fileSize;
  38.  
  39. printf("File %s allocated successfully.\n", name);
  40. return 0;
  41. }
  42.  
  43. int main() {
  44. // Example usage
  45. allocateSequential("file1.txt", 2);
  46. allocateSequential("file2.txt", 3);
  47. allocateSequential("file3.txt", 1);
  48.  
  49. return 0;
  50. }
  51.  
Success #stdin #stdout 0s 5304KB
stdin
Standard input is empty
stdout
File file1.txt allocated successfully.
File file2.txt allocated successfully.
File file3.txt allocated successfully.