move exercises and lectures into subfolders
This commit is contained in:
64
Exercises/C18/BinaryFile/binary_file_example.c
Normal file
64
Exercises/C18/BinaryFile/binary_file_example.c
Normal file
@@ -0,0 +1,64 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// Main () - execution starts here
|
||||
int main (void)
|
||||
{
|
||||
|
||||
// Declate file stream variables
|
||||
FILE *fInput, *fOutput;
|
||||
|
||||
// Other variables needed
|
||||
int i;
|
||||
int SampleArray[10] = {1,2,3,4,5,6,7,8,9,10};
|
||||
float f = 23.4;
|
||||
|
||||
// Try and open the binary "numbers.dat" (in the current directory) file for writing
|
||||
fOutput = fopen ("numbers.dat", "wb");
|
||||
|
||||
// Check we were able to open the file
|
||||
if ( fOutput == NULL)
|
||||
{
|
||||
printf ("\nThe file could not be opened for writing, exiting");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Write out a single float to the binary file
|
||||
fwrite ( &f, sizeof(float), 1 , fOutput);
|
||||
|
||||
// Now the entire array on one go
|
||||
fwrite ( SampleArray, sizeof(int), 10 , fOutput);
|
||||
|
||||
// And close the file
|
||||
fclose (fOutput);
|
||||
|
||||
|
||||
// Try and open the binary "numbers.dat" (in the current directory) file for reading
|
||||
fInput = fopen ("numbers.dat", "rb");
|
||||
|
||||
// Check we were able to open the file
|
||||
if ( fInput== NULL)
|
||||
{
|
||||
printf ("\nthe file could not be opened for reading, exiting");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Read a single float from the binary file into f
|
||||
fread ( &f, sizeof(float), 1 , fOutput);
|
||||
|
||||
// Now read the entire array on one go
|
||||
fread ( SampleArray, sizeof(int), 10 , fOutput);
|
||||
|
||||
// Display the values read from the file on the screen
|
||||
printf ("The value read into f is %f\n", f);;
|
||||
for ( i = 0 ; i < 10 ; i++)
|
||||
{
|
||||
printf ("Item %d of the array contains %d\n",i, SampleArray[i]);
|
||||
}
|
||||
|
||||
// And close the file
|
||||
fclose (fInput);
|
||||
|
||||
|
||||
return (0); // Exit indicating sucess
|
||||
}
|
BIN
Exercises/C18/BinaryFile/numbers.dat
Normal file
BIN
Exercises/C18/BinaryFile/numbers.dat
Normal file
Binary file not shown.
52
Exercises/C18/FileOpen/file_open_example.c
Normal file
52
Exercises/C18/FileOpen/file_open_example.c
Normal file
@@ -0,0 +1,52 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// Main () - execution starts here
|
||||
int main (void)
|
||||
{
|
||||
|
||||
// Declate file stream variables
|
||||
FILE *fInput, *fOutput, *fRecords;
|
||||
|
||||
|
||||
// Try and open the text "sample.txt" (in the current directory) file for reading
|
||||
fInput = fopen ("sample.txt", "r");
|
||||
|
||||
// Check we were able to open the file
|
||||
if ( fInput == NULL)
|
||||
{
|
||||
printf ("\nthe file could not be opened");
|
||||
return -1; // Exit as unsuccessful
|
||||
}
|
||||
|
||||
fclose (fInput); // Close the file
|
||||
|
||||
// Try and open the binary "samples.dat" (in the current directory) file for writing
|
||||
// if a file of this name already exists it will be deleted
|
||||
fOutput = fopen ("samples.dat", "wb");
|
||||
|
||||
// Check we were able to open the file
|
||||
if ( fOutput == NULL)
|
||||
{
|
||||
printf ("\nthe file could not be opened");
|
||||
return -1; // Exit as unsuccessful
|
||||
}
|
||||
|
||||
fclose (fOutput); // Close the file
|
||||
|
||||
// Open, for appending, the text file "records.txt". If the file does not already
|
||||
// exists, a new one of this name will be created (as if "w") were the mocde
|
||||
fRecords = fopen ("records.txt", "a");
|
||||
|
||||
// Check we were able to open the file
|
||||
if ( fRecords == NULL)
|
||||
{
|
||||
printf ("\nthe file could not be opened");
|
||||
return -1; // Exit as unsuccessful
|
||||
}
|
||||
|
||||
fclose (fRecords);
|
||||
|
||||
|
||||
return (0); // Exit indicating sucess
|
||||
}
|
10
Exercises/C18/TextFile/numbers.txt
Normal file
10
Exercises/C18/TextFile/numbers.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
1
|
||||
2
|
||||
3
|
||||
4
|
||||
5
|
||||
6
|
||||
7
|
||||
8
|
||||
9
|
||||
10
|
55
Exercises/C18/TextFile/text_file_example.c
Normal file
55
Exercises/C18/TextFile/text_file_example.c
Normal file
@@ -0,0 +1,55 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// Main () - execution starts here
|
||||
int main (void)
|
||||
{
|
||||
// Declate file stream variables
|
||||
FILE *fInput, *fOutput;
|
||||
|
||||
// Other variables needed
|
||||
int i,d;
|
||||
|
||||
// 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 ("\nthe file could not be opened for writing, exiting");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Use a loop to write values to the newly created file
|
||||
for ( i = 1 ; i <= 10 ; i++)
|
||||
{
|
||||
fprintf (fOutput, "%d\n", 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 ("\nthe file could not be opened for reading, exiting");
|
||||
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);
|
||||
printf ("Value read from file %d\n",d);
|
||||
}
|
||||
|
||||
// And close the file
|
||||
fclose (fInput);
|
||||
|
||||
return (0); // Exit indicating success
|
||||
}
|
55
Exercises/C18/ex2.c
Normal file
55
Exercises/C18/ex2.c
Normal file
@@ -0,0 +1,55 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// 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
|
||||
}
|
60
Exercises/C18/ex3.c
Normal file
60
Exercises/C18/ex3.c
Normal file
@@ -0,0 +1,60 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// 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
|
||||
}
|
92
Exercises/C18/ex4.c
Normal file
92
Exercises/C18/ex4.c
Normal file
@@ -0,0 +1,92 @@
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
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
|
||||
}
|
88
Exercises/C18/ex5.c
Normal file
88
Exercises/C18/ex5.c
Normal file
@@ -0,0 +1,88 @@
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
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
|
||||
}
|
51
Exercises/C18/ex6.c
Normal file
51
Exercises/C18/ex6.c
Normal file
@@ -0,0 +1,51 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
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);
|
||||
}
|
Reference in New Issue
Block a user