C - Files Flashcards

1
Q

fK1 stdin, stdout, stderr

A

global constant pointers,
standard streams for input, output and error output.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

K2 FILE *

A

https://www.tutorialspoint.com/cprogramming/c_file_io.htm

FILE *fp; //declare file
object containing information to control a stream.

can read, write to, appand to files. or close them.

fclose returns 0 on success

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

K3 enum

A

https://www.w3schools.com/c/c_enums.php
special type that represents a group of constants.
//create enum
enum Level {
LOW,
MEDIUM,
HIGH
};

//create variable of enum
enum Level myVar;

//assigned value must of of LOW, MEDIUM, or HIGH. match enum items
enum Level myVar = MEDIUM;

by default, first item assigned value 0. second 1. third 2.

enum Level {
LOW = 25,
MEDIUM = 50,
HIGH = 75
};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

K4 struct

A

https://www.w3schools.com/c/c_structs.php

way to group related variables into one place. can contain different data types (int, float, char).

can’t just assign strings to elements in structures normally. use strcpy() instead.

strcpy(s1.myString, “some text”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

K5 chain of responsibility

A

behavioral design pattern, passing requests along a chain of handlers. handlers either process the request or pass it to the next handler

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

F1 fopen

A

FILE *fopen( const char * “filename.txt”, “w+” );
r - open text file
w - open text file to write on. create new file if doesn’t exist. writes from beginning of file.
a - open and append.
r+ open for read and write
w+ open for read and write, truncates file to zero length.
a+ open for read and write, write from beginning but can only append.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

F2 fclose

A

int fclose( FILE *fp );

closes file. return 0 on success or EOF failiure. flushes pending data in the buffer, closes the file, releases memory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

F3 fread

A

binary input,

size_t fread(void *ptr, size_t size_of_elements, size_t number_of_elements, FILE *a_file);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

F4 fwrite

A

binary output

size_t fwrite(const void *ptr, size_t size_of_elements, size_t number_of_elements, FILE *a_file);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

F5 fseek

A

fseek( fp, 7, SEEK_SET ); //sets file position of fp(stream) to 7(given offset).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

F6 ftell

A

returns the current file position of the given stream.

fp = fopen(“file.txt”, “r”);
fseek(fp, 0, SEEK_END);
len = ftell(fp);

printf(“Total size of file.txt = %d bytes\n”, len); //Total size of file.txt = 26 bytes

https://www.tutorialspoint.com/c_standard_library/c_function_ftell.htm

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

F7 fflush

A

https://www.tutorialspoint.com/c_standard_library/c_function_fflush.htm

flushes the output buffer of a stream

fprintf(stdout, “This output will go into buff\n”);
fflush( stdout );

fprintf(stdout, “and this will appear when programm\n”);
sleep(5);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

F8 fgets

A

https://www.tutorialspoint.com/c_standard_library/c_function_fgets.htm
char *fgets(char *str, int n, FILE *stream)

reads a line from the specified stream and stores it into the string pointed to by str

if( fgets (str, 60, fp)!=NULL ) {
/* writing content to stdout */
puts(str);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

F9 fgetc

A

https://www.tutorialspoint.com/c_standard_library/c_function_fgetc.htm
gets the next (unsigned char) character from the specified stream and advances the stream’s indicator.

get character and advance pointer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

F10 fputs

A

https://www.tutorialspoint.com/c_standard_library/c_function_puts.htm
int puts(const char *str)

write string to stdout, excluding NULL character

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

F11 fputc

A

https://www.tutorialspoint.com/c_standard_library/c_function_fputc.htm
int fputc(int char, FILE *stream)
writes a character (char) to the specified stream and advances the position indicator for the stream

FILE *fp;
int ch;
fp = fopen(“file.txt”, “w+”);
for( ch = 33 ; ch <= 100; ch++ ) {
fputc(ch, fp);
}

put character and advance pointer

17
Q

Q1 what is a file in linux?

A

container used for storing data.

18
Q

Q2 what is the difference between fopen() and open()?
what is the difference between what they return?

A

open(): low-level cll
fopen(): calls the open() function in the background and returns a Filepointer directly.