Lecture 9: File Processing. Quazi Rahman
|
|
- Marlene Ray
- 5 years ago
- Views:
Transcription
1 Lecture 9: File Processing Quazi Rahman 1
2 Outlines Files Data Hierarchy File Operations Types of File Accessing Files 2
3 FILES Storage of data in variables, arrays or in any other data structures, is temporary such data is lost when a program terminates. Files are used for permanent retention of data. Computers store files on secondary or permanent storage devices. 3
4 Primary vs. Secondary Storage Primary: faster, volatile, more expensive, smaller capacity for storage. Used during the execution of a program. Register: Fastest access. Data exists for few executions. Cache: Faster access. Frequently accessed data exists for longer period. RAM: Fast access. Data exists for the period of program execution. Secondary: slower, permanent, cheaper, larger capacity for storage. Used to permanently store data. Hard Disk, CD-ROM, Flash Drives 4
5 Program I/O Model DISK I/O Bus Data Cache Cache Register CPU Data Bus Program RAM 5
6 Data Hierarchy All data items processed by a computer are reduced to binary data, just combinations of 0 s and 1 s. A bit (short of binary digit, 0 or 1) is the smallest data item in a computer. It is very hard for humans to read and manipulate bits, so we represent bit sequence in some meaningful ways. One such representation is byte. (8 bits = 1 byte). A byte is a very useful data unit. It is used to represent ASCII character set, like a,b,c A,B,C 1,2,3 etc. 6
7 Data Hierarchy Simpler Data can be combined together to represent more complex, but more meaningful and easy to handle data structures, Such as: bits bytes (smallest usable data unit) bytes characters / numbers characters/numbers fields (struct members) fields records (structures) records file 7
8 Files Essentially a file is a sequence of bytes on secondary storage. One can think of a file as an array of bytes. Each file end with a special marker character called end-offile (EOF): EOF <ctrl>d in Unix. EOF <ctrl>z in Windows. DISK (Logical Layout) H e l l o W o r l d! E O F 8
9 Simplified File Management DISK (Logical Layout) H e l l o W o r l d! E O F Other files may be here. Accessing this space directly will cause errors. File Allocation Table (FAT) File Name File Path O/S uses low level system calls to create, maintain and access files as requested by an application. Date / Time Owner / Permission 9
10 File Operations OPEN (fopen()) Opens a file and returns a pointer to the beginning of the file (unless otherwise specified). READ (fscanf()) Reads data from a opened file. WRITE (fprintf()) Writes data into a opened file. CLOSE (fclose()) Closes a opened file and flushes the data stream buffers. 10
11 File and Streams Although the actual data (bytes) stored on the disk may be physically scattered all over the disk cylinders, we can safely assume a sequential and continuous layout of the data in given file. Data retrieval (reading) from a file can be assumed as a stream of bytes coming out from the disk to memory (program). Writing data into a file is just a opposite data stream to the disk. DISK Input Stream (Reading Data) Program Output Stream (Writing Data) 11
12 File Types Two formats of files to store data: Text Files The content of the file is represented by ASCII characters. Ex: Any text file we can open with a text editor like, pico, nedit, notepad etc. Binary Files Stores binary values directly (series of bytes) Opening these files with any text editor will result in a meaningless data representation (unreadable symbols). Ex: Database files, pictures, music etc. 12
13 File Types Two ways to access a file: Sequential-Access Data can be read in one direction from the beginning of the file to the end of the file. Size of a block of data may vary. Random-Access Allow operation to seek or move the read/write head to a particular byte position. Data are stored in same sized blocks. 13
14 Sequential Access Files Main two operations: Reading from a file. Writing into a file. Steps: Reading from a file Request to open an exiting file. Check if the file was actually opened. Read something from the file. Put the values into some variables. When reading is done Close the file. 14
15 Sequential Access Files Steps: Writing into a file Request to open an exiting file or to create a new file. Check if the file was actually opened or created. Write something to the file. Write values of variables into the file When writing is done Close the file. 15
16 Sequential Access Files For both reading or writing we have to establish a connection (data stream) between the program and the file. We need a file pointer (FILE*) to point the file. We have to open the file using the file pointer and the standard library function fopen(). /*prototype of fopen()*/ FILE* fopen(const char* filename, const char* mode); (mode: why open? r = read, w = write, a = append.) Example: FILE* pfile; pfile = fopen( myfile.dat, r ); 16
17 Writing into a Sequential File #include <stdio.h> int main( ) { int num1 = 5; int num2 = 15; int num3 = 25; FILE *outfileptr; outfileptr = fopen( myfile.dat, w ); if ( outfileptr == NULL ) { printf( ERROR: File could not be opened! ); else { fprintf(outfileptr, %d %d %d\n, num1, num2, num3 ); fclose( outfileptr ); return 0; 17
18 Writing into a Sequential File #include <stdio.h> int main( ) { int num1 = 5; int num2 = 15; int num3 = 25; Include <stdio.h> to use File I/O functions FILE *outfileptr; outfileptr = fopen( myfile.dat, w ); if ( outfileptr == NULL ) { printf( ERROR: File could not be opened! ); else { fprintf(outfileptr, %d %d %d\n, num1, num2, num3 ); fclose( outfileptr ); return 0; 18
19 Writing into a Sequential File #include <stdio.h> int main( ) { int num1 = 5; int num2 = 15; int num3 = 25; Declare a pointer to the FILE structure. This will represent the file to program FILE *outfileptr; outfileptr = fopen( myfile.dat, w ); if ( outfileptr == NULL ) { printf( ERROR: File could not be opened! ); else { fprintf(outfileptr, %d %d %d\n, num1, num2, num3 ); fclose( outfileptr ); return 0; 19
20 Writing into a Sequential File #include <stdio.h> int main( ) { int num1 = 5; int num2 = 15; int num3 = 25; FILE *outfileptr; Open the file for writing. If the file does not exist, a new file will be create in the local path. If the file exists, it will be opened and overwritten. outfileptr = fopen( myfile.dat, w ); if ( outfileptr == NULL ) { printf( ERROR: File could not be opened! ); else { fprintf(outfileptr, %d %d %d\n, num1, num2, num3 ); fclose( outfileptr ); return 0; 20
21 File Opening Modes r w a Open a file for reading. Open (Create) a file for writing. Contents of an existing file will be overwritten. Open (Create) a file for appending; For an existing file, writing will be done at the end. r+ Open for reading and writing, start at beginning. w+ Open for reading and writing (overwrite the existing file) a+ Open for reading and writing (append if the file exists) 21
22 Writing into a Sequential File #include <stdio.h> int main( ) { int num1 = 5; int num2 = 15; int num3 = 25; Check if the outfileptr is NULL. If it is, then the file was not opened or created as requested! FILE *outfileptr; outfileptr = fopen( myfile.dat, w ); if ( outfileptr == NULL ) { printf( ERROR: File could not be opened! ); else { fprintf(outfileptr, %d %d %d\n, num1, num2, num3 ); fclose( outfileptr ); return 0; 22
23 What Can Go Wrong? Trying to open a file for writing when no disk space is available (or your quota is exceeded). Trying to open a file from a path/directory that does not exists. Trying to open a file for which you have no proper permissions. Trying to open a file whose contents are corrupted. Sharing violation, when some other program is writing to the file at the same time! (Exclusive lock imposed to avoid data overwriting). 23
24 Writing into a Sequential File #include <stdio.h> int main( ) { int num1 = 5; int num2 = 15; int num3 = 25; FILE *outfileptr; outfileptr = fopen( myfile.dat, w ); fprintf() works the same way as printf(), except that it specifies the FILE pointer to which you print (write) the output data. if ( outfileptr == NULL ) { printf( ERROR: File could not be opened! ); else { fprintf(outfileptr, %d %d %d\n, num1, num2, num3 ); fclose( outfileptr ); return 0; 24
25 Writing into a Sequential File #include <stdio.h> int main( ) { int num1 = 5; int num2 = 15; int num3 = 25; FILE *outfileptr; outfileptr = fopen( myfile.dat, w ); fclose() is important to close the opened file and flush all data buffers and release the file for other programs to use. if ( outfileptr == NULL ) { printf( ERROR: File could not be opened! ); else { fprintf(outfileptr, %d %d %d\n, num1, num2, num3 ); fclose( outfileptr ); return 0; 25
26 File Contents: myfile.dat DISK (Logical Layout) \ n E O F Delimiters There are four fields (four integers). The space is used as the delimiter (separator) for these fields. Other delimiter may also be used, such as, \n, comma, \t. 26
27 Reading from a Sequential File #include <stdio.h> int main() { int num; FILE *infileptr; infileptr = fopen( myfile.dat, r ); if ( infileptr == NULL ) { printf( ERROR: File could not be opened! ); else{ while (!feof( infileptr )) { fscanf( infileptr, %d, &num ); printf( Number: %d\n, num); fclose( intfileptr ); return 0; 27
28 Reading from a Sequential File #include <stdio.h> int main() { int num; FILE *infileptr; Declare a file pointer infileptr = fopen( myfile.dat, r ); if ( infileptr == NULL ) { printf( ERROR: File could not be opened! ); else{ while (!feof( infileptr )) { fscanf( infileptr, %d, &num ); printf( Number: %d\n, num); fclose( intfileptr ); return 0; 28
29 Reading from a Sequential File #include <stdio.h> int main() { int num; FILE *infileptr; Open the file for reading infileptr = fopen( myfile.dat, r ); if ( infileptr == NULL ) { printf( ERROR: File could not be opened! ); else{ while (!feof( infileptr )) { fscanf( infileptr, %d, &num ); printf( Number: %d\n, num); fclose( intfileptr ); return 0; 29
30 Reading from a Sequential File #include <stdio.h> int main() { int num; FILE *infileptr; infileptr = fopen( myfile.dat, r ); if ( infileptr == NULL ) { Continue reading the file until the end, or there is nothing (more) to read. printf( ERROR: File could not be opened! ); else{ while (!feof( infileptr )) { fscanf( infileptr, %d, &num ); printf( Number: %d\n, num); fclose( intfileptr ); return 0; 30
31 Reading from a Sequential File #include <stdio.h> int main() { int num; FILE *infileptr; infileptr = fopen( myfile.dat, r ); if ( infileptr == NULL ) { Read an integer from the file and store it into a variable (num) printf( ERROR: File could not be opened! ); else{ while (!feof( infileptr )) { fscanf( infileptr, %d, &num ); printf( Number: %d\n, num); fclose( intfileptr ); return 0; 31
32 Reading from a Sequential File #include <stdio.h> int main() { int num; FILE *infileptr; infileptr = fopen( myfile.dat, r ); if ( infileptr == NULL ) { Print the value of num in the console printf( ERROR: File could not be opened! ); else{ while (!feof( infileptr )) { fscanf( infileptr, %d, &num ); printf( Number: %d\n, num); fclose( intfileptr ); return 0; 32
33 Reading from a Sequential File #include <stdio.h> int main() { int num; FILE *infileptr; infileptr = fopen( myfile.dat, r ); if ( infileptr == NULL ) { printf( ERROR: File could not be opened! ); else{ while (!feof( infileptr )) { fscanf( infileptr, %d, &num ); printf( Number: %d\n, num); fclose( intfileptr ); return 0; Close the file 33
34 Break 34
35 Accessing Sequential File The standard library provides many functions for opening files, closing files, reading data from files and for writing data to files. Some of them are: fopen() Opens a file in the desired mode of operation. fclose() Closes an opened file and flushes the buffer. fprintf()/fscanf() Similar to printf()/scanf(), only use a FILE pointer as the first parameter to indicate where the data is being streamed to/from. 35
36 Accessing Sequential File feof() Used to test if the read/write head is currently located at the end-of-file (EOF) marker. Reading passed the EOF is a serious error! fgetc() Reads one character from a file. fputc() Writes one character to a file. fgets() Reads a line from a file. fputs() Writes a line to a file. 36
37 Sequential vs. Random Access Files As we stated previously, records in a sequential file created with the formatted output function fprintf() are not necessarily the same length. However, individual records of a random-access file are normally fixed in length and may be accessed directly (and thus quickly) without searching through other records. This makes random-access files appropriate for transaction processing systems that require rapid access to specific data. Airline reservation systems, Banking systems, Point-of-sale systems, and many more. 37
38 Random Access Files In Random-access file processing, we can access a record directly. Random access files are highly structured and well organized. Operations such as fwrite, fread and fseek are used. We need to keep track of specific byte positions, to calculate where a given record is located in the file, or where to write a given record. EX: calculate the byte offset starting from given byte position. Data can be added easily to a random-access file without destroying other data in the file. 38
39 Random Access Files To facilitate random-access, data is written in fixed-length records. Since every record is the same length, the computer can quickly calculate (as a function of the record key) the exact location of a record in relation to the beginning of the file. Data stored previously in a file with fixedlength records can also be changed and deleted without rewriting the entire file. 39
40 Fixed-Length Records Byte offsets k 100 bytes 100 bytes 100 bytes 100 bytes 100 bytes 100 bytes To find record k: fseek( FilePtr, (k 1)*sizeof(struct rec), SEEK_SET) Which File After how many Bytes Starting Byte 40
41 Random-Access File I/O Writing to a random access file involves positioning the write head to the beginning offset of the record and write command of an entire record at a specific starting offset. Example to write one record: fseek( FilePtr, (ID 1) * sizeof(student), SEEK_SET); fwrite( &s1, sizeof(student), 1, FilePtr); The structure to be written Byte size of the structure How many structures. To which file. 41
42 Random-Access File I/O Reading involves positioning the read head to the beginning offset of the record and then read an entire record into a structure. Example to seek and read one record: fseek( FilePtr, (ID 1) * sizeof(student), SEEK_SET); fread( &s2, sizeof(student), 1, FilePtr); 42
43 Other Variations When storing fixed size records we often waste a lot of unused disk space within the record. If space efficiency is in question, we can use variable size records. That is to use only the space we actually need to store our data. This, however, would complicate the way we organize the file. Finding records can no longer be easily calculated. We have to keep additional index tables to maintain the offset positions of each record in the file. This discussion is beyond the scope of this course and will be discussed in other data structure courses. 43
44 More on Files Three files and their associated streams are automatically opened when program execution begins. Standard input file (FILE* stdin) Standard output file (FILE* stdout) Standard error file (FILE* stderr) 44
45 More on Files fseek() Position the read/write head at position specified by an off-set byte from: SEEK_SET beginning of the file SEEK_CUR current location of the head SEEK_END end of thee file. 45
46 More on Files We can check the return values of fscanf, fseek and fwrite to check if they performed properly fscanf returns number of fields successfully read or EOF if any problem occurred. fseek returns 0 if the operation was successful, otherwise returns a non-zero value. fwrite returns the number of element successfully written. 46
47 Creating a Sequential Access File Problem 1: Write a simple C program to access (read/write) a sequential file to be used as a clients account file for a bank. Each client has only three data: Account number (an integer) Name (a string) Balance (a double) 47
48 Problem 1: include<stdio.h> int main(){ int account; char name[30]; double balance; FILE* cfile; cfile = fopen("alldata/clients.dat", "w"); if(cfile == NULL) printf("file could not be opened!\n"); else { printf("enter account, name and balance.\n"); printf("enter '0' to end input.\n? "); scanf("%d", &account); while( account!= 0 ){ scanf("%s%lf", name, &balance); fprintf(cfile,"%d %s %.2f\n",account,name,balance); printf("? "); scanf("%d", &account); fclose(cfile); 48
49 Problem 1: printf("data entry done...\n"); printf("read the file? [y/n]: "); char ch; fflush(stdin); scanf("%c", &ch); if(ch == 'y') { cfile = fopen("alldata/clients.dat", "r"); if(cfile == NULL) printf("file could not be opened!\n"); else { fscanf(cfile, "%d%s%lf", &account, name, &balance); while(!feof(cfile) ) { printf("%d %s %.2f\n", account, name, balance); fscanf(cfile,"%d%s%lf",&account,name,&balance); fclose(cfile); return 0; 49
50 Reading Sequential File Problem 2: Write a simple C program to read input from a sequential file. 50
51 Example 2 #include<stdio.h> int main(){ int account; char name[50]; double balance; scanf("%d%s%lf", &account, name, &balance); while(!feof(stdin) ){ printf("%d %s %.2f\n", account, name, balance); scanf("%d%s%lf", &account, name, &balance); return 0; 51
52 Creating a Random Access File Problem 3: Write a simple C program to access (read/write) a Random Access file to be used as a clients account file for a bank. The bank has only 20 clients Each client record has only three fields: Account number (an integer) Name (a string) Balance (a double) 52
53 Creating a Random Access File #include<stdio.h> #define NUM 20 typedef struct { int account; char name[30]; double balance; Client; void main() { int i; Client blankclient = {0,, 0.0; FILE* fptr; if((fptr = fopen(rclient.dat, wb )) == NULL) printf( File could not be opened.\n ); else { for(i = 0; i < NUM; i++) fwrite(&blankclient, sizeof(client), 1, fptr); fclose(fptr); 53
54 Accessing a Random Access File #include<stdio.h> #define NUM 20 typedef struct { int account; char name[30]; double balance; Client; void main() { int i; Client client = {0,, 0.0; FILE* fptr; if((fptr = fopen(rclient.dat, rb+ )) == NULL) printf( File could not be opened.\n ); else { //see next slide 54
55 Accessing a Random Access File else { printf( Enter account number (1 to 20, 0 to end)\n? ); scanf( %d, &client.account); while(client.account!= 0) { printf( Enter name and balance\n? ); scanf( %s%lf, &client.name, &client.balance); fseek(fptr, (client.account 1)*sizeof(Client), SEEK_SET); fwrite(&client, sizeof(client), 1, fptr); printf( Enter account number\n? ); scanf( %d, &client.account); fclose(fptr); 55
56 Reading a Random Access File #include<stdio.h> #define NUM 20 typedef struct { int account; char name[30]; double balance; Client; void main() { int i; Client client = {0,, 0.0; FILE* fptr; if((fptr = fopen(rclient.dat, rb )) == NULL) printf( File could not be opened.\n ); else { //see next slide 56
57 Reading a Random Access File else { printf("%-10s%-10s%10s\n", "Account", "Name", "Balance"); rewind(fptr); while(!feof(fptr)) { fread(&client, sizeof(client), 1, fptr); if(client.account!= 0) { fclose(fptr); printf("%-10d%-10s%10.2f\n", client.account, client.name, client.balance); 57
58 Access Random Access File #include<stdio.h> void main() { int i; FILE* fptr; Client clientarray[3]; for(i = 0; i < 3; i++) { clientarray[i].account = i + 1; printf("enter name and balance\n? "); scanf("%s%lf", &clientarray[i].name, &clientarray[i].balance); if((fptr = fopen("rclientarray.dat", "wb+")) == NULL) printf("file could not be opened.\n"); else { fwrite(clientarray, sizeof(client), 3, fptr); 58
59 Next topics: Dynamic Data Structures Read chapter 12 59
ENG120. Misc. Topics
ENG120 Misc. Topics Topics Files in C Using Command-Line Arguments Typecasting Working with Multiple source files Conditional Operator 2 Files and Streams C views each file as a sequence of bytes File
More informationC File Processing: One-Page Summary
Chapter 11 C File Processing C File Processing: One-Page Summary #include int main() { int a; FILE *fpin, *fpout; if ( ( fpin = fopen( "input.txt", "r" ) ) == NULL ) printf( "File could not be
More informationCh 11. C File Processing (review)
Ch 11 C File Processing (review) OBJECTIVES To create, read, write and update files. Sequential access file processing. Data Hierarchy Data Hierarchy: Bit smallest data item Value of 0 or 1 Byte 8 bits
More informationChapter 12. Files (reference: Deitel s chap 11) chap8
Chapter 12 Files (reference: Deitel s chap 11) 20061025 chap8 Introduction of File Data files Can be created, updated, and processed by C programs Are used for permanent storage of large amounts of data
More informationChapter 11 File Processing
1 Chapter 11 File Processing Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Chapter 11 File Processing Outline 11.1 Introduction 11.2 The Data Hierarchy 11.3
More informationLecture 8. Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)
Lecture 8 Data Files Dr M Kasim A Jalil Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson) Objectives In this chapter, you will learn: To be able to create, read, write and update
More informationLecture6 File Processing
1 Lecture6 File Processing Dr. Serdar ÇELEBİ 2 Introduction The Data Hierarchy Files and Streams Creating a Sequential Access File Reading Data from a Sequential Access File Updating Sequential Access
More informationFile Processing. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan
File Processing Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Outline 11.2 The Data Hierarchy 11.3 Files and Streams 11.4 Creating a Sequential
More informationC How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.
C How to Program, 6/e Storage of data in variables and arrays is temporary such data is lost when a program terminates. Files are used for permanent retention of data. Computers store files on secondary
More informationIntroduction to Computer Programming Lecture 18 Binary Files
Introduction to Computer Programming Lecture 18 Binary Files Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr 1 RECALL: Text File Handling
More informationCSI 402 Lecture 2 Working with Files (Text and Binary)
CSI 402 Lecture 2 Working with Files (Text and Binary) 1 / 30 AQuickReviewofStandardI/O Recall that #include allows use of printf and scanf functions Example: int i; scanf("%d", &i); printf("value
More informationUNIT IV-2. The I/O library functions can be classified into two broad categories:
UNIT IV-2 6.0 INTRODUCTION Reading, processing and writing of data are the three essential functions of a computer program. Most programs take some data as input and display the processed data, often known
More informationMode Meaning r Opens the file for reading. If the file doesn't exist, fopen() returns NULL.
Files Files enable permanent storage of information C performs all input and output, including disk files, by means of streams Stream oriented data files are divided into two categories Formatted data
More informationC: How to Program. Week /June/18
C: How to Program Week 17 2007/June/18 1 Chapter 11 File Processing Outline 11.1 Introduction 11.2 The Data Hierarchy 11.3 Files and Streams 11.4 Creating a Sequential Access File 11.5 Reading Data from
More informationPROGRAMMAZIONE I A.A. 2017/2018
PROGRAMMAZIONE I A.A. 2017/2018 INPUT/OUTPUT INPUT AND OUTPUT Programs must be able to write data to files or to physical output devices such as displays or printers, and to read in data from files or
More informationLAB 13 FILE PROCESSING
LAB 13 FILE PROCESSING School of Computer and Communication Engineering Universiti Malaysia Perlis 1 1. OBJECTIVES: 1.1 To be able to create, read, write and update files. 1.2 To become familiar with sequential
More informationFiles and Streams Opening and Closing a File Reading/Writing Text Reading/Writing Raw Data Random Access Files. C File Processing CS 2060
CS 2060 Files and Streams Files are used for long-term storage of data (on a hard drive rather than in memory). Files and Streams Files are used for long-term storage of data (on a hard drive rather than
More informationSTRUCTURES & FILE IO
STRUCTURES & FILE IO Structures Collections of related variables (aggregates) under one name Can contain variables of different data types Commonly used to define records to be stored in files Combined
More informationLAB 13 FILE PROCESSING
LAB 13 FILE PROCESSING School of Computer and Communication Engineering Universiti Malaysia Perlis 1 1. OBJECTIVES: 1.1 To be able to create, read, write and update files. 1.2 To become familiar with sequential
More informationAccessing Files in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts
Accessing Files in C Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie, Absolute C++, by Walter
More informationCMPE-013/L. File I/O. File Processing. Gabriel Hugh Elkaim Winter File Processing. Files and Streams. Outline.
CMPE-013/L Outline File Processing File I/O Gabriel Hugh Elkaim Winter 2014 Files and Streams Open and Close Files Read and Write Sequential Files Read and Write Random Access Files Read and Write Random
More informationSystem Software Experiment 1 Lecture 7
System Software Experiment 1 Lecture 7 spring 2018 Jinkyu Jeong ( jinkyu@skku.edu) Computer Systems Laboratory Sungyunkwan University http://csl.skku.edu SSE3032: System Software Experiment 1, Spring 2018
More informationStandard File Pointers
1 Programming in C Standard File Pointers Assigned to console unless redirected Standard input = stdin Used by scan function Can be redirected: cmd < input-file Standard output = stdout Used by printf
More informationCSci 4061 Introduction to Operating Systems. Input/Output: High-level
CSci 4061 Introduction to Operating Systems Input/Output: High-level I/O Topics First, cover high-level I/O Next, talk about low-level device I/O I/O not part of the C language! High-level I/O Hide device
More informationDarshan Institute of Engineering & Technology for Diploma Studies Unit 6
1. What is File management? In real life, we want to store data permanently so that later on we can retrieve it and reuse it. A file is a collection of bytes stored on a secondary storage device like hard
More informationFundamentals of Programming. Lecture 15: C File Processing
1 Fundamentals of Programming Lecture 15: C File Processing Instructor: Fatemeh Zamani f_zamani@ce.sharif.edu Sharif University of Technology Computer Engineering Department The lectures of this course
More informationBIL 104E Introduction to Scientific and Engineering Computing. Lecture 12
BIL 104E Introduction to Scientific and Engineering Computing Lecture 12 Files v.s. Streams In C, a file can refer to a disk file, a terminal, a printer, or a tape drive. In other words, a file represents
More informationUnit 6 Files. putchar(ch); ch = getc (fp); //Reads single character from file and advances position to next character
1. What is File management? In real life, we want to store data permanently so that later on we can retrieve it and reuse it. A file is a collection of bytes stored on a secondary storage device like hard
More informationMARKS: Q1 /20 /15 /15 /15 / 5 /30 TOTAL: /100
FINAL EXAMINATION INTRODUCTION TO ALGORITHMS AND PROGRAMMING II 03-60-141-01 U N I V E R S I T Y O F W I N D S O R S C H O O L O F C O M P U T E R S C I E N C E Winter 2014 Last Name: First Name: Student
More informationCS240: Programming in C
CS240: Programming in C Lecture 13 si 14: Unix interface for working with files. Cristina Nita-Rotaru Lecture 13/Fall 2013 1 Working with Files (I/O) File system: specifies how the information is organized
More informationC-Refresher: Session 10 Disk IO
C-Refresher: Session 10 Disk IO Arif Butt Summer 2017 I am Thankful to my student Muhammad Zubair bcsf14m029@pucit.edu.pk for preparation of these slides in accordance with my video lectures at http://www.arifbutt.me/category/c-behind-the-curtain/
More informationIntermediate Programming, Spring 2017*
600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general
More informationChapter 8 File Processing
Chapter 8 File Processing Outline 1 Introduction 2 The Data Hierarchy 3 Files and Streams 4 Creating a Sequential Access File 5 Reading Data from a Sequential Access File 6 Updating Sequential Access Files
More informationC PROGRAMMING. Characters and Strings File Processing Exercise
C PROGRAMMING Characters and Strings File Processing Exercise CHARACTERS AND STRINGS A single character defined using the char variable type Character constant is an int value enclosed by single quotes
More informationStandard C Library Functions
Demo lecture slides Although I will not usually give slides for demo lectures, the first two demo lectures involve practice with things which you should really know from G51PRG Since I covered much of
More informationPreprocessing directives are lines in your program that start with `#'. The `#' is followed by an identifier that is the directive name.
Unit-III Preprocessor: The C preprocessor is a macro processor that is used automatically by the C compiler to transform your program before actual compilation. It is called a macro processor because it
More informationContent. Input Output Devices File access Function of File I/O Redirection Command-line arguments
File I/O Content Input Output Devices File access Function of File I/O Redirection Command-line arguments UNIX and C language C is a general-purpose, high-level language that was originally developed by
More informationC Basics And Concepts Input And Output
C Basics And Concepts Input And Output Report Working group scientific computing Department of informatics Faculty of mathematics, informatics and natural sciences University of Hamburg Written by: Marcus
More informationCSI 402 Systems Programming LECTURE 4 FILES AND FILE OPERATIONS
CSI 402 Systems Programming LECTURE 4 FILES AND FILE OPERATIONS A mini Quiz 2 Consider the following struct definition struct name{ int a; float b; }; Then somewhere in main() struct name *ptr,p; ptr=&p;
More informationEM108 Software Development for Engineers
EE108 Section 4 Files page 1 of 14 EM108 Software Development for Engineers Section 4 - Files 1) Introduction 2) Operations with Files 3) Opening Files 4) Input/Output Operations 5) Other Operations 6)
More informationC for Engineers and Scientists: An Interpretive Approach. Chapter 14: File Processing
Chapter 14: File Processing Files and Streams C views each file simply as a sequential stream of bytes. It ends as if there is an end-of-file marker. The data structure FILE, defined in stdio.h, stores
More informationCSCI 171 Chapter Outlines
Contents CSCI 171 Chapter 1 Overview... 2 CSCI 171 Chapter 2 Programming Components... 3 CSCI 171 Chapter 3 (Sections 1 4) Selection Structures... 5 CSCI 171 Chapter 3 (Sections 5 & 6) Iteration Structures
More informationInput/Output and the Operating Systems
Input/Output and the Operating Systems Fall 2015 Jinkyu Jeong (jinkyu@skku.edu) 1 I/O Functions Formatted I/O printf( ) and scanf( ) fprintf( ) and fscanf( ) sprintf( ) and sscanf( ) int printf(const char*
More informationFundamentals of Programming. Lecture 10 Hamed Rasifard
Fundamentals of Programming Lecture 10 Hamed Rasifard 1 Outline File Input/Output 2 Streams and Files The C I/O system supplies a consistent interface to the programmer independent of the actual device
More informationComputer Programming Unit v
READING AND WRITING CHARACTERS We can read and write a character on screen using printf() and scanf() function but this is not applicable in all situations. In C programming language some function are
More informationOrganization of a file
File Handling 1 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example: Consider keeping students records 100 students
More informationBBM#101# #Introduc/on#to# Programming#I# Fall$2014,$Lecture$13$
BBM#101# #Introduc/on#to# Programming#I# Fall$2014,$Lecture$13$ Today#! File#Input#and#Output#! Strings#! Data&Files&! The&Data&Type&char&! Data&Hierarchy&! Characters&and&Integers&! Files&and&Streams&!
More informationProgram Design (II): Quiz2 May 18, 2009 Part1. True/False Questions (30pts) Part2. Multiple Choice Questions (40pts)
Class: No. Name: Part1. True/False Questions (30pts) 1. Function fscanf cannot be used to read data from the standard input. ANS: False. Function fscanf can be used to read from the standard input by including
More information25.2 Opening and Closing a File
Lecture 32 p.1 Faculty of Computer Science, Dalhousie University CSCI 2132 Software Development Lecture 32: Dynamically Allocated Arrays 26-Nov-2018 Location: Chemistry 125 Time: 12:35 13:25 Instructor:
More informationCSI 402 Lecture 2 (More on Files) 2 1 / 20
CSI 402 Lecture 2 (More on Files) 2 1 / 20 Files A Quick Review Type for file variables: FILE * File operations use functions from stdio.h. Functions fopen and fclose for opening and closing files. Functions
More informationSlide Set 8. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng
Slide Set 8 for ENCM 339 Fall 2017 Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary October 2017 ENCM 339 Fall 2017 Section 01 Slide
More informationFile I/O. Arash Rafiey. November 7, 2017
November 7, 2017 Files File is a place on disk where a group of related data is stored. Files File is a place on disk where a group of related data is stored. C provides various functions to handle files
More informationQuick review of previous lecture Ch6 Structure Ch7 I/O. EECS2031 Software Tools. C - Structures, Unions, Enums & Typedef (K&R Ch.
1 Quick review of previous lecture Ch6 Structure Ch7 I/O EECS2031 Software Tools C - Structures, Unions, Enums & Typedef (K&R Ch.6) Structures Basics: Declaration and assignment Structures and functions
More informationGoals of this Lecture
I/O Management 1 Goals of this Lecture Help you to learn about: The Unix stream concept Standard C I/O functions Unix system-level functions for I/O How the standard C I/O functions use the Unix system-level
More informationI/O Management! Goals of this Lecture!
I/O Management! 1 Goals of this Lecture! Help you to learn about:" The Unix stream concept" Standard C I/O functions" Unix system-level functions for I/O" How the standard C I/O functions use the Unix
More informationI/O Management! Goals of this Lecture!
I/O Management! 1 Goals of this Lecture! Help you to learn about:" The Unix stream concept" Standard C I/O functions" Unix system-level functions for I/O" How the standard C I/O functions use the Unix
More informationHIGH LEVEL FILE PROCESSING
HIGH LEVEL FILE PROCESSING 1. Overview The learning objectives of this lab session are: To understand the functions used for file processing at a higher level. o These functions use special structures
More informationChapter 12. Text and Binary File Processing. Instructor: Öğr. Gör. Okan Vardarlı. Copyright 2004 Pearson Addison-Wesley. All rights reserved.
Chapter 12 Text and Binary File Processing Instructor: Öğr. Gör. Okan Vardarlı Copyright 2004 Pearson Addison-Wesley. All rights reserved. Objectives We will explore the use of standard input, standard
More informationWeek 9 Lecture 3. Binary Files. Week 9
Lecture 3 Binary Files 1 Reading and Writing Binary Files 2 Binary Files It is possible to write the contents of memory directly to a file. The bits need to be interpreted on input Possible to write out
More informationComputer programming
Computer programming "He who loves practice without theory is like the sailor who boards ship without a ruder and compass and never knows where he may cast." Leonardo da Vinci T.U. Cluj-Napoca - Computer
More informationC programming basics T3-1 -
C programming basics T3-1 - Outline 1. Introduction 2. Basic concepts 3. Functions 4. Data types 5. Control structures 6. Arrays and pointers 7. File management T3-2 - 3.1: Introduction T3-3 - Review of
More informationBBM#101# #Introduc/on#to# Programming#I# Fall$2013,$Lecture$13$
BBM#101# #Introduc/on#to# Programming#I# Fall$2013,$Lecture$13$ Today#! File#Input#and#Output#! Strings#! Data!Files!! The!Data!Type!char!! Data!Hierarchy!! Characters!and!Integers!! Files!and!Streams!!
More informationIntroduction to Computer and Program Design. Lesson 6. File I/O. James C.C. Cheng Department of Computer Science National Chiao Tung University
Introduction to Computer and Program Design Lesson 6 File I/O James C.C. Cheng Department of Computer Science National Chiao Tung University File System in OS Microsoft Windows Filename DriveID : /DirctoryName/MainFileName.ExtensionName
More information2009 S2 COMP File Operations
2009 S2 COMP1921 9. File Operations Oliver Diessel odiessel@cse.unsw.edu.au Last updated: 16:00 22 Sep 2009 9. File Operations Topics to be covered: Streams Text file operations Binary file operations
More informationIntroduction to Programming in C Department of Computer Science and Engineering. Lecture No. #47. File Handling
Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #47 File Handling In this video, we will look at a few basic things about file handling in C. This is a vast
More informationFile Access. FILE * fopen(const char *name, const char * mode);
File Access, K&R 7.5 Dealing with named files is surprisingly similar to dealing with stdin and stdout. Start by declaring a "file pointer": FILE *fp; /* See Appendix B1.1, pg. 242 */ header
More informationUNIX System Programming
File I/O 경희대학교컴퓨터공학과 조진성 UNIX System Programming File in UNIX n Unified interface for all I/Os in UNIX ü Regular(normal) files in file system ü Special files for devices terminal, keyboard, mouse, tape,
More informationSystems Programming. 08. Standard I/O Library. Alexander Holupirek
Systems Programming 08. Standard I/O Library Alexander Holupirek Database and Information Systems Group Department of Computer & Information Science University of Konstanz Summer Term 2008 Last lecture:
More informationBBM 101 Introduc/on to Programming I Fall 2013, Lecture 13
BBM 101 Introduc/on to Programming I Fall 2013, Lecture 13 Instructors: Aykut Erdem, Erkut Erdem, Fuat Akal TAs: Yasin Sahin, Ahmet Selman Bozkir, Gultekin Isik 1 Today File Input and Output Strings Data
More informationFile IO and command line input CSE 2451
File IO and command line input CSE 2451 File functions Open/Close files fopen() open a stream for a file fclose() closes a stream One character at a time: fgetc() similar to getchar() fputc() similar to
More informationStream Model of I/O. Basic I/O in C
Stream Model of I/O 1 A stream provides a connection between the process that initializes it and an object, such as a file, which may be viewed as a sequence of data. In the simplest view, a stream object
More informationFile I/O. Last updated 10/30/18
Last updated 10/30/18 Input/Output Streams Information flow between entities is done with streams Keyboard Text input stream data stdin Data Text output stream Monitor stdout stderr printf formats data
More informationLecture 03 Bits, Bytes and Data Types
Lecture 03 Bits, Bytes and Data Types Computer Languages A computer language is a language that is used to communicate with a machine. Like all languages, computer languages have syntax (form) and semantics
More informationUNIT-V CONSOLE I/O. This section examines in detail the console I/O functions.
UNIT-V Unit-5 File Streams Formatted I/O Preprocessor Directives Printf Scanf A file represents a sequence of bytes on the disk where a group of related data is stored. File is created for permanent storage
More informationCS246 Spring14 Programming Paradigm Files, Pipes and Redirection
1 Files 1.1 File functions Opening Files : The function fopen opens a file and returns a FILE pointer. FILE *fopen( const char * filename, const char * mode ); The allowed modes for fopen are as follows
More informationInput / Output Functions
CSE 2421: Systems I Low-Level Programming and Computer Organization Input / Output Functions Presentation G Read/Study: Reek Chapter 15 Gojko Babić 10-03-2018 Input and Output Functions The stdio.h contain
More informationfopen() fclose() fgetc() fputc() fread() fwrite()
The ability to read data from and write data to files is the primary means of storing persistent data, data that does not disappear when your program stops running. The abstraction of files that C provides
More informationProgramming & Data Structure
File Handling Programming & Data Structure CS 11002 Partha Bhowmick http://cse.iitkgp.ac.in/ pb CSE Department IIT Kharagpur Spring 2012-2013 File File Handling File R&W argc & argv (1) A file is a named
More informationComputer System and programming in C
File Handling 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 storage so that the contents of files remain intact
More informationNaked C Lecture 6. File Operations and System Calls
Naked C Lecture 6 File Operations and System Calls 20 August 2012 Libc and Linking Libc is the standard C library Provides most of the basic functionality that we've been using String functions, fork,
More informationComputer Programming: Skills & Concepts (CP) Files in C
CP 20 slide 1 Tuesday 21 November 2017 Computer Programming: Skills & Concepts (CP) Files in C Julian Bradfield Tuesday 21 November 2017 Today s lecture Character oriented I/O (revision) Files and streams
More informationData File and File Handling
Types of Disk Files Data File and File Handling Text streams are associated with text-mode files. Text-mode files consist of a sequence of lines. Each line contains zero or more characters and ends with
More informationCE Lecture 11
Izmir Institute of Technology CE - 104 Lecture 11 References: - C: A software Engineering Approach 1 In this course you will learn Input and Output Sorting Values 2 Input and Output Opening and Closing
More informationProgramming Fundamentals
Programming Fundamentals Day 4 1 Session Plan Searching & Sorting Sorting Selection Sort Insertion Sort Bubble Sort Searching Linear Search Binary Search File Handling Functions Copyright 2004, 2 2 Sorting
More informationWhat Is Operating System? Operating Systems, System Calls, and Buffered I/O. Academic Computers in 1983 and Operating System
What Is Operating System? Operating Systems, System Calls, and Buffered I/O emacs gcc Browser DVD Player Operating System CS 217 1 Abstraction of hardware Virtualization Protection and security 2 Academic
More informationFile (1A) Young Won Lim 11/25/16
File (1A) Copyright (c) 2010-2016 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version
More informationFiles. Programs and data are stored on disk in structures called files Examples. a.out binary file lab1.c - text file term-paper.
File IO part 2 Files Programs and data are stored on disk in structures called files Examples a.out binary file lab1.c - text file term-paper.doc - binary file Overview File Pointer (FILE *) Standard:
More informationCS 261 Fall Mike Lam, Professor. Structs and I/O
CS 261 Fall 2018 Mike Lam, Professor Structs and I/O Typedefs A typedef is a way to create a new type name Basically a synonym for another type Useful for shortening long types or providing more meaningful
More informationSistemas Operativos /2016 Support Document N o 1. Files, Pipes, FIFOs, I/O Redirection, and Unix Sockets
Universidade de Coimbra Departamento de Engenharia Electrotécnica e de Computadores Sistemas Operativos - 2015/2016 Support Document N o 1 Files, Pipes, FIFOs, I/O Redirection, and Unix Sockets 1 Objectives
More informationProgramming. Functions and File I/O
Programming Functions and File I/O Summary Functions Definition Declaration Invocation Pass by value vs. pass by reference File I/O Reading Writing Static keyword Global vs. local variables 2 Functions
More informationCSC209H Lecture 3. Dan Zingaro. January 21, 2015
CSC209H Lecture 3 Dan Zingaro January 21, 2015 Streams (King 22.1) Stream: source of input or destination for output We access a stream through a file pointer (FILE *) Three streams are available without
More informationC Programming Language
C Programming Language File Input/Output Dr. Manar Mohaisen Office: F208 Email: manar.subhi@kut.ac.kr Department of EECE Review of the Precedent Lecture Arrays and Pointers Class Objectives What is a File?
More informationC Programming 1. File Access. Goutam Biswas. Lect 29
C Programming 1 File Access C Programming 2 Standard I/O So far all our I/O operations are read from the standard input (stdin - keyboard) and write to the standard output (stdout - VDU) devices. These
More informationC Input/Output. Before we discuss I/O in C, let's review how C++ I/O works. int i; double x;
C Input/Output Before we discuss I/O in C, let's review how C++ I/O works. int i; double x; cin >> i; cin >> x; cout
More informationDATA STRUCTURES USING C
DATA STRUCTURES USING C File Handling in C Goals By the end of this unit you should understand how to open a file to write to it. how to open a file to read from it. how to open a file to append data to
More informationProgramming in C. Session 8. Seema Sirpal Delhi University Computer Centre
Programming in C Session 8 Seema Sirpal Delhi University Computer Centre File I/O & Command Line Arguments An important part of any program is the ability to communicate with the world external to it.
More information7/21/ FILE INPUT / OUTPUT. Dong-Chul Kim BioMeCIS UTA
7/21/2014 1 FILE INPUT / OUTPUT Dong-Chul Kim BioMeCIS CSE @ UTA What s a file? A named section of storage, usually on a disk In C, a file is a continuous sequence of bytes Examples for the demand of a
More informationChapter 14 - Advanced C Topics
Chapter 14 - Advanced C Topics Outline 14.1 Introduction 14.2 Redirecting Input/Output on UNIX and DOS Systems 14.3 Variable-Length Argument Lists 14.4 Using Command-Line Arguments 14.5 Notes on Compiling
More informationComputer Programming: Skills & Concepts (CP1) Files in C. 18th November, 2010
Computer Programming: Skills & Concepts (CP1) Files in C 18th November, 2010 CP1 26 slide 1 18th November, 2010 Today s lecture Character oriented I/O (revision) Files and streams Opening and closing files
More information8. Characters, Strings and Files
REGZ9280: Global Education Short Course - Engineering 8. Characters, Strings and Files Reading: Moffat, Chapter 7, 11 REGZ9280 14s2 8. Characters and Arrays 1 ASCII The ASCII table gives a correspondence
More information