CSC209: files, binary, compiling

0.0(0)
studied byStudied by 8 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/44

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

45 Terms

1
New cards

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

2
New cards

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

3
New cards

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)

4
New cards

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

5
New cards

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

6
New cards

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

7
New cards

what is od?

prints out the values found in the binary file, sort of similar to a file viewer

8
New cards

what is -A d in relation to od?

translates ods output from the default of base 8 to a more convient base 10, address base

9
New cards

what is -j 44 in relation to od?

skipping the first 44 bytes because the header takes those up

10
New cards

what is -t d2 in relation to od?

the file consists of 2 bytes

11
New cards

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

12
New cards

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

13
New cards

compiler

any program that translates code in one language to a different language

14
New cards

gcc -S

produces the assembly code eg. helloworld.s

15
New cards

front end

translates the source code to a largely language-independent intermediate representation

16
New cards

middle end

  • semantic analysis

  • compiler optimizes your code

  • looks for ways to make it run faster

17
New cards

back end

  • code generation

  • translates the intermediate language into the assembly language of the computer that will run the program

18
New cards

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.

19
New cards

assembler

  • invoked with command as

  • object file that contains machine code instructions and data BUT is not human readable

  • not yet executable

20
New cards

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

21
New cards

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

22
New cards

why are executables not portable across different machines?

specific to the type of machine, operating system and system configuration

23
New cards

loader

It loads the executable into memory, readying it for execution by the operating system

24
New cards

preprocessor

A component that prepares source code by adding function prototypes, processing macros, and handling lines starting with a hash (#).

25
New cards

how do you check the format of a file?

by using the file command

26
New cards

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)

27
New cards

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

28
New cards

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

29
New cards

what does gcc -c do?

it compiles the source file into an object file without creating an executable

30
New cards

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

31
New cards

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)

32
New cards

static keyword

two definitions:

  1. if used on a GLOBAL variable

    1. symbol names in C are externally visible, but using static makes the symbol name local

  2. if used on a LOCAL variable

    1. means that the variable should keep its value across function executions

33
New cards

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

34
New cards

dependency

  • the source files rely on the contents of the header files

  • so we have to recompile them when the header file changes

35
New cards

makefile purpose

keeps track of dependencies

36
New cards

3 main components of makefile

  1. target

  2. recipe

  3. dependencies/prerequisites

37
New cards

target

the file to be constructed

38
New cards

recipe

the command (or list of commands) to execute that create the target

39
New cards

dependencies (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

40
New cards

What type of whitespace must precede the recipe in a Makefile?

A tab character, not spaces.

41
New cards

touch command

It updates a file's last modified time, simulating a recent change to trigger make to rebuild it

42
New cards

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.

43
New cards

PHONY target

It declares a target that is not a file, allowing commands to be run without checking for file existence or modification time.

44
New cards

pattern rule

%.o: %.c
    gcc -c $< 

45
New cards

$ variables

$<: first name in the list

$@: target name