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

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

Embed Size (px)

Citation preview

FilesCOP3275 – 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

Format Operation Example

> file.txt Redirect output to file.txt ./a.out > file.txt

< file.txt Redirect input to file.txt ./a.out < file.txt

< input.txt > output.txt Redirect both ./a.out < input.txt > 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 Files7/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 7th will be online.• That means our last lecture will be August 5th

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