Looks like no one added any tags here yet for you.
how to write to files?
first need to open file for “w” or “a”, then use fprintf(file, text)
, then close the file and always error check
are we actually writing to a file?
no we are writing to a location in memory that is controlled by the OS that is periodically written to the file on disk
flush the screen
if you absolutely need to make sure that key modifications have been made to a stream youre writing, then you can flush the screen but ususally the OS will handle
int fflush(FILE *stream)
what is a binary file?
it is a format for computers, not humans! it is smaller and more versatile than text. eg. image and music files are binary files
how to write to binary file?
size_t fwrite(const void ptr, size_t size, size_t nmemb, FILE stream);
first is a pointer to the data that we want to write to the file
second is size of each element that were writing to file
third is the # of elements that we are writing
last is the file pointer which must refer to a stream open in binary mode
it returns the # of elements successfully written into the file
can use this info for error checking
how to read from binary file?
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
takes a NON-CONSTANT pointer
returns the # of items successfully read
it equals 0 when error occurs or when the end of the file is reached
it is important that the order in which we read values is the order that we wrote them to the file
what is od?
prints out the values found in the binary file, sort of similar to a file viewer
what is -A d
in relation to od?
translates ods output from the default of base 8 to a more convient base 10
what is -j 44
in relation to od?
skipping the first 44 bytes because the header takes those up
what is -t d2
in relation to od?
the file consists of 2 bytes
what is fseek?
int fseek(FILE *stream, long int offset, int whence);
first is stream whos position wed like to change
second is byte count indicating how much the file position should change
third determines how the second parameter is interpreted
this is used because sometimes we need to jump around within a file
what are the 3 constants for fseek?
constant | meaning |
SEEK_SET | from the beginning of the file |
SEEK_CUR | from current file position |
SEEK_END | from end of file |
compiler
any program that translates code in one language to a different language
gcc -S
produces the assembly code eg. helloworld.s
front end
translates the source code to a largely language-independent intermediate representation
middle end
semantic analysis
compiler optimizes your code
looks for ways to make it run faster
back end
code generation
translates the intermediate language into the assembly language of the computer that will run the program
What does gcc
do in the compilation process of a C program?
It compiles source code into an executable file, often named a.out
by default.
assembler
invoked with command as
object file that contains machine code instructions and data BUT is not human readable
not yet executable
linker
invoked with the command ld
final stage of the compilation process
purpose is that it combines object files and other resources into a single executable file
final executable
package that contains:
all of the instructions in the program in addition to a data section
items like constant strings
links to dynamic libraries
why are executables not portable across different machines?
specific to the type of machine, operating system and system configuration
loader
It loads the executable into memory, readying it for execution by the operating system
preprocessor
A component that prepares source code by adding function prototypes, processing macros, and handling lines starting with a hash (#
).
how do you check the format of a file?
by using the file
command
separate compilation
process of compiling multiple files separately and then linking them together to create an executable
useful because you only need to recompile modified files so you save time
issues may arise if there are linking errors and inconsistencies (eg. with variable types)
header file
is an example of an interface
it explains WHAT functions do and what TYPES they require
without defining HOW they are implemented
ensure consistent function declarations and type definitions across multiple source files
can detect any mismatching in declared variable types
how to add a header file to a c file?
by using #include "file.h"
and the quotes tell the compiler to use a header file from the current directory
what does gcc -c
do?
it compiles the source file into an object file without creating an executable
duplication
if we compile file separately then try to link them it could lead to this issue
this is because each object file has the variable created in it
need to separate the declaration of the variable name + type from the definition that creates it
extern
externally defined
if we need to use the variables in multiple places we can define them in the header file
then we remove assignment of values from header and only have that where necessary in the c files
then the variables ONLY exist in 1 space
but the header file DOES NOT create space for the variables (just indicates they exist)
static keyword
two definitions:
if used on a GLOBAL variable
symbol names in C are externally visible, but using static makes the symbol name local
if used on a LOCAL variable
means that the variable should keep its value across function executions
guard condition
eg. #ifndef SORTS_H
means if not defined sorts h
so the first time the header is run, then the condition is false so SORTS_H is defined
then if its ran again, it is not redefined
need to end the if statement with #endif
dependency
the source files rely on the contents of the header files
so we have to recompile them when the header file changes
makefile purpose
keeps track of dependencies
3 main components of makefile
target
recipe
dependencies/prerequisites
target
the file to be constructed
recipe
the command (or list of commands) to execute that create the target
dependecies (makefile)
if present: the recipe is executed if one or more of the dependencies are newer than the target
if not present: the actions are ONLY executed if the target doesnt exist
What type of whitespace must precede the recipe in a Makefile?
A tab character, not spaces.
touch command
It updates a file's last modified time, simulating a recent change to trigger make
to rebuild it
How does make
determine if it needs to rebuild a file?
By checking if any of its dependencies have been modified since it was last built.
PHONY
target
It declares a target that is not a file, allowing commands to be run without checking for file existence or modification time.
pattern rule
%.o: %.c
gcc -c $<
$ variables
$<: first name in the list
$@: target name