diff --git a/C18/ex2.c b/C18/ex2.c new file mode 100644 index 0000000..1217fe9 --- /dev/null +++ b/C18/ex2.c @@ -0,0 +1,55 @@ +#include +#include + +// Main () - execution starts here +int main (void) +{ + // Declate file stream variables + FILE *fInput, *fOutput; + + // Other variables needed + int i, d, d2; + + // Try and open the text "sample.txt" (in the current directory) file for writing + fOutput = fopen ("numbers.txt", "w"); + + // Check we were able to open the file + if ( fOutput == NULL) + { + printf ("The file could not be opened for writing, exiting\n"); + return -1; + } + + // Use a loop to write values to the newly created file + for ( i = 1 ; i <= 10 ; i++) + { + fprintf (fOutput, "%d %d\n", i, i*i); + } + + // And close the file + fclose (fOutput); + + // Try and open the binary "numbers " (in the current directory) file for reading + + fInput = fopen ("numbers.txt", "r"); + + // Check we were able to open the file + if ( fInput == NULL) + { + printf ("The file could not be opened for reading, exiting\n"); + return -1; + } + + // Read, line by line the 10 values written into variable d + // and then display the contents of d on the screen + for ( i = 1 ; i <= 10 ; i++) + { + fscanf (fInput, "%d %d", &d, &d2); + printf ("Value read from file: %d %d\n", d, d2); + } + + // And close the file + fclose (fInput); + + return (0); // Exit indicating success +} diff --git a/C18/ex3.c b/C18/ex3.c new file mode 100644 index 0000000..2b8dec4 --- /dev/null +++ b/C18/ex3.c @@ -0,0 +1,60 @@ +#include +#include + +// Main () - execution starts here +int main (void) +{ + // Declate file stream variables + FILE *fInput, *fOutput; + // 8x the maximum filename length on most filesystems should be a decent size + char filename[2049] = { 0 }; + + printf("Enter filename: "); + scanf("%2048s", filename); + + // Other variables needed + int i, d, d2; + + // Try and open the text "sample.txt" (in the current directory) file for writing + fOutput = fopen (filename, "w"); + + // Check we were able to open the file + if ( fOutput == NULL) + { + printf ("The file could not be opened for writing, exiting\n"); + return -1; + } + + // Use a loop to write values to the newly created file + for ( i = 1 ; i <= 10 ; i++) + { + fprintf (fOutput, "%d %d\n", i, i*i); + } + + // And close the file + fclose (fOutput); + + // Try and open the binary "numbers " (in the current directory) file for reading + + fInput = fopen ("numbers.txt", "r"); + + // Check we were able to open the file + if ( fInput == NULL) + { + printf ("The file could not be opened for reading, exiting\n"); + return -1; + } + + // Read, line by line the 10 values written into variable d + // and then display the contents of d on the screen + for ( i = 1 ; i <= 10 ; i++) + { + fscanf (fInput, "%d %d", &d, &d2); + printf ("Value read from file: %d %d\n", d, d2); + } + + // And close the file + fclose (fInput); + + return (0); // Exit indicating success +} diff --git a/C18/ex4.c b/C18/ex4.c new file mode 100644 index 0000000..42aaf52 --- /dev/null +++ b/C18/ex4.c @@ -0,0 +1,92 @@ +#include +#include +#include + + +float degrees_to_radians(float degrees) { + return M_PI * degrees / 180.0; +} + + +// Simple function to populate an integer array +void PopulateTheArray ( int Size, float ArrayData[]) +{ + int i; // Variable to use in our loop + + for ( i = 0 ; i < Size ; i++) + { + ArrayData[i] = degrees_to_radians(i); // Treat it like a normal array + } +} +// Simple function do display contents an integer array +void DisplayTheArray ( int Size, float ArrayData[]) +{ + int i; // Variable to use in our loop + + for ( i = 0 ; i < Size ; i++) + { + printf ("%d\t%0.3f\n", i, ArrayData[i]); + } +} + +int SaveArray (char *filename, int Size, float ArrayData[]) +{ + FILE *fp; // File Pointer + int i; // Variable to use in our loop + + fp = fopen(filename, "w"); + // if file open failed, return error to caller + if (fp == NULL) { + return 1; + } + + + for ( i = 0 ; i < Size ; i++) + { + fprintf(fp, "%d\t%0.3f\n", i, ArrayData[i]); + } + + // close file, returns 0 on successful closing (therefore function has executed sucessfully) + return fclose(fp); +} +// Main () - execution starts here +int main (void) +{ + char filename[2049]; + int rc, iSizeForArray; + float *pData; // A pointer to hold the base address of out array + + // Ask for the size of the array and store result + + printf("Please enter the size of the array to dynamically allocate: "); + scanf ("%d", &iSizeForArray); + + // Use calloc with checking + pData = calloc( iSizeForArray, sizeof (float)); + + // Check we got the memory + if ( pData == NULL) + { + printf ("Sorry, I could not allocate the memory, bye!\n"); + return -1; + } + + // Pass the size, iSizeForArray) and the pointer created + // which points to the start of the sucesfully allocated memory + + PopulateTheArray(iSizeForArray, pData); + DisplayTheArray(iSizeForArray, pData); + + // Exercise doesn't ask to use function but kind of pointless without + printf("Enter filename: "); + scanf("%2048s", filename); + rc = SaveArray(filename, iSizeForArray, pData); + if (rc != 0) { + printf("Error saving file\n"); + free(pData); + return rc; + } + + free(pData); // Free up the memory before exiting + return (0); // Exit indicating sucess +} diff --git a/C18/ex5.c b/C18/ex5.c new file mode 100644 index 0000000..a5a8136 --- /dev/null +++ b/C18/ex5.c @@ -0,0 +1,88 @@ +#include +#include +#include + + +float degrees_to_radians(float degrees) { + return M_PI * degrees / 180.0; +} + + +// Simple function to populate an integer array +void PopulateTheArray ( int Size, float ArrayData[]) +{ + int i; // Variable to use in our loop + + for ( i = 0 ; i < Size ; i++) + { + ArrayData[i] = degrees_to_radians(i); // Treat it like a normal array + } +} +// Simple function do display contents an integer array +void DisplayTheArray ( int Size, float ArrayData[]) +{ + int i; // Variable to use in our loop + + for ( i = 0 ; i < Size ; i++) + { + printf ("%d\t%0.3f\n", i, ArrayData[i]); + } +} + +int SaveArrayBinary (char *filename, int Size, float ArrayData[]) +{ + FILE *fp; // File Pointer + int i; // Variable to use in our loop + + fp = fopen(filename, "wb"); + // if file open failed, return error to caller + if (fp == NULL) { + return 1; + } + + // write data + fwrite(ArrayData, sizeof(float), Size, fp); + // close file, returns 0 on successful closing (therefore function has executed sucessfully) + return fclose(fp); +} +// Main () - execution starts here +int main (void) +{ + char filename[2049]; + int rc, iSizeForArray; + float *pData; // A pointer to hold the base address of out array + + // Ask for the size of the array and store result + + printf("Please enter the size of the array to dynamically allocate: "); + scanf ("%d", &iSizeForArray); + + // Use calloc with checking + pData = calloc( iSizeForArray, sizeof (float)); + + // Check we got the memory + if ( pData == NULL) + { + printf ("Sorry, I could not allocate the memory, bye!\n"); + return -1; + } + + // Pass the size, iSizeForArray) and the pointer created + // which points to the start of the sucesfully allocated memory + + PopulateTheArray(iSizeForArray, pData); + DisplayTheArray(iSizeForArray, pData); + + // Exercise doesn't ask to use function but kind of pointless without + printf("Enter filename: "); + scanf("%2048s", filename); + rc = SaveArrayBinary(filename, iSizeForArray, pData); + if (rc != 0) { + printf("Error saving file\n"); + free(pData); + return rc; + } + + free(pData); // Free up the memory before exiting + return (0); // Exit indicating sucess +} diff --git a/C18/ex6.c b/C18/ex6.c new file mode 100644 index 0000000..f315699 --- /dev/null +++ b/C18/ex6.c @@ -0,0 +1,51 @@ +#include +#include + + +int main() { + FILE *fp; // file pointer + float *data; // data pointer (array of floats) + int i, file_size, arr_size; + char filename[2049]; + + printf("Enter filename to read: "); + scanf("%2048s", filename); + + fp = fopen(filename, "rb"); + if (fp == NULL) { + printf("Failed to open file\n"); + return 1; + } + + // get file size (technically isn't the best way because SEEK_END is undefined behaviour: + // + // "Library implementations are allowed to not meaningfully support SEEK_END + // (therefore, code using it has no real standard portability)." + // ~ https://cplusplus.com/reference/cstdio/fseek/ + // + // " Setting the file position indicator to end-of-file, as with fseek(file, 0, SEEK_END), + // has undefined behavior for a binary stream (because of possible trailing null characters) or + // for any stream with state-dependent encoding that does not assuredly end in the initial shift + // state. " + // ~ https://port70.net/~nsz/c/c11/n1570.html#note268 + // + // it is also limited to 2GB files as it returns a signed integer + // better to use something like this probably: + // https://stackoverflow.com/a/238609 + // + // but louise uses fseek so i will too + fseek(fp, 0, SEEK_END); + file_size = ftell(fp); + arr_size = file_size/sizeof(float); + rewind(fp); + + // allocate memory and read data + data = malloc(file_size); + fread(data, sizeof(float), arr_size, fp); + + for (i = 0; i < arr_size; i++) { + printf("%d %0.3f\n", i, data[i]); + } + + free(data); +}