CS 36 Unit 3 Study GUide Flashcards
What is the format for fopen and what does it do
The format for fopen is fopen(“filename.exe”, “w”); and it basically creates a new file if it doesn’t exist but if it does exist then it is opened through the write format, meaning you can add stuff inside of it
What do you set equal to fopen and whatnot?
You do FILE *fPtr = fopen(“filename.exe”, “w”); and then if it doesn’t open then it is equal to NULL
How to open a file in binary format? How to add stuff into the binary file?
You do fopen(“filename.exe”, “wb”); Binary file is best when you have a struct of data and you want to implement it in the file. Then, you would fill a struct with blank info, (if typedef you would do name and then variable, if not struct name and then variable). You would set the variable equal to what is in the structure’s variables.
From there, you would do fwrite(&variableName, sizeof(struct name), 1, pointer);
fclose
Whenever you open a file, you would have to close it through this. You would do fclose(ptrName);
What is the fopen format to update already existing information in a binary file?
instead of fopen(“filename.exe”, “wb”);
You would do fopen(“filename.exe”, “rb+”);
And then you would do fseek(pointerFile, sizeof(struct name) * (variablestruct.dataFromStruct - 1), SEEK_SET)(this places the pointer at the beginning of the file)
What does fprintf do and how does that differ from fwrite?
fprintf is like fwrite but it is used in a regular file. Basically, you would have to do fprintf(fileName, “%datatype %datatype\n”, variable name, variable name2); This would write this text into the file
What does fscanf do?
fscanf has three different parameters. fscanf(pointerName, “%datatype%datatype%datatype, &variable, &variable, variable(if this is character variable). This function can be used if the file is opened in read, and changes current variable values.
What are the three different file access modes?
wb, rb+ and wb+. wb is used to open/create files, as well as writing initial information, rb+ is used to change information in the file, and wb+ is used to read the file and print information
feof(review this)
Looks until the rest of the file for NULL
rewind
For normal files, this makes the cursor go back to the beginning, unlike fseek(pointer name, (function name.variable - 1) * (sizeof(struct name), SEEK_SET (start of the file, where the cursor starts in the file)
What are the three different reasons to use interface and implementation header files(create our own?)
Solve specific tasks, share code with others, and hide implementation (reusability, portability, security)
What are the different include guards
ifndef STACK_H
#define STACK_H
#endif
it checks if it is not defined, then it defines if it isn’t, and then it ends the thing