Initial commit of lecture code
This commit is contained in:
45
LC18/filemove.c
Normal file
45
LC18/filemove.c
Normal 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
38
LC18/filesize.c
Normal 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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user