move exercises and lectures into subfolders
This commit is contained in:
55
Lectures/LC18/EndOfFile/EndOfFile.c
Normal file
55
Lectures/LC18/EndOfFile/EndOfFile.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
|
||||
while (!feof(fInput))
|
||||
{
|
||||
fscanf (fInput, "%d", &d);
|
||||
printf ("Value read from file %d\n",d);
|
||||
}
|
||||
|
||||
// And close the file
|
||||
fclose (fInput);
|
||||
|
||||
return (0); // Exit indicating success
|
||||
}
|
10
Lectures/LC18/EndOfFile/numbers.txt
Normal file
10
Lectures/LC18/EndOfFile/numbers.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
1
|
||||
2
|
||||
3
|
||||
4
|
||||
5
|
||||
6
|
||||
7
|
||||
8
|
||||
9
|
||||
10
|
57
Lectures/LC18/EndOfFileBreak/EndOfFileBreak.c
Normal file
57
Lectures/LC18/EndOfFileBreak/EndOfFileBreak.c
Normal file
@@ -0,0 +1,57 @@
|
||||
#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
|
||||
while (1)
|
||||
{
|
||||
fscanf (fInput, "%d", &d);
|
||||
if ( feof(fInput))
|
||||
break;
|
||||
printf ("Value read from file %d\n",d);
|
||||
}
|
||||
|
||||
// And close the file
|
||||
fclose (fInput);
|
||||
|
||||
return (0); // Exit indicating success
|
||||
}
|
10
Lectures/LC18/EndOfFileBreak/numbers.txt
Normal file
10
Lectures/LC18/EndOfFileBreak/numbers.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
1
|
||||
2
|
||||
3
|
||||
4
|
||||
5
|
||||
6
|
||||
7
|
||||
8
|
||||
9
|
||||
10
|
54
Lectures/LC18/EndOfFileScanf/EndOfFileScanf.c
Normal file
54
Lectures/LC18/EndOfFileScanf/EndOfFileScanf.c
Normal file
@@ -0,0 +1,54 @@
|
||||
#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
|
||||
while (fscanf (fInput, "%d", &d) != EOF)
|
||||
{
|
||||
printf ("Value read from file %d\n",d);
|
||||
}
|
||||
|
||||
// And close the file
|
||||
fclose (fInput);
|
||||
|
||||
return (0); // Exit indicating success
|
||||
}
|
10
Lectures/LC18/EndOfFileScanf/numbers.txt
Normal file
10
Lectures/LC18/EndOfFileScanf/numbers.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
1
|
||||
2
|
||||
3
|
||||
4
|
||||
5
|
||||
6
|
||||
7
|
||||
8
|
||||
9
|
||||
10
|
BIN
Lectures/LC18/FileMove/data.dat
Normal file
BIN
Lectures/LC18/FileMove/data.dat
Normal file
Binary file not shown.
48
Lectures/LC18/FileMove/filemove.c
Normal file
48
Lectures/LC18/FileMove/filemove.c
Normal file
@@ -0,0 +1,48 @@
|
||||
#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 */
|
||||
if (fread (&ValRead, sizeof(int), 1, fptr ) == 1)
|
||||
{
|
||||
/* Display the read value */
|
||||
printf ("Item %d is %d\n",Item,ValRead);
|
||||
}
|
||||
else
|
||||
printf("Failed to read at location %d\n", Item);
|
||||
|
||||
/* And close */
|
||||
fclose (fptr);
|
||||
|
||||
/* All Done */
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
BIN
Lectures/LC18/FileSize/data.dat
Normal file
BIN
Lectures/LC18/FileSize/data.dat
Normal file
Binary file not shown.
38
Lectures/LC18/FileSize/filesize.c
Normal file
38
Lectures/LC18/FileSize/filesize.c
Normal file
@@ -0,0 +1,38 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/* Example 3 - jumping & getting element count */
|
||||
|
||||
int 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 %ld\n",FileBytes);
|
||||
|
||||
/* And close */
|
||||
fclose (fptr);
|
||||
|
||||
/* All Done */
|
||||
return 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user