1 / 19

Programs with Data Files

Programs with Data Files. We have primarily been working with programs which take their input from the screen and write their output to the screen This works well for programs which have little I/O, but not for programs with much I/O For data intensive programs, we must work with data files.

ian-hayden
Download Presentation

Programs with Data Files

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programs with Data Files • We have primarily been working with programs which take their input from the screen and write their output to the screen • This works well for programs which have little I/O, but not for programs with much I/O • For data intensive programs, we must work with data files

  2. Programs with Data Files • We have seen that one way of working with files is to use I/O redirection using the ‘>’ and ‘<‘ when we invoke the program from the command line • An alternative is to use the file oriented commands fprintf and fscanf which are completely analogous to the printf and scanf commands you already know

  3. Programs with Data Files • The only difference between the two groups of commands is that the file-oriented commands must tell which file to read from or write to • Therefore, fprintf and fscanf have an additional argument • The first argument for these commands is a file pointer

  4. Programs with Data Files • A file pointer is just another variable it is declared as in the following example: FILE *inp, *outp; /* 2 file pointers */ • Note the ‘*’ in front of each variable of type file pointer in the declaration - a ‘*’ is a pointer in C • A pointer is a variable which tells us the location of some data type (FILE in this case) • Since a file pointer is just a variable, it must be initialized before we use it

  5. Programs with Data Files • We initialize using the fopen function as in the following examples: inp = fopen(“a:myfile.dat”, “r”); outp = fopen(“a:myoutput.dat”, “w”); • The fopen function takes two arguments • First is the name of the file we will be working with. This is a string, so we enclose it in quotes, being sure to give the complete path of the file • Second is the mode of the file - again in quotes - “r” if we will be reading from the file, “w” if we will be writing to the file

  6. Programs with Data Files • Now that we have initialized the variables inp and outp, we will use these variables to refer to the files in the rest of the program (not the name of the file as it appears to the OS!): fscanf(inp, “%lf”, &miles); fprintf(outp, “Miles is %.2f.\n”, miles);

  7. Programs with Data Files • When we are done using the files (and before we return from the program) we need to close the open data files: fclose(inp); fclose(outp); • You won’t get an error if you don’t close your files, but there is greater risk of data corruption if you don’t

  8. Programs with Data Files • How do we know when we reach the end of an input data file? • We might know how many lines of input (or data records) there are in the file • If we don’t know, we can use the value that fscanf returns - when the end of file is reached the function returns the constant EOF

  9. Programs with Data Files • Example of file processing using EOF: input_status = fscanf(inp, “%d”, &score); while (input_status != EOF) { printf(“%5d\n”, score); sum += score; input_status = fscanf(inp, “%d”, &score); } • The input data file can be create using any text editor (including that of your IDE or Notepad)

  10. Nested Loops • Loops may be nested just like other control structures (e.g. the if) • Nested loops contain an outer loop and one or more inner loops • Each time through the outer loop, we go through the inner loop as many times as required by the controlling condition

  11. Nested Loops #include <stdio.h> int main(void) { int inner, outer; for (outer = 1; outer < 10; outer++){ for (inner = 1; inner < 10; inner++) printf(“%d%d “, outer, inner); printf(“\n”); } return(0); }

  12. The do-while Statement • Both the for and the while loop statement evaluate a loop repetition before the first execution of the loop body • If the condition is false the first we enter the loop, the loop body is never evaluated • In some cases, we must evaluate the loop at least one time • The do-while statement allows us to do this

  13. The do-while Statement • Consider the following loop fragment do { printf(“Enter a letter from A through E> “); scanf(“%c”, &letter_choice); } while (letter_choice < ‘A’ || letter_choice > ‘E’); • The loop body must be executed at least once, so we can use a do-while

  14. Flag-controlled loops • Sometimes a loop condition becomes so complex that placing the expression in its usual spot is awkward • In these cases, a flag variable can be used to indicate whether or not a certain condition has occurred • A flag either has the value 1 (true) or 0 (false)

  15. Flag-controlled loops • Consider the file outline of a loop: error = 0; /* No errors yet! */ do { get some input; if (no error) { process the input; } else error = 1; /* Error condition */ get some more input;

  16. Flag-controlled loops if (no error) { process the input; } else error = 1; /* Error condition */ while (!error); • What would we do if we couldn’t process the second input if the first caused an error?

  17. Debugging and Testing Programs • Syntax errors are discovered at compile time by the compiler • Logic errors and run-time errors occur only when the program is run • To discover the cause of these errors requires special techniques • One way to better understand the behavior of a program is to make use of a special program known as a debugger

  18. Debugging and Testing Programs • A debugger program allows you to execute the statements in the program one at a time (single-step execution) • In this way you can observe the values of variables after each program statement to see if they are having the effect you think they are • You can also set breakpoints where execution will halt so you can observe the values of variables

  19. Debugging and Testing Programs • Some debuggers also allow you to step in and over functions, stop execution when a variable changes, or when some expression evaluates to true • If you don’t have a debugger available, you can achieve somewhat the same effect with diagnostic calls to printf which give you the value of some variables at strategic points in the execution of the program

More Related