files in c n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Files in C PowerPoint Presentation
Download Presentation
Files in C

Loading in 2 Seconds...

play fullscreen
1 / 27
rafael-kim

Files in C - PowerPoint PPT Presentation

99 Views
Download Presentation
Files in C
An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Files in C

  2. File Types • The UNIX filesystem contains several different types of files: • Ordinary Files • Used to store your information, such as some text you have written or an image you have drawn. This is the type of file that you usually work with. • Always located within/under a directory file • Do not contain other files • Directories • Branching points in the hierarchical tree • Used to organize groups of files • May contain ordinary files, special files or other directories • Never contain "real" information which you would work with (such as text). Basically, just used for organizing files. • All files are descendants of the root directory, ( named / ) located at the top of the tree.

  3. More Files • Special Files • Used to represent a real physical device such as a printer, tape drive or terminal, used for Input/Ouput (I/O) operations • Unix considers any device attached to the system to be a file - including your terminal: • By default, a command treats your terminal as the standard input file (stdin) from which to read its input • Your terminal is also treated as the standard output file (stdout) to which a command's output is sent • Stdin and stdout will be discussed in more detail later • Two types of I/O: character and block • Usually only found under directories named /dev • Pipes • UNIX allows you to link commands together using a pipe. The pipe acts a temporary file which only exists to hold data from one command until it is read by another • For example, to pipe the output from one command into another command: who | wc -l This command will tell you how many users are currently logged into the system. The standard output from the who command is a list of all the users currently logged into the system. This output is piped into the wc command as its standard input. Used with the -l option this command counts the numbers of lines in the standard input and displays the result on its standard output - your terminal.

  4. Important concepts: • File -- a named set of data stored together on the disk. C views any file as a stream of bytes – it’s up to the programmer to give it a structure. • • to use: (1) open the file; (2) read/write from/to the file; (3) close file.

  5. Hierarchical File Structure • All of the files in the UNIX file system are organized into a multi-leveled hierarchy called a directory tree. • A family tree is an example of a hierarchical structure that represents how the UNIX file system is organized. The UNIX file system might also be envisioned as an inverted tree or the root system of plant. • At the very top of the file system is single directory called "root" which is represented by a / (slash). All other files are "descendents" of root. • The number of levels is largely arbitrary, although most UNIX systems share some organizational similarities. The "standard" UNIX file system is discussed later.

  6. / (root) • | • ------------------------- • | | | • /bin /usr /tmp • | • ------------------------- • | | | • /public /misc /staff

  7. Introduction • • All Input/Output up to now has been done from/to the keyboard/screen. • • There is a serious limitation with this: when the program terminates, the • data is lost. So we can’t write a database program, which stores • information and retrieves this information next time the program is started. • Or a word processor. Or an email system. Or a spreadsheet. Or … • • We can however use external storage devices like the computer’s hard disk for storage. Instead of writing output to the screen, we can write it to a file. This data won’t be lost when the program terminates.

  8. Binary and Text Files • C can handle two types of files: binary files and text (or ASCII) files. The difference between the two file types is in the way they store numeric data. • If numeric data is stored in a binary file, it is stored as binary numbers. • If numeric data is stored in a text file, each digit of the numeric data is converted to its ASCII format and stored like that. • So the number 123 requires three bytes of storage in a text file because it has three digits: ‘1’, ‘2’, and ‘3’. • 123 only requires 2 bytes of storage in a binary file because the binary representations of integer values take up two bytes. • So binary files are more compact for storing data which is primarily • numeric.

  9. Introduction to File Input/Output • Also, numeric data must be converted to its ASCII form before • writing to a text file and converted from its ASCII form when reading • from a text file. • With binary files, no such conversions are necessary. • However, text files are easily displayed on the screen (using e.g. the • Windows notepad editor), so their contents can be easily examined or • checked. They can also be read easily by other programs. Neither of these • is true for binary files.

  10. Declaring Files • You declare a variable to store a file pointer like this: • FILE *fp; • The type FILE is predefined for you by <stdio.h>. It is a data structure which holds the information the standard I/O library needs to keep track of the file for you. For historical reasons, you declare a variable which is a pointer to this FILE type. • The name of the variable can (as for any variable) be anything you choose; it is traditional to use the letters fp in the variable name (since we're talking about a file pointer). If you were reading from two files at once you'd probably use two file pointers: FILE *fp1, *fp2;

  11. File Names • UNIX permits file names to use most characters, but avoid spaces, tabs and characters that have a special meaning to the shell, such as: & ; ( ) | ? \ ' " ` [ ] { } < > $ - ! / • Case Sensitivity: uppercase and lowercase are not the same! These are three different files: NOVEMBER November november • Length: can be up to 256 characters • Extensions: may be used to identify types of files libc.a - archive, • library file program.c - C language source file alpha2.f - Fortran source file • xwd2ps.o - Object/executable code • mygames.Z - Compressed file

  12. Miscellaneous File Names • Hidden Files: have names that begin with a dot (.) For example: .cshrc .login .mailrc .mwmrc • Uniqueness: as children in a family, no two files with the same parent directory can have the same name. Files located in separate directories can have identical names. • Reserved Filenames: • / - the root directory (slash) • . - current directory (period) • .. - parent directory (double period) • ~ - your home directory (tilde)

  13. Pathnames • Specify where a file is located in the hierarchically organized file system • Must know how to use pathnames to navigate the UNIX file system • Absolute Pathname: tells how to reach a file begining from the root; always begins with / (slash). For example: /usr/local/doc/training/sample.f • Relative Pathname: tells how to reach a file from the directory you are currently in ( current or working directory); never begins with / (slash). For example: training/sample.f ../bin ~/projects/report.001 • For example, if your current directory is /usr/home/johnson and you wanted to change to the directory /usr/home/quattro, you could use either of these commands: cd ../quattro - relative pathname cd /usr/home/quattro - absolute pathname

  14. Input from File Example • #include <stdio.h> • void main(void){ • int var1, var2; /* not initialised */ • FILE *fp; • /* fp is a variable of type "FILE *" and is */ • /* called a file pointer. The datatype */ • /* "FILE" is defined in stdio.h, and is a */ • /* structure in which C keeps information */ • /* about a file */ • fp=fopen("datafile.txt", "r"); /* open read-only */ • fscanf(fp, "%d %d", &var1, &var2); • fclose(fp); • printf("first value read in was %d\n", var1); • printf("second value read in was %d\n", var2); • } • Contents of file datafile.txt: 3 • -1 • Produces the screen output: first value read in was 3 • second value read in was -1

  15. Input from file continued • fp=fopen("datafile.txt", "r"); • This statement “associates” the file datafile.txt with the file pointer fp, • and tells the C compiler that the program is opening datafile.txt for • reading only. • 2nd argument to fopen() gives the file’s “open mode” – possibilities include: • r open an existing file for reading only • w open a new file (or overwrite an existing file) for writing only • a open a file for appending (writing at the end of the file) – creates a new file if it • doesn’t currently exist • r+ open an existing file for reading and writing, starting at the beginning of the file • w+ open a new file (or overwrite an existing file) for reading and writing • a+ open a file for reading and appending – creates a new file if it doesn’t currently exist

  16. fscanf(fp, "%d %d", &var1, &var2); • Enables formatted input from the file pointed to by fp, otherwise has the • same functionality as scanf() • fclose(fp); • When program is finished with an open file, it should “break” the • association that was formed when the file was opened. This fclose() • statement closes the file pointed to by fp. • Any files which have not been closed by fclose() are automatically • closed (by the Operating System) when program execution is finished.

  17. Output To File • #include <stdio.h> • void main(void){ • int var1, var2; /* not initialised */ • FILE *fp; • FILE *fptr; • fp=fopen("datafile.txt", "r"); • fscanf(fp, "%d %d", &var1, &var2); • fclose(fp); • fptr=fopen("datafile.txt", "w"); /* open write-only */ • fprintf(fptr, "%d\n%d", var2, var1); /* swap values */ • fclose(fptr); • } • Contents of file datafile.txt before program execution: 3 • -1 • Contents of file datafile.txt after program execution: -1 • 3 • (so in this case, the contents of datafile.txt have been overwritten)

  18. Output To File • fptr=fopen("datafile.txt", "w"); • Now we open datafile.txt again, this time for writing. We use a • different file pointer, fptr, to point to the opened file. • fprintf(fptr, "%d\n%d", var2, var1); • Enables formatted output to the file pointed to by fp, otherwise has the • same functionality as printf() • fclose(fptr); • Again, it is important to close the file when the program is finished with it.

  19. Output To File • #include <stdio.h> • void main(void){ • int var1, var2; /* not initialised */ • FILE *fp; • fp=fopen("datafile.txt", "r"); • fscanf(fp, "%d %d", &var1, &var2); • fp=fopen("datafile.txt", "w"); /* change mode */ • fprintf(fp, "%d\n%d", var2, var1); • fclose(fp); • } • Contents of file datafile.txt before program execution: 3 • -1 • Contents of file datafile.txt after program execution: -1

  20. Output to File example of Appending • #include <stdio.h> • void main(void){ • int var1, var2; /* not initialised */ • FILE *fp; • FILE *fptr; • fp=fopen("datafile.txt", "r"); • fscanf(fp, "%d %d", &var1, &var2); • fclose(fp); • fptr=fopen("datafile.txt", "a"); • fprintf(fptr, "%d\n%d", var2, var1); • fclose(fptr); • } • Contents of file datafile.txt before program execution: 3 • -1 • Contents of file datafile.txt after program execution: 3 • -1 • -1 • 3

  21. Reading Data from a file • First, you have to know the name of the file and where it is stored. • You also need to know how the data is laid out in the file. In particular: • • Order and datatype(s) of the data values. • • How much data is in the file. 3 common methods for this: • – First line tells the number of data “records” that follow. • – Sentinel signal: a value outside the range of actual data values • which indicates that the end of the data has been reached. • – Test to see if the special end-of-file indicator (inserted into every • file by the Operating System) has been reached. This is usually • the best choice. • Of course, if you write data to a file, you may have to decide which way • of storing the data is most suitable for your application.

  22. Reading Data from a file example • Contents of datafile1.txt: 3 • 0.2 80 • 0.7 60 • 0.1 50 • (here, the first value tells us the number of lines of data – to read them, you • should use a counter-controlled loop…) • Contents of datafile2.txt: 0.2 80 • 0.7 60 • 0.1 50 • -99 -99 • (here, the sentinel values tell us that the end of the data has been reached – you • should use a loop and test for the sentinel values each time around…) • Contents of datafile3.txt: 0.2 80 • 0.7 60 • 0.1 50 • (here, the file is just data – you should check whether each read got the end-offile • indicator. An example program to do this is given next)

  23. Reading data from a file example Program • #include <stdio.h> • void main(void){ • float prob, avg=0.0; • int quantity, num_values=0; • FILE *fptr; • fptr=fopen("datafile3.txt", "r"); • while(fscanf(fptr, " %f %d", &prob, &quantity)==2){ • /* if return value from this fscanf() is not 2, */ • /* the end-of-file indicator has been reached */ • avg = avg + (prob*quantity); • } • fclose(fptr); • printf("average value is %.2f\n", avg); • } • Produces the correct screen output: average value is 63.00 • Note: return value from fscanf() – and scanf() – is the number of • successful assignments. So in this case, this return value should be 2 every • time a valid data “record” has been read. Otherwise – error or end-of-file.

  24. I/O with Files • The companion function to printf is fprintf, and the file pointer argument comes first. To print a string to the output.dat file we opened in the previous section, we might call • fprintf(ofp, "Hello, world!\n"); • The companion function to putchar is putc, and the file pointer argument comes last. To write a character to output.dat, we could call • putc(c, ofp);

  25. Input • The companion function to getchar is getc, and the file pointer is its only argument. To read a character from the input.dat file we opened in the previous section, we might call • int c; c = getc(ifp); • Our own getline function calls getchar and so always reads the standard input. We could write a companion fgetline function which reads from an arbitrary file pointer:

  26. Example Program • #include <stdio.h> /* Read one line from fp, */ • /* copying it to line array (but no more than max chars). */ • /* Does not place terminating \n in line array. */ • /* Returns line length, or 0 for empty line, or EOF for end-of-file. */ • int fgetline(FILE *fp, char line[], int max) • { int nch = 0; int c; max = max - 1; • /* leave room for '\0' */ • while((c = getc(fp)) != EOF) • { if(c == '\n') break; if(nch < max) • { line[nch] = c; nch = nch + 1; } • } • if(c == EOF && nch == 0) return EOF; • line[nch] = '\0'; • return nch; }

  27. Or Else • Now we could read one line from ifp by calling • char line[MAXLINE]; ... fgetline(ifp, line, MAXLINE);