CSC 107 - Programming for Science Lecture 18: More Data Files.

Slides:



Advertisements
Similar presentations
I/O means Input and Output. One way: use standard input and standard output. To read in data, use scanf() (or a few other functions) To write out data,
Advertisements

UNIT 15 File Processing.
Recitation By yzhuang, sseshadr. Agenda Debugging practices – GDB – Valgrind – Strace Errors and Wrappers – System call return values and wrappers – Uninitialization.
BITS Pilani, Pilani Campus TA C252 Computer Programming - II Vikas Singh File Handling.
UNIT 12 UNIX I/O Redirection.
CSC Programming for Science Lecture 5: Actual Programming.
Chapter 11: Data Files & File Processing In this chapter, you will learn about Files and streams Creating a sequential access file Reading data from a.
1 ICS103 Programming in C Lecture 8: Data Files. 2 Outline Why data files? Declaring FILE pointer variables Opening data files for input/output Scanning.
Topic 13 – Various Other Topics. Enumerated Types.
Guide To UNIX Using Linux Third Edition
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
Chapter 18 I/O in C. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Standard C Library I/O commands.
CSC 107 – Programming For Science. Today’s Goal  Learn C functions to input and output data  Using printf to print out variables and strings  Read.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students C File Input and Output Checking input for errors.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
Files COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
File Handling Spring 2013Programming and Data Structure1.
CGS 3460 More On File IO. CGS 3460 What is a File n A file is a package of information with a name attached to it. n Files are used for various purposes:
File IO and command line input CSE 2451 Rong Shi.
Engr 0012 (04-1) LecNotes Engr 0012 (04-1) LecNotes C++ errors/debugging build/compile compiler does not recognize a statement build/compile.
Lecture 8a: File I/O BJ Furman 21MAR2011. Learning Objectives Explain what is meant by a data stream Explain the concept of a ‘file’ Open and close files.
Chapter 3 Input and Output
Chapter 11: Data Files and File Processing Files and streams Creating a sequential access file Reading data from a sequential access file Using fgetc()
Chapter 7 : File Processing1 File-Oriented Input & Output CHAPTER 7.
1 CHAPTER6 CHAPTER 6. Objectives: You’ll learn about;  Introduction  Files and streams  Creating a sequential access file  Reading data from a sequential.
Week 9 - Nov 7, Week 9 Agenda I/O redirection I/O redirection pipe pipe tee tee.
CSC Programming for Science Lecture 36: Structures.
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
PHY 107 – Programming For Science. The Week’s Goal.
CS 261 – Recitation 7 Spring 2015 Oregon State University School of Electrical Engineering and Computer Science.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
Files A collection of related data treated as a unit. Two types Text
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic properties and characteristics of external files ❏ To.
Lecture 20: C File Processing. Why Using Files? Storage of data in variables and arrays is temporary Data lost when a program terminates. Files are used.
Connecting to Files In order to read or write to a file, we need to make a connection to it. There are several functions for doing this. fopen() – makes.
Hank Childs, University of Oregon April 15 th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / /
User-Written Functions
Chapter 7 Text Input/Output Objectives
Chapter 7 Text Input/Output Objectives
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
TMF1414 Introduction to Programming
Chapter 7 Text Input/Output Objectives
CS1010 Programming Methodology
File Access (7.5) CSE 2031 Fall July 2018.
CS 261 – Recitation 7 Fall 2013 Oregon State University
Plan for the Day: I/O (beyond scanf and printf)
CS111 Computer Programming
Programming in C Input / Output.
Programming in C Input / Output.
Files I/O, Streams, I/O Redirection, Reading with fscanf
Hank Childs, University of Oregon
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
File I/O Lesson Outline
IPC144 Week 10 – Lesson 2 Working with Files
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
File Input and Output.
Accessing Files in C Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Line at a time I/O with fgets() and fputs()
Programming in C Input / Output.
Files.
Lecture 4 Redirecting standard I/O & Pipes
FILE handeling.
Module 12 Input and Output
ETE 132 Lecture 8 By Munirul Haque.
CSc 352 File I/O Saumya Debray Dept. of Computer Science
ICS103: Programming in C 6: Pointers and Modular Programming
Professor Jodi Neely-Ritz University of Florida
Files Chapter 8.
Presentation transcript:

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