Files Working with Files in C ATS 315. Files Misunderstandings about “files” In Windows and on Macs, we tend to think of files as “containing something”.

Slides:



Advertisements
Similar presentations
Computer Programming Lecture 14 C/C++ Pointers
Advertisements

Introduction to C Programming
Week 5 Part I Kyle Dewey. Overview Exam will be back Thursday New office hour More on functions File I/O Project #2.
UNIT 15 File Processing.
Engineering EG167C - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect M1 P. 1Winter Quarter Midterm I Review.
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
Single Variable and a Lot of Variables The declaration int k; float f; reserve one single integer variable called k and one single floating point variable.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 11 – File Processing Outline 11.1Introduction 11.2The Data Hierarchy 11.3Files and Streams 11.4Creating.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
1 Key Concepts:  Data types in C.  What is a variable?  Variable Declaration  Variable Initialization  Printf()  Scanf()  Working with numbers in.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect M1 P. 1Winter Quarter Midterm I Review Topics.
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
CSE1301 Computer Programming: Revision. Topics Type of questions What do you need to know? About the exam Exam technique Sample questions.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
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.
V-1 University of Washington Computer Programming I File Input/Output © 2000 UW CSE.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
© 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 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
Programming I Introduction Introduction The only way to learn a new programming language is by writing programs in it. The first program to.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 7P. 1Winter Quarter File I/O in C Lecture.
1 CSE1301 Computer Programming: Revision. 2 Topics Type of questions What do you need to know? About the exam Exam technique Staff consultation Revision.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room , Chris Hill, Room ,
Engr 0012 (04-1) LecNotes Engr 0012 (04-1) LecNotes C++ errors/debugging build/compile compiler does not recognize a statement build/compile.
EXERCISE Arrays, structs and file processing. Question An online store sells various types of children’s clothing. You are to create a program that stores.
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 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()
 2000 Prentice Hall, Inc. All rights reserved Introduction Data files –Can be created, updated, and processed by C programs –Are used for permanent.
ATS 315: Working With Loops in C Loops in C ATS 315.
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:
chap8 Chapter 12 Files (reference: Deitel ’ s chap 11)
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
1 CSC103: Introduction to Computer and Programming Lecture No 28.
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.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic properties and characteristics of external files ❏ To.
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.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
User Interaction and Variables
Lesson #2 Introduction to C.
Lesson #2 Introduction to C.
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.
Chapter 7 Text Input/Output Objectives
What's a Computer? Monitor Disk Main mouse Memory Keyboard Network
File Access (7.5) CSE 2031 Fall July 2018.
Basic notes on pointers in C
IPC144 Week 10 – Lesson 2 Working with Files
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.
Lesson #2 Introduction to C.
EPSII 59:006 Spring 2004.
CSc 352 File I/O Saumya Debray Dept. of Computer Science
ICS103: Programming in C 6: Pointers and Modular Programming
Files Chapter 8.
Presentation transcript:

Files Working with Files in C ATS 315

Files Misunderstandings about “files” In Windows and on Macs, we tend to think of files as “containing something”. But that’s a bad metaphor.

Files Single “file” All lined up, working with them in order.

Files Single “file” Files are single files of ones and zeros (a.k.a. “bits”)

Files Single “file” Usually work with eight bits at a time (a.k.a. “byte”) There are 256 possible bytes

Files ASCII A code that converts each of the 256 possible bytes into a symbol “A” “B” “newline” “+”

Files A typical file …

Files A typical file …

Files A typical file … #

Files A typical file … #i

Files A typical file … #inc

Files A typical file … #include

Files An ASCII File Need to know the “format” of the file. In this case, the format is: –1 st number=time –2 nd number=temp –3 rd number=dewp

Files An ASCII File This file looks like some kind of table…

Files An ASCII File …but it is really a “file”—lots of numbers and symbols all lined up

Files An ASCII File To read this file, the computer must “scan” through the file. Values in the file are separated by “tokens”, typically spaces

Files An ASCII File To read the first value: –Must know that it is going to be an integer. –Reads “0”, “0”, “0”, and “0” Computes 0x x x x10 0 = 0

Files An ASCII File To read the second value: –Must know that it is going to be a float. –Reads “3”, “4”, “.”, and “5” Computes 3x x x10 -1 = 34.5

Files Opening a File Your files are somewhere on the computer’s harddrive. But where?

Files Opening a File You might think there are “territories” on the disk that contain your files.

Files Opening a File And all of your files are right there, side by side. Assignment6.c Assignment5.c

Files Opening a File But files are really scattered all over the disk, put wherever the operating system could find room for them. Assignment6.c Assignment5.c

Files Opening a File Therefore, to open a file, you are going to need the “address” of the file on the harddrive. Fortunately, there is a C function that gives this to you!

Files Opening a File fin = fopen(“weather.dat”,”r”); The function is “fopen”, for “file open”. It has two “arguments”— –The first argument is the name of the file to open. –The second argument is the action you are going to take: r = “read”, w = “write”

Files Opening a File fin = fopen(“weather.dat”,”r”); The function returns the address of this file on the harddrive. In this case, the address of the file is being stored in a variable called “fin”—for “input file”, but you could call it anything.

Files Opening a File What “type” of variable is fin? FILE *fin; FILE, in contrast to float or int.

Files Opening a File What “type” of variable is fin? FILE *fin; Notice that there is an asterisk in front of the name of the variable when you declare it. That means that fin is a “pointer” to a file.

Files Opening a File An example program. Notice that the file is eventually closed with fclose. main () { FILE *fin; fin = fopen(“weather.dat”,”r”); fclose (fin); }

Files Reading a File Reading from the keyboard: scanf Reading from a file: fscanf

Files Reading a File scanf scanf(“%d”,&time); fscanf

Files Reading a File scanf scanf(“%d”,&time); fscanf fscanf(fin,”%d”,&time);

Files Reading a File scanf scanf(“%d”,&time); fscanf fscanf(fin,”%d”,&time); One additional argument—the address of the file you are reading!

Files Reading a File We use fscanf to read in the values of time, temp, and dewp. main () { FILE *fin; int time; float temp,dewp; fin = fopen(“weather.dat”,”r”); fscanf (fin,”%d”,&time); fscanf (fin,”%f”,&temp); fscanf (fin, %f”,&dewp); fclose (fin); }

Files Reading a File Could all be done in one fscanf statement, if you wanted. main () { FILE *fin; int time; float temp,dewp; fin = fopen(“weather.dat”,”r”); fscanf (fin,”%d %f %f”, &time,&temp,&dewp); fclose (fin); }

Files Reading a File To read six observations, this will need to be in a loop. Don’t open and close the file multiple times! main () { FILE *fin; int time,i; float temp,dewp; fin = fopen(“weather.dat”,”r”); for(i=0;i<5;i++) { fscanf (fin,”%d %f %f”, &time,&temp,&dewp); } fclose (fin); }

Files Writing a File Never read and write from the same file! Writing a file that already exists clobbers the old version of the file, so be careful!

Files Writing a File printf printf(“%d”,time); fprintf

Files Writing a File printf printf(“%d”,time); fprintf fprintf(fout,“%d”,time);

Files Writing a File printf printf(“%d”,time); fprintf fprintf(fout,“%d”,time); Only one additional argument, the address of the file you are writing!

Files Writing a File The file you are writing will be ASCII, meaning that you can look at it in vi or pico, to see if your program is working.

Files Your Assignment Make a copy of the decoded.data file from the ~JSchrage directory. cp ~JSchrage/decoded.data. Contains ten observations. –WARNING: Wind speed is in knots!

Files Your Assignment For each of the ten observations: –Read the observation from the file. –Use your metcalc.c library to compute relative humidity, potential temperature, and wind chill. –Write these results to a file called my.output. –Due Friday, February 13