Computer science 2009-2010 C programming language lesson 4.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

File Management in C. What is a File? A file is a collection of related data that a computers treats as a single unit. Computers store files to secondary.
UNIT 15 File Processing.
Engineering EG167C - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect M1 P. 1Winter Quarter Midterm I Review.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
Character String Manipulation. Overview Character string functions sscanf() function snprintf() function.
BITS Pilani, Pilani Campus TA C252 Computer Programming - II Vikas Singh File Handling.
File Management in C. A file is a collection of related data that a computers treats as a single unit. File is a collection of data stored permanently.
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.
File I/O Lesson CS1313 Spring File I/O Lesson Outline 1.File I/O Lesson Outline 2.File I/O Using Redirection #1 3.File I/O Using Redirection #2.
CS 241 Section Week #5 2/23/12. 2 Topics This Section MP4 overview Function Pointers Pthreads File I/O.
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.
C Language Summary HTML version. I/O Data Types Expressions Functions Loops and Decisions Preprocessor Statements.
Files Programs and data are stored on disk in structures called files Examples Turbo C++ - binary file Word binary file lab1.c - text file lab1.data.
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.
1 Lecture09: File I/O 5/6/2013 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
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.
1 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
1 Lecture09: File I/O 11/19/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Chapter 3 Input and Output
Chapter 11: Data Files and File Processing Files and streams Creating a sequential access file Reading data from a sequential access file Using fgetc()
FILE IO in ‘C’ by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
C Programming Lecture 12 : File Processing
ME-2221 COMPUTER PROGRAMMING Lecture 18 FILE OPERATIONS Department of Mechanical Engineering A.H.M Fazle Elahi Khulna University of engineering & Technology.
Files A collection of related data treated as a unit. Two types Text
CS 241 Section Week #5 9/22/11. 2 Topics This Section File I/O Advanced C.
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.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic properties and characteristics of external files ❏ To.
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.
Connecting to Files In order to read or write to a file, we need to make a connection to it. There are several functions for doing this. fopen() – makes.
REEM ALAMER REVISION FOR C LANGUAGE COURSE. OUTPUTS int main (void) { int C1, C2; int *p1, *p2; C1 = 8; p1 = &C1; C2 = *p1 / 2 + 5; p2 = &C2; printf ("C1.
Chapter 7 Text Input/Output Objectives
Chapter 7 Text Input/Output Objectives
Chapter 4 File Processing
C Programming Files I/O
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
Lecture 11 File input/output
Chapter 7 Text Input/Output Objectives
Chapter 18 I/O in C.
Plan for the Day: I/O (beyond scanf and printf)
CS111 Computer Programming
File Input/Output.
Programming in C Input / Output.
File I/O We are used to reading from and writing to the terminal:
Lecture 13 Input/Output Files.
C Programming Lecture-15 File I/O
Lecture 15 Files.
File I/O Lesson Outline
Input/Output and the Operating Systems
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
File Handling.
File Access (7.5) CSE 2031 Fall February 2019.
Programming in C Input / Output.
C Preprocessing File I/O
File Handling in C.
FILE handeling.
Module 12 Input and Output
Character Arrays char string1[] = “first”;
Chapter 11 Files chap8.
CSc 352 File I/O Saumya Debray Dept. of Computer Science
EECE.2160 ECE Application Programming
I/O CS580U - Fall 2018.
File I/O We are used to reading from and writing to the terminal:
Files Chapter 8.
Presentation transcript:

Computer science C programming language lesson 4

Aims of lesson 4 Reading from / writing to files

In general a program needs : Files - To read data (text, numbers, images, …) Need to write and to read in files from inside the program - To save results (text, numbers, images, …) To manipulate a file one needs to use a pointer : FILE *fichier fichier is called a dataflow. Opening a file : FILE *fopen (char *nom, char *mode) Example : fichier = fopen(“C:/Data/file1.txt”,”r”); This function returns a pointer on the open file

Files mode explains how to use the dataflow : - “r” : read - “w” : write (if existing the file is suppressed) - “a” : append (at the end of an existing file) Closing a file : int fclose (FILE *fichier) nom is a character string (character array) containing the file name or a standard dataflow (stdin, …) FILE *fopen (char *nom, char *mode) Always close a file after use ! Example : fclose(fichier);

Reading and writing functions Writing : double a; fprintf(fichier,«Var = %lf\n»,a); fprintf(FILE *fichier,*char proto,…); printf(«Var = %lf\n»,a); Same syntax as printf ! Example : Reading : fscanf(FILE *fichier,*char proto,…); double a; fscanf(fichier,«Var = %lf\n»,&a); scanf(«Var = %lf\n»,&a); Same syntax as scanf ! Example :

Example - Writing include void main() { double a,b; FILE *fichier; a=1.5;b=2.5; // Open the file in writing mode fichier = fopen("C:/goudail/fichier.txt","w"); // Check if opening was successful if (fichier != NULL) { // Write data to the file fprintf(fichier,"%lf\n",a); fprintf(fichier,"%lf\n",b); // Close the file fclose(fichier); }

Example - Reading #include void main() { int i; double tab[2]; double *p; FILE *fichier // Open file in reading mode fichier = fopen("C:/goudail/fichier.txt","r"); if (fichier != NULL) { p=tab; // Initialize pointer p for(i=0;i<2;i++) scanf(fichier,"%lf\n",p++); fclose(fichier); } for(i=0;i<2;i++) printf("%lf\n",tab[i]); }

File formats The preceding files were written in « text » format -> we have used the fprintf and fscanf functions -> the ASCII code of each character is written in the file When we use numerical data, it is often more efficient to write them in a binary form. Consider the number E-18 In double format, its size is 8 bytes In ASCII format, its size is 20 bytes (1 byte per character) Writing a data bloc in binary format : int fwrite(void *source,int size, int count, FILE *flow) Reading a data bloc in binary format : int fread(void *dest,int size, int count, FILE *flow) The data are registered thanks to a pointer

Example – Binary file #define DIM void main() { int i; double sum,tab1[DIM],tab2[DIM]; FILE *fichier; // Fill the array for(i=0;i<DIM;i++) {tab1[i]=i*atan(1);} // Write to the file in binary mode fichier = fopen("C:/goudail/fichier.bin","wb"); if (fichier != NULL) { fwrite(tab1,sizeof(double),DIM,fichier); fclose(fichier); } // Read the file fichier = fopen("C:/goudail/fichier.bin","rb"); if (fichier != NULL) { fread(tab2,sizeof(double),DIM,fichier); fclose(fichier); }