13
CHAPTER 10 CHAPTER 10 C FILE PROCESSING C FILE PROCESSING Copyright © 2002 Harcourt, Inc. All rights reserved

Chapter 10-C FILE PROCESSING

Embed Size (px)

DESCRIPTION

CHAPTER 10, COMPUTER PROGRAMMING (DAE 20102)

Citation preview

Page 1: Chapter 10-C FILE PROCESSING

CHAPTER 10CHAPTER 10

C FILE PROCESSINGC FILE PROCESSING

Copyright © 2002 Harcourt, Inc. All rights reserved

Page 2: Chapter 10-C FILE PROCESSING

Copyright © 2002 Harcourt, Inc. All rights reserved.

INTRODUCTION

C can process two kinds of files : Text files and binary files In this chapter we will use the standard input/output and program-controlled of text In C, file information is stored in a structure To open a file need to use a pointer. This pointer is called file pointer File pointer = is a pointer to information that defines various characteristics of an opened file. The declaration of file pointer is:

FILE *fptr;

Page 3: Chapter 10-C FILE PROCESSING

Copyright © 2002 Harcourt, Inc. All rights reserved.

OPENING A FILE : fopen()

We use fopen() function to open a specified file Example:

FILE *fptr;fptr = fopen(“test.txt”,”w”);

Declare a pointer variable

Open the text.txt file

File Modes: Mode Description r Open a file for reading. The file must already existw Create a file for writing. If the file already exists, discard the current contents. If does not exist, one will be createda Open a text file for appending. Data will be added to the end of

the an existing file. If it does not exist, one will be createdr+ Open a file for update (reading and writing) w+ Create a file for update. If the file already exists, discard the current contents. a+ Append; open or create a file for update writing is done at the END of the file.

Page 4: Chapter 10-C FILE PROCESSING

Copyright © 2002 Harcourt, Inc. All rights reserved.

stdin, stdout, and stderr

• Every C program has three files opened for them at start-up: stdin, stdout, and stderr

• stdin is opened for reading, while stdout and stderr are opened for writing

• They can be used wherever a FILE * can be used.

• Examples:• fprintf(stdout, "Hello there!\n");

• This is the same as printf("Hello there!\n");• fscanf(stdin, "%d", &int_var);

• This is the same as scanf("%d", &int_var);• fprintf(stderr, "An error has occurred!\n");

• This is useful to report errors to standard error - it flushes output as well, so this is really good for debugging!

Page 5: Chapter 10-C FILE PROCESSING

Copyright © 2002 Harcourt, Inc. All rights reserved.

Four Ways to Read and Write Files

Formatted file I/OGet and put a characterGet and put a stringBlock read and write

Page 6: Chapter 10-C FILE PROCESSING

Copyright © 2002 Harcourt, Inc. All rights reserved.

Formatted File I/O• Formatted File input is done through fscanf:

• int fscanf (FILE * fptr, const char * fmt, ...) ;

• Formatted File output is done through fprintf:• int fprintf(FILE *fptr, const char *fmt, …);

{FILE *fptr1, *fptr2;int n;fptr1 = fopen("file1", "r");fptr2 = fopen("file2", "w");fscanf(fptr1, "%d", &n);fprintf(fptr2, "%d", n);fclose(fptr1);fclose(fptr2);

}

Page 7: Chapter 10-C FILE PROCESSING

Copyright © 2002 Harcourt, Inc. All rights reserved.

Get and Put a Character

#include <stdio.h>int fgetc(FILE * fptr);int fputc(int c, FILE * fptr);• These two functions read or write a

single byte from or to a file.• fgetc returns the character that was

read, converted to an integer.• fputc returns the same value of

parameter c if it succeeds, otherwise, return EOF.

Page 8: Chapter 10-C FILE PROCESSING

Copyright © 2002 Harcourt, Inc. All rights reserved.

CHARACTER OUTPUT: putc()

Function putc() is use to write a character to an already opened file test.text Example:

putc (‘A’,fptr); Coding:

char ch = ‘A’;do {

putc(ch,fptr);}while (ch++ !=z);

Start with the letter A

A do-while loop

Write the character to the file

Stop after letter Z

Page 9: Chapter 10-C FILE PROCESSING

Copyright © 2002 Harcourt, Inc. All rights reserved.

CHARACTER OUTPUT: getc()

By using the function getc(). We can read a character from our already opened file test.txt.

Coding:do {

ch = getc(fptr);

} while (ch!=EOF);

A do-while loop

Read a character from the file

Until end of file is reached

Page 10: Chapter 10-C FILE PROCESSING

Copyright © 2002 Harcourt, Inc. All rights reserved.

Get and Put a String

#include <stdio.h>char *fgets(char *s, int n, FILE * fptr);int fputs(char *s, FILE * fptr);• fputs() = writes a string to a specified file. • It does not write the ‘\0’ character at the end

of the stringe.g: fputs(“This is a string”,fptr);

• fgets() = reads a string from a specified file. • It reads until a new line character is read or

the length of the string is less than the specified maximum number of characters

e.g:fgets(string,80,fptr)

Page 11: Chapter 10-C FILE PROCESSING

Copyright © 2002 Harcourt, Inc. All rights reserved.

Closing and Flushing Files

• Syntax:int fclose (FILE * fptr) ;

• closes fp -- returns 0 if it works -1 if it fails

• You can clear a buffer without closing itint fflush (FILE * fptr) ;

• Essentially this is a force to disk.

• Very useful when debugging.

• Without fclose or fflush, your updates to a file may not be written to the file on disk.

Page 12: Chapter 10-C FILE PROCESSING

Copyright © 2002 Harcourt, Inc. All rights reserved.

Example Program 1#include <stdio.h> int main() {

int account; char name[ 30 ]; double balance; FILE *cfPtr; /* cfPtr = clients.dat file pointer */

if ( ( cfPtr = fopen( "clients.dat", "w" ) ) == NULL ) printf( "File could not be opened\n" );

else {

printf( "Enter the account, name, and balance.\n" ); printf( "Enter EOF (Ctrl-d) to end input.\n" ); printf( "? " ); scanf( "%d%s%lf", &account, name, &balance ); while ( !feof( stdin ) )

{ fprintf( cfPtr, "%d %s %.2f\n", account, name, balance ); printf( "? " ); scanf( "%d%s%lf", &account, name, &balance );

} fclose( cfPtr ); } return 0;

}

Page 13: Chapter 10-C FILE PROCESSING

Copyright © 2002 Harcourt, Inc. All rights reserved.

Example Program 2#include <stdio.h> int main() {

int account; char name[ 30 ]; double balance; FILE *cfPtr; /* cfPtr = clients.dat file pointer */

if ( ( cfPtr = fopen( "clients.dat", "r" ) ) == NULL ) printf( "File could not be opened\n" );

else {

printf( "%-10s%-13s%s\n", "Account", "Name", "Balance" ); fscanf( cfPtr, "%d%s%lf", &account, name, &balance );

while ( !feof( cfPtr ) ) {

printf( "%-10d%-13s%7.2f\n", account, name, balance ); fscanf( cfPtr, "%d%s%lf", &account, name, &balance );

} fclose( cfPtr ); } return 0;

}