Files COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.

Slides:



Advertisements
Similar presentations
File Management in C. What is a File? A file is a collection of related data that a computers treats as a single unit. Computers store files to secondary.
Advertisements

Week 5 Part I Kyle Dewey. Overview Exam will be back Thursday New office hour More on functions File I/O Project #2.
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,
UNIT 15 File Processing.
BITS Pilani, Pilani Campus TA C252 Computer Programming - II Vikas Singh File Handling.
File Management in C. A file is a collection of related data that a computers treats as a single unit. File is a collection of data stored permanently.
Files in C Rohit Khokher. Files in C Real life situations involve large volume of data and in such cases, the console oriented I/O operations pose two.
C Programming - Lecture 3 File handling in C - opening and closing. Reading from and writing to files. Special file streams stdin, stdout & stderr. How.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Data files –Can be created, updated,
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Topic 13 – Various Other Topics. Enumerated Types.
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
V-1 University of Washington Computer Programming I File Input/Output © 2000 UW CSE.
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
The preprocessor and the compilation process COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students C File Input and Output Checking input for errors.
C Programming - Lecture 6 This lecture we will learn: –Error checking in C –What is a ‘wrappered function’? –What is a clean interface? –How to earn your.
1 Homework Introduction to HW7 –Complexity similar to HW6 –Don’t wait until last minute to start on it File Access will be needed in HW8.
C Basic File Input/Output Manipulation C Programming – File Outline v File handling in C - opening and closing. v Reading from and writing to files.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 7P. 1Winter Quarter File I/O in C Lecture.
22. FILE INPUT/OUTPUT. File Pointers and Streams Declarations of functions that perform file I/O appear in. Each function requires a file pointer as a.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Introduction to Programming Using C Files. 2 Contents Files Working with files Sequential files Records.
File IO and command line input CSE 2451 Rong Shi.
COP Structures Instructor: Diego Rivera-Gutierrez I’m back baby!
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
1 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
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.
Pointers COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
Arrays as pointers and other stuff COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
COP 3275 – Character Strings Instructor: Diego Rivera-Gutierrez.
COP 3275 – Character Strings and Introduction to Pointers Instructor: Diego Rivera-Gutierrez.
Files A collection of related data treated as a unit. Two types Text
FILES IN C. File Operations  Creation of a new file  Opening an existing file  Reading from a file  Writing to a file  Moving to a specific location.
CSC Programming for Science Lecture 18: More Data Files.
Files. FILE * u In C, we use a FILE * data type to access files. u FILE * is defined in /usr/include/stdio.h u An example: #include int main() { FILE.
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.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
FILE I/O: Low-level 1. The Big Picture 2 Low-Level, cont. Some files are mixed format that are not readable by high- level functions such as xlsread()
Lesson #5 Repetition and Loops.
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.
Lesson #5 Repetition and Loops.
TMF1414 Introduction to Programming
University of Washington Computer Programming I
Chapter 7 Text Input/Output Objectives
File Access (7.5) CSE 2031 Fall July 2018.
File I/O.
Plan for the Day: I/O (beyond scanf and printf)
Programming in C Input / Output.
Some Basics for Problem Analysis and Solutions
Files I/O, Streams, I/O Redirection, Reading with fscanf
CSE1320 Files in C Dr. Sajib Datta
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Lesson #5 Repetition and Loops.
Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
FILE HANDLING IN C.
File Input and Output.
File Handling.
Fundamental of Programming (C)
Chapter 12: File I/O.
Lesson #5 Repetition and Loops.
Module 12 Input and Output
ETE 132 Lecture 8 By Munirul Haque.
CSc 352 File I/O Saumya Debray Dept. of Computer Science
EECE.2160 ECE Application Programming
Professor Jodi Neely-Ritz University of Florida
Files Chapter 8.
Presentation transcript:

Files COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

Administrative stuff Quiz #7 Posted yesterday! Due Saturday at 11:59pm through Canvas. No late submissions. Homework #5 Cases were posted. I’ll play a game with each homework to check the default to a random board. Questions?

Files!

The “boring” way Ok, maybe it isn’t boring. Actually today, it might be the one you find the most useful… Do I have your attention yet? Linux/UNIX can redirect input and/or output to a file… So instead of manually entering all those long long cases to test our homework… We could create a file with all the plays, and let the Linux do it’s thing! ONE Big caveat… Save the file with Unix line endings… Let’s look at that in Notepad++…. Edit->EOL operations->Unix/OSX

Now how do we redirect input output FormatOperationExample > file.txtRedirect output to file.txt./a.out > file.txt < file.txtRedirect input to file.txt./a.out < file.txt output.txtRedirect both./a.out output.txt Let’s do a couple examples with my homework #5 solution

Fair warning This doesn’t help you with the extra points… But it IS an awesome way to do your testing!

Now the one that actually matters for the extra credit

Where are the files we work with stored? Files are usually stored in? The hard drive! When we are working on them, though… like having a Word document open… where are they? Both in memory and the hard drive. Technically there are two copies! One in memory – “the working copy” One in hard drive – “the saved copy” That’s why if we don’t save and our computer turns off, we lose our changes. They were in the working copy, which is memory. Memory gets cleared on reboot (it is non-permanent storage). When we open a file, we are loading a copy in memory. Accessing hard disk is expensive (time consuming), so we do most operations and memory (fast) and then save to hard disk.

Now, how does that work in C? First we have a type. (Defined in stdio.h, so nothing new to include). FILE And a function fopen Receives two parameters, both are character strings. Parameter 1: Address to the file Parameter 2: Mode in which it is being open (more on that in a sec) Return type: FILE * (yes a pointer) Example: FILE *f = fopen("wow.txt", "r"); Why a pointer? fopen loads the file into memory and returns a pointer to the position in memory where the file is stored. NULL is returned if the file could not be open.

I/O Mode We need to tell the OS what sort of operations we will be performing on the file. We can perform use: “r” for read mode “w” for write mode (overrides existing) “a” for append mode (writes to the end of file) Each one has an “update” mode that combines both reading and writing: “r+” “w+” “a+” On Windows if you are opening binary files you might need to add a ‘b’ to those modes.

getc and putc getc and putc allows to read and write one single character getc receives a single parameter of type FILE * and returns the character read. putc receives two parameters: a character and a FILE *. Examples: FILE * f = fopen("example", "r+“); char c = getc(f); putc('\n', f);

fflush Remember, once we open the file, it is in memory. All the operations of getc and putc are in memory. By default, the operating system will replicate the changes to disk whenever it is convenient. However, sometimes your program might fail before this happens, leaving unsaved changes. How do we save? fflush is a function that makes sure the changes made in memory get saved to disk. Receives a single parameter of type FILE *. If you send NULL, it flushes the console output (which is sometime convenient for debugging purposes)

fclose Once you are done with your changes to a file, it is good practice to close it. fclose receives one parameter of type FILE * and unloads the file from memory. It is similar to calling free on a pointer created using malloc.

feof If you are reading a file, it is important to check for the end of file (so that we don’t go past the end into memory we don’t have access to). feof is a function that returns true if the end of file has been reached, and false if it hasn’t. It receives a single parameter of type FILE *.

fprintf and fscanf Of course, writing and reading by character is extremely inefficient. Versions of printf and scanf that access files exist. They receive the FILE * as the first parameter, but otherwise work exactly the same as our usual printf and scanf.

fgets and fputs On Monday. fgets -> reads a full line fputs -> writes a string.

Let’s do a couple examples

More on Files 7/20/2015

Administrative stuff Quizzes on 7/24 and 7/31 will definitely be in class. 7/24 Will be mostly on files, but will have a bit of pointers (more details on Wednesday) Quiz on August 7 th will be online. That means our last lecture will be August 5 th Homework #5 Due Friday at 11:59pm Any Questions? Homework #6 Will be available on Friday. You will have until the last

Let’s start with a quick recap of the last quiz

Recap on files Type: FILE Opening: fopen Closing: fclose “Saving”/Flushing: fflush Read: getc fscanf fgets Write putc fprintf fputs

Let’s go back and improve a bit on the code we wrote on Friday

The loop we were using to read and write FILE *f = fopen(argv[1], "r"); FILE *output = fopen(argv[2], "w"); while(!feof(f) ) { char c = getc(f); putc(c, output); } If you remember, there was a weird character at the end of the file. Why?

A bit more on end of file The weird character is the end of file. We looked at feof That one checked if the end of file was reached. The problem is that with our program getc only raises the end of file flag after a read operation actually read the end of file. while(!feof(f) ) { char c = getc(f); putc(c, output); }

We can abuse an interesting fact about assignments Some of you have found this before but didn’t really understand what was happening. int i = 0; if(i = 10) { printf("true"); }else { printf("false"); }

We can abuse an interesting fact about assignments Some of you have found this before but didn’t really understand what was happening. int i = 0; if(i = 10) { printf("true"); }else { printf("false"); } When we do an assignment, the assignment operation returns a value. The value we are assigning to the variable. In the example: 10.

So, to avoid the EOF making the output file: char c; while( (c = getc(f)) != EOF ) { putc(c, output); }

Recap on files Type: FILE Opening: fopen Closing: fclose “Saving”/Flushing: fflush Read: getc fscanf fgets Write putc fprintf fputs

fgets Reads up to a certain number of characters or to the end of line. Three parameters char* str: a pointer to an array of characters. The read input will be saved here num: maximum number of characters to be copied. (Usually the size of your array). FILE* stream: your file. Returns the pointer to the array of characters, or NULL on error. Fun fact… gets (without the f) does the same thing but with the standard input (the console)gets This is KEY to your implementation of the extra credit….

fputs Writes a string to a file. This one is not as exciting. Two parameters: char *str: the string to be written (should be null-terminated '\0' ) FILE* stream: the file to write to.

Let’s work on some code