CSC Programming for Science Lecture 18: More Data Files
Question of the Day How can you add 5 matches to these 6 and make 9?
Today’s Goal Today’s lecture discusses using & manipulating data stored on disk C commands to open and close files, read, and write contents of opened files After today’s lecture, you should be ready to write programs that reads & writes files Also know a few extra debugging tips
Data File Review To use a file, must first create a file pointer File pointer is variable of type FILE * Created using fopen(“FILENAME”, “ACCESS”); Once complete, call fclose(filePointer); Open file pointer used in I/O functions Read data using fscanf(filePointer,...); Check if at end of file feof(filePointer); To use multiple files, must use multiple file pointers
File Pointer Variables Need to be careful using & declaring FILE *fin; // Good FILE *bob, jim; // Bad! jim lacks * power FILE *ham, *eggs; // Yes, now we are cooking! Variables only describe files Assignment to variable does not affect files Variable’s values meaningful only to C bob = ham; // Both use same file now int i = bob; // Gets warning, i’s value = ?
Opening a File To use a file, must first open the file: FILE* fopen(“FILENAME”, “ACCESS”); ACCESS specifies how file will be used “r” can read from file “w” will overwrite contents of file “a” will append contents of file Cannot read files that do not exist Can write & append them, however
Opening a File (2) fopen returns NULL when unsuccessful NULL is symbolic constant defined in stdio.h Using NULL file pointer will crash program Returns some other value on success Value not meaningful to humans, anyway
Writing to File First assign file pointer to the opened file fprintf(filePointer,...) writes to file First argument is file pointer to write to Rest of arguments identical to printf
Writing To Screen Always been using fprintf fprintf used to print to screen FILE* stdout “auto-magically” exists stdout simplifies printing to screen double a, b;... printf(“%lf %lf”, a, b); is just shorthand for: double a, b;... fprintf(stdout, “%lf %lf”, a, b);
Writing To Screen (2) stdout does not print immediately Will be delayed for performance purposes Can be a problem when debugging FILE* stderr similar to stdout Both created at startup Each normally prints to screen Printing to stderr appears immediately stderr typically used to print error messages
Always Close Your Files Once finished using a file, close it Done using fclose(filePointer) function Must re-open closed file pointer before using Closing file can be important Data may not be recorded until file closed Often need to close & re-open file to switch from reading it to writing it
Stupid Programming Tricks Redirection/piping takes advantage of file flexibility Redirect files to appear as if they were typed in from keyboard Redirect output to be stored directly in files Neat trick that works on (nearly) all OSs
Redirect Input Use “<“ on the command line: a.out < hawaii.txt Program uses scanf, sees input on keyboard Input actually is contents of hawaii.txt Great way to automate testing
Redirect Standard Output Use “>“ on the command line: a.out > miami.txt Program uses printf or fprintf(stdout,…); Nothing appears on screen, but instead saved to miami.txt Contents of miami.txt overwritten, however Saves program results, useful to handle lots of debugging output a.out >> miami.txt appends results
Redirect Standard Error Use “2>“ on the command line: a.out 2> phoenix.txt Program uses fprintf(stderr,…); Only redirects stderr, other output appears as normal Can be combined with “>”, but not to same file a.out > tahiti.txt 2> sanDiego.txt
Your Turn Get into groups and complete daily activity
For Next Lecture Complete week #7 weekly assignment Start programming assignment #2