Вы находитесь на странице: 1из 18

Filing

File processing consists of creating, storing, and/or retrieving, the contents of a file to or from a recognizable medium. For example, it is used to save word processed files to a hard drive, to store a presentation on floppy disk, or to open a file from a CD-ROM. FILE is a structure and it is defined in the stdio.h header file. It is equipped with variables used to indicate what operation would be performed. To use this structure, first declare an instance of the FILE structure.

Slide 1

Filing
FILE *f; After instantiating this structure, defining what to do with it becomes easy, using one of the provided functions. The functions used to perform its related operations on files are also declared in the stdio.h header file.

Slide 2

Fopen

Opening a file with the fopen function creates a new stream and establishes a connection between the stream and a file. This function takes two arguments - the first one is the name with path. The second argument is mode and determines how the file is to be opened. fopen returns a FILE pointer if the file was opened successfully, else it returns NULL.
Slide 3

Modes
"r" "w" "a" Open a file for reading. The file must exist. Create an empty file for writing. If a file with the same name already exists its content is erased. Append to a file. Writing operations append data at the end of the file. The file is created if it doesn't exist.

"r+"
"w+"

Open a file for reading and writing. The file must exist.
Create an empty file for reading and writing. If a file with the same name already exists its content is erased before it is opened.

Open a file for reading and appending. All writing operations are done at the end of the file protecting the previous content. You can reposition (fseek, rewind) the "a+" pointer to anywhere in the file for reading, but writing operations will move back to the end of file. The file is created if it doesn't exist.

Slide 4

Fclose
When file processing is finished, it's best to close the file. Simply pass it a FILE pointer, but be warned not to pass a NULL pointer (it points to nothing), the program might crash. It closes the file associated with the specified stream after flushing all buffers associated with it.

Slide 5

#include <stdio.h> int main() { FILE *file; /* declare a FILE pointer */ file = fopen("hello.txt", "r"); /* open a text file for reading, hello.txt should exist */ if(file==NULL) { cout<<"Error: can't open file.\n"; return 1; } else { cout<<"File opened. Now closing it...\n"; fclose(file); return 0; } }
Slide 6

Fread
fread takes four arguments: The first is a char array. The second argument is the size of char, i.e. 1. The third argument in the number of elements to read. The last argument is the FILE pointer. fread returns the number of characters read if a char array is passed to it.

Slide 7

#include <stdio.h> int main() { FILE *file; char c[30]; /* make sure it is large enough to hold all the data! */ int n; file = fopen("numbers.txt", "r"); if(file==NULL) { cout<<Error: can't open file.\n"; return 1; }
Slide 8

else { cout<<File opened successfully.\n"; n = fread(c, 1, 10, file); /* passing a char array, reading 10 characters */ c[n] = '\0'; /* a char array with the null char at the end */ printf(%s,c); /* print out the string */ cout<<"Characters read<<n; fclose(file); /* to read the file from the beginning, */ /* we need to close and reopen the file return 0; } }
Slide 9

Fwrite
fwrite takes four arguments: The first is a char array. The second argument is the size of char, i.e. 1. The third argument in the number of elements to write. The last argument is the FILE pointer. fwrite returns the number of characters written if a char array is passed to it.

Slide 10

#include <stdio.h> void main( void ) { FILE *stream; char list[30]; int i, numread, numwritten; /* Open file in text mode: */ if( (stream = fopen( "fread.txt", "w+" )) != NULL ) { for ( i = 0; i < 25; i++ ) list[i] = (char)('z' - i); /* Write 25 characters to stream */ numwritten = fwrite( list, 1, 25, stream ); cout<<"Wrote<< numwritten<<items\n; fclose( stream ); } else cout<<"Problem opening the file\n" ;
Slide 11

if( (stream = fopen( "fread.txt", "r+" )) != NULL ) { /* Attempt to read in 25 characters */ numread = fread( list, 1, 25, stream ); cout<<"Number of items read =<<numread ; printf("Contents of buffer %s,list); fclose( stream ); } else cout<< "File could not be opened\n"; }

Slide 12

#include <stdio.h> int main() { FILE *sourceFile; FILE *destinationFile; char *buffer; int n; sourceFile = fopen("file1.txt", "r"); destinationFile = fopen("file2.txt", "w"); if(sourceFile==NULL) { cout<<Error: can't access file.txt\n"; return 1; } else if(destinationFile==NULL) { cout<<"Error: can't create file for writing.\n"; return 1; } Slide 13

else { n = fread(buffer, 1, 1000, sourceFile); /* grab all the text */ fwrite(buffer, 1, n, destinationFile); /* put it in file2.txt */ fclose(sourceFile); fclose(destinationFile); destinationFile = fopen("file2.txt", "r"); n = fread(buffer, 1, 1000, destinationFile); /* read file2.txt */ printf(%s,buffer); /* display it all */ fclose(destinationFile); return 0; } }
Slide 14

Supported Functions

fgetc function to read one character at a time. c = fgetc(file) fgets function to read 1 line at a time. fgets(c, 10, file) fputc function for writing one character at a time. fputc(sentence[i], file)

Slide 15

Contd..
fputs function for writing one line at a time. fputc(sentence, file) rewind takes the file pointer to the beginning of the file. rewind(file) The fseek function moves the file pointer (if any) associated with stream to a new location that is offset bytes from origin. fseek(file,offset,origin)

Slide 16

Contd..
ftell to determine the current location of a file. long=ftell(file) fflush causes file disk buffer to be flushed. fflush(file) fprintf function to write output. fprintf(file,string) fscanf function to read input. fscanf(file,string);

Slide 17

Contd..

Constants defined in stdio.h SEEK_CUR

Current position of file pointer. End of file.

SEEK_END

SEEK_SET

Beginning of file.

Slide 18

Вам также может понравиться