C - Files Flashcards
fK1 stdin, stdout, stderr
global constant pointers,
standard streams for input, output and error output.
K2 FILE *
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
K3 enum
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
};
K4 struct
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”);
K5 chain of responsibility
behavioral design pattern, passing requests along a chain of handlers. handlers either process the request or pass it to the next handler
F1 fopen
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.
F2 fclose
int fclose( FILE *fp );
closes file. return 0 on success or EOF failiure. flushes pending data in the buffer, closes the file, releases memory.
F3 fread
binary input,
size_t fread(void *ptr, size_t size_of_elements, size_t number_of_elements, FILE *a_file);
F4 fwrite
binary output
size_t fwrite(const void *ptr, size_t size_of_elements, size_t number_of_elements, FILE *a_file);
F5 fseek
fseek( fp, 7, SEEK_SET ); //sets file position of fp(stream) to 7(given offset).
F6 ftell
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
F7 fflush
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);
F8 fgets
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);
}
F9 fgetc
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
F10 fputs
https://www.tutorialspoint.com/c_standard_library/c_function_puts.htm
int puts(const char *str)
write string to stdout, excluding NULL character