File I/O
2File Input/Output Input/Output device for programs– Input: Keyboard– Output: Monitor Is it possible to read or write da...
3File Input/Ooutput File Input/Output ProcessOpen the fileRead data from fileWrite data to fileClose the filefopenfscanf,...
4File Operation Function fopen() fopen()– Used to prepare external files to use in programs• All files are associated wit...
5File Operation Function fopen() Example– Open my_file.txt in the read mode– Return NULL at failure– Eg) If a file named ...
6File Operation Function fopen() Example– Open my_file.txt in the write mode– If my_file.txt does not exist, it is create...
7File Operation Function fopen() Example– Open my_file.txt in the append mode (similar to write mode)– If my_file.txt doe...
8File Operation Function fopen() Example– Open my_file.txt in the read/write mode– Return NULL, if my_file.txt does not e...
9File Operation Function fopen() Example– Open my_file.txt in the read/write mode– If my_file.txt does not exist, it is c...
10File Operation Function fopen() Example– Open my_file.txt in the read/write mode– If my_file.txt does not exist, it is ...
11File pointer File pointer– File I/O is processed through file pointer ‘FILE’ Type a type of structure defined with ty...
12File Operation Function fclose() fclose()– Close the file opened by fopen fclose() Syntaxint fclose( FILE *stream );fi...
13File Operation Function fopen() fclose() Example#include <stdio.h>void main() {FILE *fp ;fp = fopen(“my_file.txt”, “r”)...
14Formatted I/O Function fprintf() fprintf()– printf() for file operation– First argument is the file pointer which has o...
15Formatted I/O Function fprintf() fscanf()– scanf() for file operation– First argument is file pointer which has one of ...
16feof() Function feof()– Check whether the file pointer is at the end of file or notint feof( FILE *stream );Opened file...
17Formatted I/O Function fprintf() and fscanf()#include <stdio.h>void main() {int i, j, k ;FILE *ifp, *ofp ;ifp = fopen("a...
18Formatted I/O Function fprintf() andfscanf()#include <stdio.h>void main() {int i, j, k ;FILE *ifp, *ofp ;ifp = fopen("ab...
19Formatted I/O Function fprintf() andfscanf()#include <stdio.h>void main() {int i, j, k ;FILE *ifp, *ofp ;ifp = fopen("ab...
20Formatted I/O Function fprintf() andfscanf()#include <stdio.h>void main() {char c ;FILE *ifp, *ofp ;ifp = fopen("abc.txt...
21Misc. File I/O Functions fgetc() , getc()– Read a character from a file• If there is no more character to read, return ...
22Misc. File I/O Functions fputc() , putc()– Write a character to a file– fputc() is identical to putc()char c ;FILE *fp ...
23Misc. File I/O Functions fputs() : Writing a string– puts() for file I/O– Print a string into file– At success, it retu...
24Misc. File I/O FunctionsFILE *fp;int i;char *data[3]={"to ben","or notn","to ben"};fp = fopen("abc.txt", "w");for(i = 0;...
25Misc. File I/O Functions fgets() : Reading a String– gets() for file I/O– Read (num -1) characters from fp and store to...
26Misc. File I/O Functions fgets()char s[5] ;FILE* fp = fopen("abc.txt", "r" ) ;fgets( s, 5, fp ) ;1231 21234567
27Misc. File I/O Functions Difference between gets and fgets– gets: read before ‘n’. Not store ‘n’– fgets: read before ‘n...
28Accessing a File Randomly How to read the file again?char c ;FILE *fp = fopen("abc.txt", "r" ) ;while( !foef(fp) ) {fsc...
29Accessing a File Randomly rewind()– Move file position indicatorto the beginning of the filevoid rewind( FILE* ) ;char ...
30Accessing a File Randomly fseek()– Move file position indicator to offset from place– Value of place0 (starting point o...
31Accessing a File Randomly fseek()fseek( file_ptr, offset, place);0 (SEEK_SET)1 (SEEK_CUR)2 (SEEK_END)0123456789abcdefgh...
32Accessing a File Randomly ftell()– Return the current value of file position indicator– Returned value is the number of...
33Accessing a File Randomly ftell()char c ;int pos ;FILE *fp = fopen("abc.txt", “r” ) ;while( !foef(fp) ) {fscanf( fp, "%...
34stdin, stdout & stderr Print output with fprintf– stdout is a file pointer for monitor Read from keyboard with fscanf–...
35stdin, stdout & stderrStandard C files in stdio.hWritten in C Name Remarkstdin standard input file connected to the keyb...
36stdin, stdout & stderr#include <stdio.h>void main() {int k, j ;fscanf( stdin, "%d %d", &k, &j ) ;fprintf( stdout, "Sum: ...
Upcoming SlideShare
Loading in...5
×

14. fiile io

467

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
467
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
13
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

14. fiile io

  1. 1. File I/O
  2. 2. 2File Input/Output Input/Output device for programs– Input: Keyboard– Output: Monitor Is it possible to read or write data from/to files?A B C1 80 90 702 80 60 403 60 50 70…C programA B C Avg.1 80 90 70 802 80 60 40 603 60 50 70 60…read writefile1.txt file2.txt
  3. 3. 3File Input/Ooutput File Input/Output ProcessOpen the fileRead data from fileWrite data to fileClose the filefopenfscanf, fprintf, fgets, fputs, …fclose
  4. 4. 4File Operation Function fopen() fopen()– Used to prepare external files to use in programs• All files are associated with a data structure known as a streamif fopen successes– Return value: a pointer of FILE structure• File stream is manipulated with FILE pointer returned by fopen fopen() SyntaxFILE *fopen( const char *filename, const char *mode );Return by file pointer Filename in disk File open mode
  5. 5. 5File Operation Function fopen() Example– Open my_file.txt in the read mode– Return NULL at failure– Eg) If a file named “my_file.txt” does not existFILE *fp ;fp = fopen(“my_file.txt”, “r”);
  6. 6. 6File Operation Function fopen() Example– Open my_file.txt in the write mode– If my_file.txt does not exist, it is created– If my_file.txt exists, the content of the file is erasedFILE *fp ;fp = fopen(“my_file.txt”, “w”);
  7. 7. 7File Operation Function fopen() Example– Open my_file.txt in the append mode (similar to write mode)– If my_file.txt does not exist, it is created– If my_file.txt exists, the file content is kept and new data isappended at the end of fileFILE *fp ;fp = fopen(“my_file.txt”, “a”);
  8. 8. 8File Operation Function fopen() Example– Open my_file.txt in the read/write mode– Return NULL, if my_file.txt does not exist– If my_file.txt exists, the file content is kept– Read/write pointer is positioned at the beginning of the fileFILE *fp ;fp = fopen(“my_file.txt”, “r+”);
  9. 9. 9File Operation Function fopen() Example– Open my_file.txt in the read/write mode– If my_file.txt does not exist, it is created– If my_file.txt exists, the file content is erasedFILE *fp ;fp = fopen(“my_file.txt”, “w+”);
  10. 10. 10File Operation Function fopen() Example– Open my_file.txt in the read/write mode– If my_file.txt does not exist, it is created– If my_file.txt exists, the file content is kept– Read/write pointer is positioned at the end of the fileFILE *fp ;fp = fopen(“my_file.txt”, “a+”);
  11. 11. 11File pointer File pointer– File I/O is processed through file pointer ‘FILE’ Type a type of structure defined with typedef in stdio.h containsinformation of file[Ex]FILE *fp; /* declare ‘fp’ asfile pointer */
  12. 12. 12File Operation Function fclose() fclose()– Close the file opened by fopen fclose() Syntaxint fclose( FILE *stream );file pointer returned by fopenReturns 0 : successfulEOF : error(EOF is defined as –1 in stdio.h)
  13. 13. 13File Operation Function fopen() fclose() Example#include <stdio.h>void main() {FILE *fp ;fp = fopen(“my_file.txt”, “r”);...fclose( fp ) ;}
  14. 14. 14Formatted I/O Function fprintf() fprintf()– printf() for file operation– First argument is the file pointer which has one of writemodes (w, a, r+, w+, a+)– The rest arguments are the same as printf()– fprintf() writes data into abc.txtFILE* fp = fopen( “abc.txt”, “w” ) ;fprintf( fp, “%d %dn”, 1, 2 );
  15. 15. 15Formatted I/O Function fprintf() fscanf()– scanf() for file operation– First argument is file pointer which has one of read modes(r, r+, w+, a+)– The rest arguments are the same as printf()– scanf() reads data from abc.txtint i, j ;FILE* fp = fopen( “abc.txt”, “r” ) ;fscanf( fp, “%d %d”, &i, &j );
  16. 16. 16feof() Function feof()– Check whether the file pointer is at the end of file or notint feof( FILE *stream );Opened file pointerReturns none-zero : if it is EOF0 : if it is not EOF
  17. 17. 17Formatted I/O Function fprintf() and fscanf()#include <stdio.h>void main() {int i, j, k ;FILE *ifp, *ofp ;ifp = fopen("abc.txt", "r" ) ;ofp = fopen("cal.txt", "w" ) ;fscanf( ifp, "%d %d %d", &i, &j, &k ) ;fprintf( ofp, "sum: %dn", i + j + k ) ;fclose( ifp ) ;fclose( ofp ) ;} fprintf(), fscanf() Example1 2 3abc.txtcal.txt is not exist
  18. 18. 18Formatted I/O Function fprintf() andfscanf()#include <stdio.h>void main() {int i, j, k ;FILE *ifp, *ofp ;ifp = fopen("abc.txt", "r" ) ;ofp = fopen("cal.txt", "w" ) ;fscanf( ifp, "%d %d %d", &i, &j, &k ) ;fprintf( ofp, "sum: %dn", i + j + k ) ;fclose( ifp ) ;fclose( ofp ) ;} fprintf(), fscanf() Example1 2 3abc.txtsum: 6cal.txt
  19. 19. 19Formatted I/O Function fprintf() andfscanf()#include <stdio.h>void main() {int i, j, k ;FILE *ifp, *ofp ;ifp = fopen("abc.txt", "r" ) ;ofp = fopen("cal.txt", "a" ) ;fscanf( ifp, "%d %d %d", &i, &j, &k ) ;fprintf( ofp, "sum: %dn", i + j + k ) ;fclose( ifp ) ;fclose( ofp ) ;} fprintf(), fscanf() Example1 2 3abc.txtsum: 6cal.txt
  20. 20. 20Formatted I/O Function fprintf() andfscanf()#include <stdio.h>void main() {char c ;FILE *ifp, *ofp ;ifp = fopen("abc.txt", "r" ) ;ofp = fopen("abc2.txt", "a" ) ;while( feof(ifp) == 0 ) {fscanf( ifp, "%c", &c ) ;fprintf( ofp, "%c", c ) ;}fclose( ifp ) ;fclose( ofp ) ;} fprintf(), fscanf() ExampleThis is afile.1 2 3abc.txtThis is afile.1 2 3abc2.txt
  21. 21. 21Misc. File I/O Functions fgetc() , getc()– Read a character from a file• If there is no more character to read, return EOF– fgetc() is equivalent to getc()char c ;FILE *fp = fopen("abc.txt", "r" ) ;while( (c = fgetc(fp)) != EOF )printf("%c", c ) ;int fgetc ( FILE *stream ) ;
  22. 22. 22Misc. File I/O Functions fputc() , putc()– Write a character to a file– fputc() is identical to putc()char c ;FILE *fp = fopen("abc.txt", "r" ) ;FILE *ofp = fopen("xyz.txt", "w" ) ;while( (c = fgetc(fp)) != EOF )fputc( c, ofp ) ;int fputc ( int c, FILE *stream ) ;
  23. 23. 23Misc. File I/O Functions fputs() : Writing a string– puts() for file I/O– Print a string into file– At success, it returns a non-negative value– At failure, it returns EOFint fputs(char *str, FILE *fp);
  24. 24. 24Misc. File I/O FunctionsFILE *fp;int i;char *data[3]={"to ben","or notn","to ben"};fp = fopen("abc.txt", "w");for(i = 0; i<3; i++) fputs(data[i],fp);fclose( fp );
  25. 25. 25Misc. File I/O Functions fgets() : Reading a String– gets() for file I/O– Read (num -1) characters from fp and store to str– When fp meets ‘n’ or EOF, it stops reading and store NULLinto str– At success, it returns the address of str. Otherwise, itreturns NULLchar *fgets(char *str, int num, FILE *fp);
  26. 26. 26Misc. File I/O Functions fgets()char s[5] ;FILE* fp = fopen("abc.txt", "r" ) ;fgets( s, 5, fp ) ;1231 21234567
  27. 27. 27Misc. File I/O Functions Difference between gets and fgets– gets: read before ‘n’. Not store ‘n’– fgets: read before ‘n’. Store also ‘n’
  28. 28. 28Accessing a File Randomly How to read the file again?char c ;FILE *fp = fopen("abc.txt", "r" ) ;while( !foef(fp) ) {fscanf( fp, "%c", &c ) ;printf("%c", c ) ;}while( !foef(fp) ) {fscanf( fp, "%c", &c ) ;printf("%c", c ) ;}abcdefghiabc.txt
  29. 29. 29Accessing a File Randomly rewind()– Move file position indicatorto the beginning of the filevoid rewind( FILE* ) ;char c ;FILE *fp = fopen("abc.txt", “r” ) ;while( !foef(fp) ) {fscanf( fp, "%c", &c ) ;printf("%c", c ) ;}rewind( fp ) ;while( !foef(fp) ) {fscanf( fp, "%c", &c ) ;printf("%c", c ) ;}abcdefghiabc.txt
  30. 30. 30Accessing a File Randomly fseek()– Move file position indicator to offset from place– Value of place0 (starting point of the file)1 (current position)2 (end of the file)fseek( file_ptr, offset, place);
  31. 31. 31Accessing a File Randomly fseek()fseek( file_ptr, offset, place);0 (SEEK_SET)1 (SEEK_CUR)2 (SEEK_END)0123456789abcdefghjkabc.txtFILE *fp = fopen("abc.txt", “r” ) ;fseek(fp, 3, SEEK_SET) ;fseek(fp, 5, SEEK_CUR) ;fseek(fp, -10, SEEK_END) ;
  32. 32. 32Accessing a File Randomly ftell()– Return the current value of file position indicator– Returned value is the number of bytes from the beginning ofthe file– Value of indicator is increased by one when it read acharacterint ftell( FILE* );
  33. 33. 33Accessing a File Randomly ftell()char c ;int pos ;FILE *fp = fopen("abc.txt", “r” ) ;while( !foef(fp) ) {fscanf( fp, "%c", &c ) ;printf("%d: %cn", ftell(fp), c ) ;}abcdefghiabc.txt
  34. 34. 34stdin, stdout & stderr Print output with fprintf– stdout is a file pointer for monitor Read from keyboard with fscanf– Stdin is a file pointer for keyboardprintf( “This is a testn” ) ; fprintf( stdout, “This is a testn” ) ;scanf( “%d”, &k ) ; fscanf( stdin, “%d”, &k ) ;
  35. 35. 35stdin, stdout & stderrStandard C files in stdio.hWritten in C Name Remarkstdin standard input file connected to the keyboardstdout standard output file connected to the screenstderr standard error file connected to the screen Three kinds of file pointer in stdio.h
  36. 36. 36stdin, stdout & stderr#include <stdio.h>void main() {int k, j ;fscanf( stdin, "%d %d", &k, &j ) ;fprintf( stdout, "Sum: %dn", k + j ) ;fprintf( stderr, "OKn") ;}
  1. Gostou de algum slide específico?

    Recortar slides é uma maneira fácil de colecionar informações para acessar mais tarde.

×