CSC209: files, binary, compiling

studied byStudied by 0 people
0.0(0)
Get a hint
Hint

how to write to files?

1 / 44

encourage image

There's no tags or description

Looks like no one added any tags here yet for you.

45 Terms

1

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

New cards
2

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

New cards
3

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)

New cards
4

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

New cards
5

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

New cards
6

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

New cards
7

what is od?

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

New cards
8

what is -A d in relation to od?

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

New cards
9

what is -j 44 in relation to od?

skipping the first 44 bytes because the header takes those up

New cards
10

what is -t d2 in relation to od?

the file consists of 2 bytes

New cards
11

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

New cards
12

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

New cards
13

compiler

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

New cards
14

gcc -S

produces the assembly code eg. helloworld.s

New cards
15

front end

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

New cards
16

middle end

  • semantic analysis

  • compiler optimizes your code

  • looks for ways to make it run faster

New cards
17

back end

  • code generation

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

New cards
18

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.

New cards
19

assembler

  • invoked with command as

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

  • not yet executable

New cards
20

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

New cards
21

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

New cards
22

why are executables not portable across different machines?

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

New cards
23

loader

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

New cards
24

preprocessor

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

New cards
25

how do you check the format of a file?

by using the file command

New cards
26

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)

New cards
27

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

New cards
28

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

New cards
29

what does gcc -c do?

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

New cards
30

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

New cards
31

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)

New cards
32

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

New cards
33

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

New cards
34

dependency

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

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

New cards
35

makefile purpose

keeps track of dependencies

New cards
36

3 main components of makefile

  1. target

  2. recipe

  3. dependencies/prerequisites

New cards
37

target

the file to be constructed

New cards
38

recipe

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

New cards
39

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

New cards
40

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

A tab character, not spaces.

New cards
41

touch command

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

New cards
42

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.

New cards
43

PHONY target

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

New cards
44

pattern rule

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

New cards
45

$ variables

$<: first name in the list

$@: target name

New cards

Explore top notes

note Note
studied byStudied by 635 people
... ago
4.8(5)
note Note
studied byStudied by 11 people
... ago
5.0(1)
note Note
studied byStudied by 1 person
... ago
5.0(1)
note Note
studied byStudied by 11 people
... ago
4.5(2)
note Note
studied byStudied by 21 people
... ago
5.0(1)
note Note
studied byStudied by 93 people
... ago
5.0(1)
note Note
studied byStudied by 7 people
... ago
5.0(1)
note Note
studied byStudied by 13 people
... ago
5.0(1)

Explore top flashcards

flashcards Flashcard (32)
studied byStudied by 47 people
... ago
4.0(1)
flashcards Flashcard (22)
studied byStudied by 6 people
... ago
5.0(1)
flashcards Flashcard (500)
studied byStudied by 24 people
... ago
5.0(1)
flashcards Flashcard (90)
studied byStudied by 2 people
... ago
4.0(1)
flashcards Flashcard (118)
studied byStudied by 9 people
... ago
4.0(1)
flashcards Flashcard (25)
studied byStudied by 2 people
... ago
4.0(1)
flashcards Flashcard (30)
studied byStudied by 10 people
... ago
5.0(1)
flashcards Flashcard (88)
studied byStudied by 559 people
... ago
5.0(1)
robot