Engr 0012 (04-1) LecNotes 23-01
Engr 0012 (04-1) LecNotes C++ errors/debugging build/compile compiler does not recognize a statement build/compile process stops error message(s) issued sometimes points to line with actual error sometimes points to line after error
Engr 0012 (04-1) LecNotes C++ errors/debugging run time build/compile has no errors program stops sometimes just stops
Engr 0012 (04-1) LecNotes C++ errors/debugging run time sometimes displays an error message like
Engr 0012 (04-1) LecNotes C++ errors/debugging run time sometimes displays an error message like
Engr 0012 (04-1) LecNotes C++ errors/debugging most common causes of run time errors division by zero using value rather than address in scanf using improper placeholder in scanf allowing array index to exceed amount in use
Engr 0012 (04-1) LecNotes file management creating a data file use your favorite text editor export (text) file from excel export a file from MATLAB use a data logger (experimental)
Engr 0012 (04-1) LecNotes file management opening a file requires a pointer to the file tells program where to find file declaring a FILE pointer // variable declaration FILE *pInfile; // input file pointer FILE *pOutfile; // output file pointer // variable declaration FILE *preadf; // input file pointer FILE *pwritef; // output file pointer file pointer type “value at” or pointer operator lower case “ p ” to remind you that this name is a pointer declaration normally in functions that use files
Engr 0012 (04-1) LecNotes file management opening a file for reading - basic pInfile = fopen("c:\temp\mydata.dat", "r" ); pointer name w/o * fopen function call file name/path read problems “hardwired” need to rewrite/recompile program to open a different file what happens if file not there? no error message!!! just keeps going
Engr 0012 (04-1) LecNotes file management opening a file for reading - better method pInfile = fReadOpen(); pointer name w/o * fReadOpen NOT in any.h library fReadOpen not in any text advantages asks user for file name checks for file existence fReadOpen available in Get12 fReadOpen function call
Engr 0012 (04-1) LecNotes file management opening a file for writing - basic pOutfile = fopen("c:\temp\myresults.out", "w" ); pointer name w/o * write problems “hardwired” need to rewrite/recompile program to open a different file what happens if file already there? no error message!!! just keeps going ==> destroys contents of file file name/path fopen function call
Engr 0012 (04-1) LecNotes file management opening a file for writing - better method pOutfile = fWriteOpen(); pointer name w/o * fWriteOpen NOT in any.h library fWriteOpen not in any text advantages asks user for file name checks for file existence fWriteOpen available in Get12 fWriteOpen function call
Engr 0012 (04-1) LecNotes file management reading from a file numread = fscanf( pInfile, "%lf", &value ); file pointer just created with fReadOpen “file” scanf placeholder for data type address to store value prototype for fscanf (and scanf ) is int fscanf( char[], … ); int scanf( char[], … ); fscanf (and scanf ) can return the number of values actually read in!!! we will exploit this to read files with an unknown number of entries
Engr 0012 (04-1) LecNotes file management writing to a file fprintf( pOutfile, "%7.2lf", value ); “file” fprintf file pointer just created with fReadOpen formatted placeholder for data type value to print only difference between fprintf and printf is the file pointer
Engr 0012 (04-1) LecNotes file management hygiene open a file??? close it when finished!!! fclose( pInfile ); or fclose( pOutfile );