C: IO cautions
This morning I was trying to do some basic file IO with the new learnt file related functions, here are a few cautions to take care of.
List of functions :
1) FILE * fopen(const char* filename, const char* mode)
2) int fclose(FILE *stream)
3) int fprintf(FILE *stream, const char* format,....)
4) fgets(char *str,int n, FILE *stream)
1. Watch the mode of using your file related functions
Opening with fopen and mode “w” would always overwrite whatever was in the file beforehand. Therefore, in order to maintain the original content, use the “r” mode for reading only, meaning that no writing can be done to the file, or “a” mode for appending data to the original end of the file. The modes would only support one of reading, appending or writing at a time, where as with a “+” sign added to the end, it would have multiple functions.
“r+” mode would support both reading and writing , similar for “w+” and “a+”.
Constraints for each:
“r” modes would only work if the desired file exists
“w” modes would erase whatever was in the file (!!!)
“a” modes would only append to the very end
2. Always remember to close file pointer
An awkward situation arose when I was attempting to manipulate the content of multiple files in one C program.
The intention of the program was to enter lines of text into a file, then reading from that file to printout whatever was entered in the previous step. The issue was the appending/overwriting of text was smooth, but the program could not print anything read from the program.
After asking the tutor in the morning session, it turns out that every time a new file is opened, the previous file needs to be closed to ensure the proper functioning of fgets.
3. The new line @ EOF in fgets
This “problem“ was discovered when I was trying to match the expected output as shown on Edstem(internal studying platform for Usyd), where the printing should have no extra line after encountering an EOF in fgets.
It turned out that this issue only arises when reading from the command line in terminal as standard input, where fgets treats a plain enter key as the invalid condition.
After odd hours of searching, the only valid solution (at least it looks correct) was to move the cursor on the terminal up by one line.
Referencing from this post :
printf("\033[XA"); // Move up X lines;
printf("\033[XB"); // Move down X lines;
printf("\033[XC"); // Move right X column;
printf("\033[XD"); // Move left X column;
printf("\033[2J"); // Clear screen
By moving the cursor up by 1 line, using
The extra line encountered by fgets would disappear and the standard output would begin from the last line with valid strings.
According to this post :
These commands are called terminal control sequences and there are a few extra ones which we can play around with on the terminal.
Clear the screen.
Move the cursor to the upper-left corner of the screen.
Move the cursor to row r, column c. Note that both the rows and columns are indexed starting at 1.
Hide the cursor.
Delete everything from the cursor to the end of the line.
Reset special formatting (such as colour).