Question

In: Computer Science

c program //the file's size in bytes unsigned int fileSize(const char* name){     return 0; }...

c program

//the file's size in bytes
unsigned int fileSize(const char* name){
    return 0;
}

//same as fileSize except if the file is a directory then it recursively finds the size of directory and all files it contains
unsigned int fileSizeRec(const char* name){
    return 0;
}

Solutions

Expert Solution

#include <stdio.h>
#include <sys/stat.h>
#include <dirent.h>
#include <string.h>

//the file's size in bytes
unsigned int fileSize(const char* name){
        struct stat st;
        stat(name, &st);
        return st.st_size;
}

//same as fileSize except if the file is a directory then it recursively finds the size of directory and all files it contains
unsigned int fileSizeRec(const char* name){
    struct stat st;
    stat(name, &st);
    
    // check if this is a file
    if(S_ISREG(st.st_mode)) {
        return fileSize(name);
    } else {
        // to calculate total size of the current directory
        unsigned int directory_size = 0;
        
        // Open current directory
        DIR *pDir;
        if ((pDir = opendir(name)) != NULL) {
                struct dirent *pDirent;
                
                // read directory's content one by one.
                while ((pDirent = readdir(pDir)) != NULL){
                
                        // create the relative path to the file inside folders
                        char buffer[PATH_MAX + 1];
                        strcat(strcat(strcpy(buffer, name), "/"), pDirent->d_name);
                        
                        // If current entry in directory, is another directory
                        // and it is not . or .. (which is for parent and current 
                        // directory) pointers..
                        if (pDirent->d_type == DT_DIR) {
                                if (strcmp(pDirent->d_name, ".") != 0 && strcmp(pDirent->d_name, "..") != 0) {
                                    // recursively find the size of internal directory
                                    directory_size += fileSizeRec(buffer);
                                }
                        } else {
                                // current entry is a file, add its size
                                directory_size += fileSize(buffer);
                        }
                }

                closedir(pDir);
        }
        return directory_size;
    }
}


int main(void) {
  int x = fileSizeRec("..");
  printf("%d", x);
  return 0;
}


Related Solutions

Design a C program to print out the unsigned char (integer) values of the 4 bytes...
Design a C program to print out the unsigned char (integer) values of the 4 bytes in sequential order (i.e., the first byte is printed first). Using the code framework provided below, the printing is done by the function void byte_value(int *), in which only pointer variables can be declared and used. #include <stdio.h> ​ void byte_value(int *); ​ int main() { int n = 1; byte_value(&n); printf("Enter an integer: "); if (scanf("%d", &n) == 1) byte_value(&n); return 0; }...
Convert the following C++ statements to an ARM assembly language program: const int size = 10;...
Convert the following C++ statements to an ARM assembly language program: const int size = 10; int x[size] = {8, 2, 9, 6, 7, 0, 1, 3, 5, 4}; int y[size] = {399, -87, 12, 0, 42, -367, 57, 92, -1000, 25}; for i = 0; i < size; i++) if (x([ i ] > y[ i ]) z[ i ] = 0 else z[ i ] = 1;
Translate the following C++ program to Pep/9 assembly language. const char chConst = '+'; char ch1;...
Translate the following C++ program to Pep/9 assembly language. const char chConst = '+'; char ch1; char ch2; int main() { cin.get(ch1); cin.get(ch2);    cout << ch1 << chConst << ch2;    return 0; }
6 C++ Questions: 19. Assuming an int is size 4 bytes, what is the size in...
6 C++ Questions: 19. Assuming an int is size 4 bytes, what is the size in memory of the below array: int cards[10] = {7, 4, 7, 5, 7}; 20. In the array from question 19, what is the value of the below elements: cards[1]; cards[8]; Assume you have an array with 128 integers which is sorted from lowest to highest. 21a. You are searching for a value which is contained in the array using the binary search algorithm. What...
convert this string type   program into interger data type #include "stdafx.h" #include #include unsigned int putIntoHashTable(char...
convert this string type   program into interger data type #include "stdafx.h" #include #include unsigned int putIntoHashTable(char *ptrInputData, unsigned int bufferLength); // function to add to hash table unsigned int getFromHashTable(char *ptrOutputData, unsigned int bufferLength); // function to retrieve data from hash table #define INPUT_BUFFER_SIZE 200 // local buffer used for adding data to the hash table (there is no reason in this assignment to change this value) #define HASH_SIZE 100 // size of hash table to be used (for testing...
*Answer in C program* #include <stdio.h> int main() {      FILE *fp1;      char c;     ...
*Answer in C program* #include <stdio.h> int main() {      FILE *fp1;      char c;      fp1= fopen ("C:\\myfiles\\newfile.txt", "r");      while(1)      {         c = fgetc(fp1);         if(c==EOF)             break;         else             printf("%c", c);      }      fclose(fp1);      return 0; } In the program above which statement is functioning for opening a file Write the syntax for opening a file What mode that being used in the program. Give the example from the program Referring to...
Consider the following program: 1 #define Size 64 int A[Size; Size], B[Size; Size], C[Size; Size]; int...
Consider the following program: 1 #define Size 64 int A[Size; Size], B[Size; Size], C[Size; Size]; int register i, j; for (j = 0; j< Size; j ++) { { for (i = 0; i< Size; i++) C[i; j] = A[i; j] + B[i; j]; } } Assume that the program is running on a system using demand paging and the page size is 1 Kilobyte. Each integer is 4 bytes long. It is clear that each array requires a 16-page...
Implement stack in C Struct: struct patients{ int id; int severity; char *firstName; char *lastName; char...
Implement stack in C Struct: struct patients{ int id; int severity; char *firstName; char *lastName; char *state; int time_spent; }; Given Array: struct patients* patientsArray[4] = {&p1, &p2, &p3, &p4}; Create two functions one for pushing this array to the stack and one for popping the variables such as p1 p2 p3 p4 by its ID
Debug this code getting segmentation faults. //array_utils.h int contains(const int *arr,int size , int k); int...
Debug this code getting segmentation faults. //array_utils.h int contains(const int *arr,int size , int k); int containsWithin(const int *arr,int size , int k,int i , int j); int *paddedCopy(const int *arr,int oleSize , int newSize); void reverse(int *arr,int size); int *reverseCopy(const int *arr,int size); //array_utils.c #include"array_utils.h" #include<stdlib.h> int contains(const int *arr,int size , int k){    int i=0;    for(i=0;i<size;i++){        if(arr[i] == k)return 1;    }    return 0; } int containsWithin(const int *arr,int size , int k,int...
#include <iostream> using namespace std; const int DECLARED_SIZE = 10; void fillArray(int a[], int size, int&...
#include <iostream> using namespace std; const int DECLARED_SIZE = 10; void fillArray(int a[], int size, int& numberUsed) { cout << "Enter up to " << size << " nonnegative whole numbers.\n" << "Mark the end of the list with a negative number.\n"; int next, index = 0; cin >> next; while ((next >= 0) && (index < size)) { a[index] = next; index++; cin >> next; } numberUsed = index; } int search(const int a[], int numberUsed, int target) {...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT