Changes to file structure for VSCode and bug fixes

This commit is contained in:
Louise Brown
2022-11-14 13:51:40 +00:00
parent 7fd908108a
commit caee190d05
41 changed files with 346 additions and 124 deletions

View 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
}

View File

@@ -0,0 +1,10 @@
1
2
3
4
5
6
7
8
9
10