Presentation is loading. Please wait.

Presentation is loading. Please wait.

Engr 0012 (04-1) LecNotes 23-01. Engr 0012 (04-1) LecNotes 23-02 C++ errors/debugging build/compile compiler does not recognize a statement build/compile.

Similar presentations


Presentation on theme: "Engr 0012 (04-1) LecNotes 23-01. Engr 0012 (04-1) LecNotes 23-02 C++ errors/debugging build/compile compiler does not recognize a statement build/compile."— Presentation transcript:

1 Engr 0012 (04-1) LecNotes 23-01

2 Engr 0012 (04-1) LecNotes 23-02 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

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

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

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

6 Engr 0012 (04-1) LecNotes 23-06 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

7 Engr 0012 (04-1) LecNotes 23-07 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)

8 Engr 0012 (04-1) LecNotes 23-08 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

9 Engr 0012 (04-1) LecNotes 23-09 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

10 Engr 0012 (04-1) LecNotes 23-10 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

11 Engr 0012 (04-1) LecNotes 23-11 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

12 Engr 0012 (04-1) LecNotes 23-12 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

13 Engr 0012 (04-1) LecNotes 23-13 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

14 Engr 0012 (04-1) LecNotes 23-14 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

15 Engr 0012 (04-1) LecNotes 23-15 file management hygiene open a file??? close it when finished!!! fclose( pInfile ); or fclose( pOutfile );


Download ppt "Engr 0012 (04-1) LecNotes 23-01. Engr 0012 (04-1) LecNotes 23-02 C++ errors/debugging build/compile compiler does not recognize a statement build/compile."

Similar presentations


Ads by Google