1 / 7

File Handling

File Handling. Summer 2014 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University. Your vision? Seek with all your heart?. Unit Learning Objectives. Explain the concept pf data files Explain different types of files. Types of Files. Three categories of files

hue
Download Presentation

File Handling

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 Handling Summer 2014 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University

  2. Your vision? Seek with all your heart? Unit Learning Objectives • Explain the concept pf data files • Explain different types of files Code Optimization

  3. Types of Files • Three categories of files • Text files • Formatted files • Binary files Introduction

  4. Common Functions • Define a File pointer • FILE *fp • Open a File • Fopen(“filename, “mode” • R, w, a, r+, w+, a+ • Close a File • Fclose(fp) • Perform operations • Fscanf() • fprintf() • Fgetc() • Fputc() Introduction

  5. Create a file #include <stdio.h> main() { FILE *fp; fp= fopen(“test.txt", "w+"); fprintf(fp, "This is testing for fprintf...\n"); fputs("This is testing for fputs...\n", fp); fputc(ch, fp); fclose(fp); } Introduction

  6. Read a file int main () { FILE *fp; int c; fp= fopen("file.txt","r"); while(1) { c = fgetc(fp); if( feof(fp) ) { break ; } printf("%c", c); } fclose(fp); return(0); } Introduction

  7. Merging command line arguments /* fgetsexmaple */ #include <stdio.h> intmain(intarg, char *argv[]) { FILE * pFile; char string [BUFSIZ]; pFile = fopen (argv[1] , "r"); if (pFile == NULL) perror ("Error opening file"); else { while (fgets (string , BUFSIZ, pFile) != NULL) { puts (string); } fclose (pFile); } return 0; } Introduction

More Related