Added book examples
This commit is contained in:
64
C18/binary_file_example.c
Normal file
64
C18/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
|
||||
}
|
52
C18/file_open_example.c
Normal file
52
C18/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
|
||||
}
|
55
C18/text_file_example.c
Normal file
55
C18/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
|
||||
}
|
Reference in New Issue
Block a user