1 / 10

File Input/Output

File Input/Output. 2014/10/07. What you will learn?. Create Open Close. File. Sequence of bytes – binary and text Opening a file - FILE * fopen ( const char * filename, const char * mode);. Modes. " rb ", " wb ", "ab", " rb +", " r+b ", " wb +", " w+b ", "ab+", " a+b ".

Download Presentation

File Input/Output

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. File Input/Output 2014/10/07

  2. What you will learn? • Create • Open • Close

  3. File • Sequence of bytes – binary and text • Opening a file - FILE *fopen(const char * filename, const char * mode);

  4. Modes "rb", "wb", "ab", "rb+", "r+b", "wb+", "w+b", "ab+", "a+b"

  5. Close intfclose( FILE *fp ); Returns 0 on Successs EOF for an error

  6. Writing to a File intfputc( int c, FILE *fp ); intfputs( const char *s, FILE *fp );

  7. Sample program #include <stdio.h> main() { FILE *fp; fp = fopen("/tmp/test.txt", "w+"); fprintf(fp, "This is testing for fprintf...\n"); fputs("This is testing for fputs...\n", fp); fclose(fp); }

  8. Reading from a File intfgetc( FILE * fp ); char *fgets( char *buf, int n, FILE *fp );

  9. Sample Program #include <stdio.h> main() { FILE *fp; char buff[255]; fp = fopen("/tmp/test.txt", "r"); fscanf(fp, "%s", buff); printf("1 : %s\n", buff ); fgets(buff, 255, (FILE*)fp); printf("2: %s\n", buff ); fgets(buff, 255, (FILE*)fp); printf("3: %s\n", buff ); fclose(fp); }

  10. Binary I/O size_tfread(void *ptr, size_tsize_of_elements, size_tnumber_of_elements, FILE *a_file); size_tfwrite(const void *ptr, size_tsize_of_elements, size_tnumber_of_elements, FILE *a_file);

More Related