Output

Chapter 12 File Input/Output 1. Streams and Buffers 2. Files 3. Accessing a File 4. Character Input/Output 5. String Input/Output 6. Formatted File In...
Author: Myrtle Simpson
3 downloads 0 Views 233KB Size
Chapter 12 File Input/Output 1. Streams and Buffers 2. Files 3. Accessing a File 4. Character Input/Output 5. String Input/Output 6. Formatted File Input/Output 7. Binary and Block I/O 8. Random Access 9. Other I/O Functions

1. Streams and Buffers • Files are used to store large amount of info on secondary storage devices • File Concept in Unix and C: – There are input and output devices, e.g., – input devices: keyboard, camera, disk drive, ... – output devices: disk drive, tape drive, printer, robot arm, monitor, ...

• In Unix and C, each device is conveniently treated as a file – input files: keyboard, camera, disk drive, .. – output files:disk drive, tape drive, printer, robot arm, monitor, ...

Stream I/O • All file I/O operations are carried out by means of streams. • A stream is a sequence of characters. It connects a program to a device (or file) and transfers the data in (input stream) and out (output stream) from the program. • Advantage: the programmer does not need to write specific input/output functions for different devices such as keyboard, display, printer, etc.

data

data program stream

keyboard

scanf(); gets();

printf(); puts();

stream

display

Standard Streams • In C, 3 files are automatically opened for every executable program, e.g. a.out, to get inputs, write outputs and write error messages as shown in the following diagram. • These three files are referred by three file pointers stdin (standard input), stdout (standard output) and stderr (standard error). program stdout (1)

display

stdin (0) stderr (2)

keyboard

Continued • By default, the three automatically opened files are the keyboard and the monitor. • These three files / devices are opened automatically no matter your program will take inputs or not (or will produce outputs or not). • Stream Redirection – One can explicitly change the file pointers stdin, stdout to point to other I/O devices using redirections as:

a.out > outputFile1 a.out  outputFile

display

outputFile1 program stdout (1) stdin (0) stderr (2)

inputFile1 display

prog < inputFile1 > outputFile1

Continued • To read inputs from stdin, one can use the getchar(), gets(), and scanf() functions. • To write outputs to stdout, one can use the putchar(), puts() and printf() functions. • Buffering – Buffering is used for input and output. – I/O operations are expensive & involve mechanical movement. – A buffer is a sequence of memory locations that is used to store data temporarily between the program and an I/O device.

Input buffer Program gets(string); printf(“World\n”);

Hello! Hello!

Output buffer

World World

Type “Hello!” then press ENTER keyboard World display

• Input buffer – collects input characters until the Enter key is pressed or a newline character is encountered. • Output buffer – keeps the output characters and transfer the data to the output device as a block whenever the output stream is flushed (i.e. a newline is encountered or when the program switches from generating output to reading input.)

Files • In C, a file stream is seen as a continuous sequence of bytes, each of which can be read individually. • This corresponds to the file structure in the UNIX environment. • However, since other environments may not implement files in the same way as UNIX, ANSI C provides 2 ways to view files: text and binary.

Line one\r\n Line two\r\n

A DOS-format text file

^Z

Line one\r\n Line two\r\n ^Z

• the way it looks to a C program when opened in the text mode: Needs conversion

Line one\n Line two\n

• the way it looks to a C program when opened in the binary mode

3. Accessing a File • In general, some programs might need to open more than just three files • stdio provides the following 4 steps in accessing files in general. – Creating File Pointers – Opening Files – Performing I/O Operations – Closing Files

Illustration /* Version 1 ­ Open 2 input files to read and open one output  file to write.*/ #include  main(void)  {      /* Step 1: create file pointers */      FILE *fp1, *fp2, *fp3;      int i, j;      /* Step 2: initialize file pointers on opening three files  */      fp1 = fopen("data1", "r");      fp2 = fopen("data2", "r");      fp3 = fopen("output", "w");      /* Step 3: do I/O using file ptrs */      fscanf(fp1, "%d", &i);      fscanf(fp2, "%d", &j);      fprintf(fp3, "%d %d\n", i, j);   

     /* step 4: close the files */      fclose(fp1);      fclose(fp2);      fclose(fp3);      return(0); }

• Step 1: create / define file pointer variables using a new data type FILE (declared in stdio.h) as FILE *fp1, *fp2, *fp3;

• Step 2: initialize file pointers to point to some files, or opening some files to do I/O.

Continued • This is done by using the file opening function, fopen(), provided in stdio.h. For example, the following two statements fp2 = fopen("data2", "r"); fp3 = fopen("output", "w");

• will open the files data2 and output for reading and writing respectively. • The function prototype of fopen() is FILE *fopen(const char *filename, const char * mode);

or FILE *fopen(const char *, const char *);

• fopen() returns a pointer to a file. It returns the NULL pointer if it cannot open the file. It takes in two input strings. The first string is the name of the file to be opened and the second string is the access mode, i.e. to read, write, append or a combination of them as:

Access Mode file must exist before open old file deleted file can be read file can be written file written only at end r: read

r y   y    

w   y   y  

w: write a: append

a r+ w+ a+   y         y     y y y y y y y y     y

C Library Support • The C library also supports both text and binary modes of files. – text mode – append ‘t’ to an access mode – binary mode – append ‘b’ to an access mode

• Example of text mode: “rt” (text mode is the default) • Example binary mode: “rb”, "wb", "ab", "rb+", "r+b", "wb+", "w+b", "ab+" and "a+b". The string "ab+" is equivalent to "a+b".

Steps: Continued • Step 3: do I/O using these file pointers likes fscanf(fp2, "%d", &j); fprintf(fp3, "%d %d\n", i, j); • where fscanf() and fprintf() are similar to scanf() and printf(). fscanf() takes inputs from the file pointed to by fp2 and fprintf() writes output to the file pointed to by fp3. • Step 4: It is a good practice to close an I/O file after all the inputs or outputs are done.

Closing a file • This is done by using fclose(), provided in stdio. For example, fclose(fp2); • will close the file identified by fp2. fclose() returns 0 if successful, and EOF if not. • In what circumstances can a fopen() or fclose() fail?

Illustration /* Version 2 ­ Open 2 input files to read and open one  output file to write. To execute the program, type a.out  data1 data2 output where data1 and data2 are the input  file names while output is the output file name */ #include  main(int argc, char *argv[])  { /* Step 1 */ FILE *fp1, *fp2, *fp3; int i, j; if (argc != 4)  { fprintf(stderr, "Usage: %s in1 in2 out\n", argv[0]); return(0); }  

Continued      /* Step 2 */      fp1 = fopen(argv[1], "r");      fp2 = fopen(argv[2], "r");      fp3 = fopen(argv[3], “w");      /* Step 3 */      fscanf(fp1, "%d", &i);      fscanf(fp2, "%d", &j);      fprintf(fp3, "%d %d\n", i, j);      /* Step 4 */      fclose(fp1);      fclose(fp2);      fclose(fp3);      return(0); }

Performing Read/Write Operations Functions to Functions to Access Standard Access Any Files Files printf fprintf scanf fscanf getchar getc, fgetc putchar putc, fputc gets fgets puts fputs   ungetc   fflush   fread, fwrite   ftell, fseek

I/O Operations I/O Operations on 4 Different Sizes getc, fgetc, getchar, Input / Output one putc, fputc, putchar, character ungetc I/O more than one scanf, fscanf, printf, character fprintf I/O one line of gets, fgets, puts, characters fputs I/O one block of fread, fwrite characters  

Character I/O Functions reads a character ch from the fgetc(fp) stream fp. reads a character ch from the getc(fp) stream fp. getchar() reads a character ch from stdin. writes a character ch to the fputc(ch,fp) stream fp. writes a character ch to the putc(ch, fp) stream fp. putchar(ch) writes a character ch to stdout.

Copying file data using character I/O functions #include  main(void)  {    char filename1[80], filename2[80];    FILE *fp1, *fp2;    char ch;    printf("Enter the input file name: ");    gets(filename1);    printf("Enter the output file name: ");    gets(filename2);    fp1 = fopen(filename1, "r");    fp2 = fopen(filename2, "w");

Continued    if ((fp1 == NULL) || (fp2 == NULL))  { printf("Error in opening the files\n"); exit(1); }    while ((ch = fgetc(fp1)) != EOF)   {   

fputc(ch, fp2);

  

putchar(ch);

  }   fclose(fp1);    fclose(fp2);   return 0; }

String I/O Functions Function Description

Remarks on the newline and null character

reads string from a file and fgets(buf, stores it into buf from max, fp) stream fp. At most max-1 characters are read.

retains newline character and adds null character.

reads characters from stdin and places them into buf.

replaces newline character with null character.

fputs(buf, writes the string pointed to fp) by buf to the stream fp.

does not add null character and newline character.

gets(buf)

puts(buf)

writes the string pointed to by buf to stdout.

adds newline character, does not add null character.

Copying file data using string I/O functions #include  #define MAX_SIZE 80 main(void) {    char filename1[80], filename2[80];    FILE *fp1, *fp2;    char line[MAX_SIZE];    printf("Enter the input file name: ");    gets(filename1);    printf("Enter the output file name: ");    gets(filename2);    fp1 = fopen(filename1, "r");    fp2 = fopen(filename2, "w");

Continued    if ((fp1 == NULL) || (fp2 == NULL)) { printf("Error in opening the files\n"); exit(1); }    

while(fgets(line, MAX_SIZE, fp1)!= NULL)  {    fputs(line, fp2); puts(line);

   }    fclose(fp1);  fclose(fp2); return 0; }

Copying file data using formatted file I/O functions #include  #include  /* needed for exit() */ main(void)  {    FILE *fp1, *fp2;    int i,j,k;    float num1=1.0, num2;    if ((fp1 = fopen("input", "w")) == NULL)  { fprintf(stderr, "Error in opening the  exit(1);    }    for (i=0; i