C Programming Lecture 12 : File Processing

Slides:



Advertisements
Similar presentations
Files in C Rohit Khokher. Files in C Real life situations involve large volume of data and in such cases, the console oriented I/O operations pose two.
Advertisements

Summer 2014 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University C: Formatted Files.
Text File Input and Output. Overview Text File Buffered I/O Functions fopen Function Demo of File Write and Read.
CSCI 171 Presentation 12 Files. Working with files File Streams – sequence of data that is connected with a specific file –Text Stream – Made up of lines.
File I/O.
 Types of files  Command line arguments  File input and output functions  Binary files  Random access.
An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
N305: C Programming Copyright ©2006  Department of Computer & Information Science File Handling in C.
Lone Leth Thomsen Input / Output and Files. April 2006Basis-C-8/LL2 sprintf() and sscanf() The functions sprintf() and sscanf() are string versions of.
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
1 Lecture09: Global Variables, File I/O, and Variable-Length Arguments 5/16/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
22. FILE INPUT/OUTPUT. File Pointers and Streams Declarations of functions that perform file I/O appear in. Each function requires a file pointer as a.
File Handling In C By - AJAY SHARMA. We will learn about- FFile/Stream TText vs Binary Files FFILE Structure DDisk I/O function OOperations.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
1 Lecture09: File I/O 5/6/2013 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Characters and Strings File Processing Exercise C Programming:Part 3.
Text and Binary File Processing 程式設計 潘仁義 CCU COMM.
Introduction to Programming Using C Files. 2 Contents Files Working with files Sequential files Records.
File IO and command line input CSE 2451 Rong Shi.
Chapter 8 File-Oriented Input and Output. 8.1 INTRODUCTION a file can also be designed to store data. We can easily update files, A data file as input.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 4.
ECE 103 Engineering Programming Chapter 44 File I/O Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material developed.
1 Lecture09: File I/O 11/19/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Introduction to Systems Programming (CS 0449) C Preprocessing Makefile File I/O.
C Program Design C File Processing 主講人:虞台文. Content Introduction Data Hierarchy Files and Streams Creating a Sequential-Access File Reading Data from.
FILE IO in ‘C’ by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Mobility Research Lab mobility.ceng.metu.edu.tr Applied Innovative Interdisciplinary (AI2) Research Lab Short Course on Programming in C/C++
C File Processing. Objectives To be able to create, write and update files. To become familiar with sequential access file processing. To become familiar.
Structured Programming Approach Module VIII - Additional C Data Types File Handling Prof: Muhammed Salman Shamsi.
Chapter 12 Files (reference: Deitel ’ s chap 11) chap8.
1 CSC103: Introduction to Computer and Programming Lecture No 28.
Gramming An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
Adv. UNIX:fp/101 Advanced UNIX v Objectives of these slides: –a more detailed look at file processing in C Special Topics in Comp. Eng.
 2007 Pearson Education, Inc. All rights reserved. 1 C File Processing.
FILES IN C. File Operations  Creation of a new file  Opening an existing file  Reading from a file  Writing to a file  Moving to a specific location.
Files. FILE * u In C, we use a FILE * data type to access files. u FILE * is defined in /usr/include/stdio.h u An example: #include int main() { FILE.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
Advanced Programming in the UNIX Environment Hop Lee.
Silberschatz and Galvin  C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education.
Lecture 12 CIS 208 Friday, March 3rd , 2005.
External Files: Abstractly, a file can be thought of as a stream of data (either char or binary). C has two groups of files: standard files, such as stdin,
Chapter 4 File Processing
Lecture 11 File input/output
TMF1414 Introduction to Programming
Introduction to Computer Programming Lecture 18 Binary Files
File I/O.
Session #5 File I/O Bit Masks, Fields & Bit Manipulations
CGS 3460, Lecture 41 Apr 21, 2006 Hen-I Yang
File Handling in C.
Input/Output (Continued)
CSC215 Lecture Input and Output.
CS111 Computer Programming
What you need for the 1st phase of project
File Handling in C.
Introduction to Programming
Introduction to Programming
Programming and Data Structures
File I/O We are used to reading from and writing to the terminal:
Lecture 15 Files.
CSC215 Lecture Input and Output.
CSC215 Lecture Input and Output.
Text and Binary File Processing
File Handling in C.
Henning Schulzrinne Columbia University
C Preprocessing File I/O
ETE 132 Lecture 8 By Munirul Haque.
CSc 352 File I/O Saumya Debray Dept. of Computer Science
I/O CS580U - Fall 2018.
File I/O We are used to reading from and writing to the terminal:
Programming Fundamental
Presentation transcript:

C Programming Lecture 12 : File Processing

File File Data are stored as a form of consecutive bytes n-1 1 2 3 … …

File Types Text File Binary File Storing characters with ascii codes Storing binary numbers as a consecutive bytes

Handling Files Declare FILE type variable Open a file Use standard library : #include <stdio.h> ex) FILE* fp; Open a file Connect FILE type variable with actual file using fopen function ex) fp=fopen(“test.txt”,”r”); Perform I/O with the open file Text file : fscanf(), fprintf(), fgets(), fputs(), … Binary file : fread(), fwrite() Close a file Break the connection between FILE type variable and actual file ex) fclose(fp);

File Open Before using a file, you must open the file fopen() FILE *fopen(const char *filename, const char *mode); Example 1 FILE *fp; fp = fopen("c:\work\text.txt", "r"); if (fp == NULL) {     printf(“file open error!\n"); } Example 2 fp = fopen("outdata.txt", "w"); fp = fopen("outdata.txt", "a");

File Open Modes r or rb w or wb a or ab r+ or rb+ or r+b Open file for reading. w or wb Truncate to zero length or create file for writing. a or ab Append; open or create file for writing at end-of-file. r+ or rb+ or r+b Open file for update (reading and writing). b stands for “binary”

Text File Processing char* fgets(char *s, int n, FILE *fp); int fputs(const char *s, FILE *fp); int fprintf(FILE *fp, const char *format, ...); int fscanf(FILE *fp, const char *format, ...);

deleteLine.c #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { FILE *fp1; FILE *fp2; char buffer[100]; fp1 = fopen(argv[1], "r"); fp2 = fopen(argv[2], "w"); if (fp1 == NULL) { printf("file not found"); exit(1); } while(fgets(buffer, 100, fp1) != NULL) if (*buffer != '\n') fputs(buffer, fp2); fclose(fp1); fclose(fp2);

Binary File Processing int fread(void *buf, int size, int n, FILE *fp); int fwrite(const void *buf, int size, int n, FILE *fp);

student1.c output: Student_id Name year Major #include <stdio.h> #include <stdlib.h> #include "student.h" main() { struct student st, *stp = &st; FILE *fp = fopen("st_file", "wb"); if(fp == NULL ) { printf("file open error\n"); exit(1); } printf("student_id Name Year Major\n“); while (scanf("%d %s %d %s", &st.stud_id, st.name, &st.year, st.major) == 4) fwrite(stp, sizeof(struct student), 1, fp); fclose(fp); output: Student_id  Name  year  Major 200601001 Mike 1 Computer 200601002 Tom 1 Computer ... ^Z

student2.c #include <stdio.h> #include <stdlib.h> #include "student.h" int main() { struct student st, *stp = &st; FILE *fp = fopen("st_file", "rb"); if (fp == NULL ) { printf("File Open Error.\n"); exit(1); } printf("-----------------------------------\n"); printf(“%10s %6s %6s %10s\n", ”Student_ID“, "Name“, ”Year“, ”Major“); while (fread(stp, sizeof(struct student), 1, fp) > 0) printf(“%10d %6s %6d %10s\n", st.stud_id, st.name, st.year, st.major); fclose(fp); return 0; output: -----------------------------------    Student_ID   Name   Year   Major     0601001   Mike       1  Computer     0601002   Tom       1  Computer               ...

Random Access Current file position Updated after file I/O is performed n-1 1 2 3 … … Current position

Related Functions int fseek(FILE *fp, long offset, int mode) void rewind(FILE *fp) int ftell(FILE *fp)

fseek mode mode val meaning Example SEEK_SET file start SEEK_CUR 1 file start SEEK_CUR 1 Current SEEK_END 2 file end Example fseek(fp, 0L, SEEK_SET)  파일처음으로 이동 fseek(fp, 100L, SEEK_CUR)  현재 위치에서 100 바이트 우로 이동 fseek(fp, 0L, SEEK_END)   파일 끝으로 이동   

#include <stdio. h> #include <stdlib #include <stdio.h> #include <stdlib.h> #define MAX 256 int main(int argc, char *argv[]) { FILE *fp; char w[MAX]; if ((fp = fopen(argv[1], "a+") ) == NULL ) { fprintf(stderr, "file open error!\n"); exit(1); } puts("Insert a sentence to a file. "); puts("(Just press enter to end the input.)\n"); while (gets(w) != NULL && w[0] != '\0') fprintf(fp, "%s", w); puts("file contents :"); rewind(fp); while (fscanf(fp, "%s", w) == 1) puts(w); if (fclose(fp) != 0) fprintf(stderr, "file close error! \n"); return 0; output: C:> wordAppend message.txt Insert a sentence to a file. Just press enter to end the input.) Hello world. Thank you very much. File Contents: Hello world. Thank you very much. `