Initial commit of lecture code

This commit is contained in:
Louise Brown
2022-04-07 16:58:01 +01:00
commit 5b205092a7
60 changed files with 2413 additions and 0 deletions

45
LC18/filemove.c Normal file
View File

@@ -0,0 +1,45 @@
#include <stdio.h>
#include <stdlib.h>
/* Example 3 - jumping & getting element count */
int main(void)
{
int ValRead;
int Item;
int Locn;
FILE *fptr;
/* Open the file from example 1 */
fptr = fopen ("data.dat","rb");
if ( fptr == NULL )
{
printf ("\nError opening input file - aborting ");
exit (0);
}
/* Ask the user which value to jump to */
printf ("\nWhich value do you wish to view ? ");
scanf ("%d",&Item);
/* Jump to this item */
/* notice we move in steps of item size */
Locn = Item * sizeof(int);
fseek (fptr, Locn, SEEK_SET);
/* And read a single integer in */
fread (&ValRead, sizeof(int), 1, fptr );
/* Display the read value */
printf ("Item %d is %d\n",Item,ValRead);
/* And close */
fclose (fptr);
/* All Done */
return 0;
}

38
LC18/filesize.c Normal file
View File

@@ -0,0 +1,38 @@
#include <stdio.h>
#include <stdlib.h>
/* Example 3 - jumping & getting element count */
main()
{
long FileBytes;
FILE *fptr;
/* Open the file from example 1 */
fptr = fopen ("data.dat","rb");
if ( fptr == NULL )
{
printf ("\nError opening input file - aborting ");
exit (0);
}
/* Move to the end of file */
fseek (fptr, 0, SEEK_END);
/* Get the byte size */
FileBytes = ftell(fptr);
/* Convert to size based on fact all int's */
FileBytes = FileBytes / sizeof(int);
/* Display the read value */
printf ("No. of items in file is %d\n",FileBytes);
/* And close */
fclose (fptr);
/* All Done */
return 0;
}