files working with files in c ats 315. files misunderstandings about “files” in windows and on...

42
Files Working with Files in C ATS 315

Upload: roger-gilmore

Post on 30-Dec-2015

226 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: 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”

Files

Working with Files in C

ATS 315

Page 2: 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”

Files

Misunderstandings about “files”

• In Windows and on Macs, we tend to think of files as “containing something”.

• But that’s a bad metaphor.

Page 3: 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”

Files

Single “file”

• All lined up, working with them in order.

Page 4: 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”

Files

Single “file”

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

1 0 0 0 1 1 0 1

Page 5: 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”

Files

Single “file”

• Usually work with eight bits at a time (a.k.a. “byte”)

• There are 256 possible bytes.

1 0 0 0 1 1 0 1

Page 6: 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”

Files

ASCII

• A code that converts each of the 256 possible bytes into a symbol.

01011101

“A”

01101110

“B”

11101001

“newline”

11100101

“+”

Page 7: 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”

Files

A typical file

0100101010011100101111010110101010101010011010101010101010101010110110100101010101001001010010101010101010101010100101010011100101111010110101010101010011010101010101010101010110110100101010101001001010010101010101010101010…

Page 8: 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”

Files

A typical file

0100101010011100101111010110101010101010011010101010101010101010110110100101010101001001010010101010101010101010100101010011100101111010110101010101010011010101010101010101010110110100101010101001001010010101010101010101010…

Page 9: 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”

Files

A typical file

0100101010011100101111010110101010101010011010101010101010101010110110100101010101001001010010101010101010101010100101010011100101111010110101010101010011010101010101010101010110110100101010101001001010010101010101010101010…

#

Page 10: 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”

Files

A typical file

0100101010011100101111010110101010101010011010101010101010101010110110100101010101001001010010101010101010101010100101010011100101111010110101010101010011010101010101010101010110110100101010101001001010010101010101010101010…

#i

Page 11: 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”

Files

A typical file

0100101010011100101111010110101010101010011010101010101010101010110110100101010101001001010010101010101010101010100101010011100101111010110101010101010011010101010101010101010110110100101010101001001010010101010101010101010…

#inc

Page 12: 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”

Files

A typical file

0100101010011100101111010110101010101010011010101010101010101010110110100101010101001001010010101010101010101010100101010011100101111010110101010101010011010101010101010101010110110100101010101001001010010101010101010101010…

#include<stdio.h>

Page 13: 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”

Files

An ASCII File

• Need to know the “format” of the file.

• In this case, the format is:– 1st number=time– 2nd number=temp– 3rd number=dewp

0000 34.5 30.90100 33.9 30.80200 33.1 30.70300 32.7 30.00400 31.5 30.00500 31.0 29.8

Page 14: 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”

Files

An ASCII File

• This file looks like some kind of table…

0000 34.5 30.90100 33.9 30.80200 33.1 30.70300 32.7 30.00400 31.5 30.00500 31.0 29.8

Page 15: 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”

Files

An ASCII File

• …but it is really a “file”—lots of numbers and symbols all lined up.

0000 34.5 30.9<newline>0100 33.9 30.8<newline>0200 33.1 30.7<newline>0300<newline>32.7<newline>30.0<newline>0400 31.5 30.0<newline>0500 31.0 29.8

Page 16: 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”

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.

0000 34.5 30.90100 33.9 30.80200 33.1 30.70300 32.7 30.00400 31.5 30.00500 31.0 29.8

Page 17: 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”

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”.

0000 34.5 30.90100 33.9 30.80200 33.1 30.70300 32.7 30.00400 31.5 30.00500 31.0 29.8

Computes 0x103 + 0x102 + 0x101 + 0x100 = 0

Page 18: 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”

Files

An ASCII File

• To read the second value:– Must know that it is

going to be a float.– Reads “3”, “4”, “.”,

and “5”.

0000 34.5 30.90100 33.9 30.80200 33.1 30.70300 32.7 30.00400 31.5 30.00500 31.0 29.8

Computes 3x101 + 4x100 + 5x10-1 = 34.5

Page 19: 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”

Files

Opening a File

• Your files are somewhere on the computer’s harddrive.

• But where?

Page 20: 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”

Files

Opening a File

• You might think there are “territories” on the disk that contain your files.

Page 21: 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”

Files

Opening a File

• And all of your files are right there, side by side.

Assignment6.c

Assignment5.c

Page 22: 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”

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

Page 23: 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”

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!

Page 24: 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”

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”

Page 25: 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”

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.

Page 26: 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”

Files

Opening a File

• What “type” of variable is fin?

• FILE *fin;

• FILE, in contrast to float or int.

Page 27: 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”

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.

Page 28: 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”

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);

}

Page 29: 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”

Files

Reading a File

• Reading from the keyboard: scanf

• Reading from a file: fscanf

Page 30: 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”

Files

Reading a File

scanf

• scanf(“%d”,&time);

fscanf

Page 31: 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”

Files

Reading a File

scanf

• scanf(“%d”,&time);

fscanf

• fscanf(fin,”%d”,&time);

Page 32: 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”

Files

Reading a File

scanf

• scanf(“%d”,&time);

fscanf

• fscanf(fin,”%d”,&time);

• One additional argument—the address of the file you are reading!

Page 33: 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”

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);

}

Page 34: 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”

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);

}

Page 35: 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”

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);

}

Page 36: 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”

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!

Page 37: 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”

Files

Writing a File

printf

• printf(“%d”,time);

fprintf

Page 38: 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”

Files

Writing a File

printf

• printf(“%d”,time);

fprintf

• fprintf(fout,“%d”,time);

Page 39: 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”

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!

Page 40: 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”

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.

Page 41: 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”

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!

Page 42: 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”

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