Engr 0012 04 1 Lec Notes 23 01

  • Slides: 15
Download presentation
Engr 0012 (04 -1) Lec. Notes 23 -01

Engr 0012 (04 -1) Lec. Notes 23 -01

C++ errors/debugging build/compiler does not recognize a statement build/compile process stops error message(s) issued

C++ errors/debugging build/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) Lec. Notes 23 -02

C++ errors/debugging run time build/compile has no errors program stops sometimes just stops Engr

C++ errors/debugging run time build/compile has no errors program stops sometimes just stops Engr 0012 (04 -1) Lec. Notes 23 -03

C++ errors/debugging run time Engr 0012 (04 -1) Lec. Notes 23 -04 sometimes displays

C++ errors/debugging run time Engr 0012 (04 -1) Lec. Notes 23 -04 sometimes displays an error message like

C++ errors/debugging run time Engr 0012 (04 -1) Lec. Notes 23 -05 sometimes displays

C++ errors/debugging run time Engr 0012 (04 -1) Lec. Notes 23 -05 sometimes displays an error message like

C++ errors/debugging most common causes of run time errors division by zero using value

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) Lec. Notes 23 -06

file management creating a data file use your favorite text editor export (text) file

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) Lec. Notes 23 -07

file management opening a file requires a pointer to the file tells program where

file management opening a file requires a pointer to the file tells program where to find file lower case “p” to remind declaring a FILE pointer you that this name is a pointer // variable declaration FILE *p. Infile; *p. Outfile; // input file pointer // output file pointer // variable declaration FILE *preadf; // input file pointer FILE *pwritef; // output file pointer type Engr 0012 (04 -1) Lec. Notes 23 -08 “value at” or pointer operator declaration normally in functions that use files

file management opening a file for reading - basic read p. Infile = fopen("c:

file management opening a file for reading - basic read p. Infile = fopen("c: tempmydata. dat", "r" ); pointer name w/o * fopen function call file name/path 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) Lec. Notes 23 -09

file management opening a file for reading - better method p. Infile = f.

file management opening a file for reading - better method p. Infile = f. Read. Open(); pointer name w/o * f. Read. Open function call f. Read. Open NOT in any. h library f. Read. Open not in any text f. Read. Open available in Get 12 advantages asks user for file name checks for file existence Engr 0012 (04 -1) Lec. Notes 23 -10

file management write opening a file for writing - basic p. Outfile = fopen("c:

file management write opening a file for writing - basic p. Outfile = fopen("c: tempmyresults. out", "w" ); pointer name w/o * fopen function call file name/path 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 Engr 0012 (04 -1) Lec. Notes 23 -11

file management opening a file for writing - better method p. Outfile = f.

file management opening a file for writing - better method p. Outfile = f. Write. Open(); pointer name w/o * f. Write. Open function call f. Write. Open NOT in any. h library f. Write. Open not in any text f. Write. Open available in Get 12 advantages asks user for file name checks for file existence Engr 0012 (04 -1) Lec. Notes 23 -12

file management reading from a file placeholder for data type numread = fscanf( p.

file management reading from a file placeholder for data type numread = fscanf( p. Infile, "%lf", &value ); address to store file pointer just created with f. Read. Open value “file” scanf prototype for fscanf (and scanf) is int fscanf( char[], … ); int scanf( char[], … ); (and scanf) can return the number of values actually read in!!! fscanf we will exploit this to read files with an unknown number of entries Engr 0012 (04 -1) Lec. Notes 23 -13

file management writing to a file formatted placeholder for data type fprintf( p. Outfile,

file management writing to a file formatted placeholder for data type fprintf( p. Outfile, "%7. 2 lf", value ); “file” fprintf file pointer just created with f. Read. Open only difference between fprintf and printf is the file pointer Engr 0012 (04 -1) Lec. Notes 23 -14 value to print

file management hygiene open a file? ? ? close it when finished!!! fclose( p.

file management hygiene open a file? ? ? close it when finished!!! fclose( p. Infile ); or fclose( p. Outfile ); Engr 0012 (04 -1) Lec. Notes 23 -15