CS1061: C Programming Lecture 19: Random Access Files A. O’Riordan, 2004, 2007 updated.

Slides:



Advertisements
Similar presentations
Lecture 3 Some commonly used C programming tricks. The system command Project No. 1: A warm-up project.
Advertisements

Introduction to C Programming
Strings Input/Output scanf and printf sscanf and sprintf gets and puts.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
Character String Manipulation. Overview Character string functions sscanf() function snprintf() function.
Lecture 20 Arrays and Strings
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
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.
Pointer, malloc and realloc 1. Name entered was 6 char, not enough space to put null terminator 2 Array of char.
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
Kernighan/Ritchie: Kelley/Pohl:
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Data files –Can be created, updated,
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Lec11: File Processing 廖雪花 TEL: 年 5 月.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 11 – File Processing Outline 11.1Introduction 11.2The Data Hierarchy 11.3Files and Streams 11.4Creating.
Strings in C. Strings are Character Arrays Strings in C are simply arrays of characters. – Example:char s [10]; This is a ten (10) element array that.
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.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
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.
Programming Practice Introduction Tree Operations. Binary Search Tree. File Processing Create, read, write and update files. Sequential.
1 File Processing Dr. Serdar ÇELEBİ. 2 Outline Introduction The Data Hierarchy Files and Streams Creating a Sequential Access File Reading Data from a.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Programming Languages -1 (Introduction to C) files Instructor: M.Fatih AMASYALI
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.
Chapter 8 : Binary Data Files1 Binary Data Files CHAPTER 8.
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.
Chapter 11: Data Files and File Processing Files and streams Creating a sequential access file Reading data from a sequential access file Using fgetc()
Chapter 11 File Processing. Objectives In this chapter, you will learn: –To be able to create, read, write and update files. –To become familiar with.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Introduce some standard library functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 11 – File Processing Outline 11.1Introduction.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
FILE IO in ‘C’ by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
C File Processing. Objectives To be able to create, write and update files. To become familiar with sequential access file processing. To become familiar.
Chapter 12 Files (reference: Deitel ’ s chap 11) chap8.
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 29 Thanks for Lecture Slides:
 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.
ECE 103 Engineering Programming Chapter 29 C Strings, Part 2 Herbert G. Mayer, PSU CS Status 7/30/2014 Initial content copied verbatim from ECE 103 material.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Chapter 11 – File Processing
11 C File Processing.
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
ECE Application Programming
Chapter 4 File Processing
Lecture 11 File input/output
TMF1414 Introduction to Programming
Introduction to Computer Programming Lecture 18 Binary Files
File Processing (Cont.) and Structures
C Programming:Part 3 Characters and Strings File Processing Exercise.
CS111 Computer Programming
What you need for the 1st phase of project
Chapter 11 – File Processing
Lecture 13 Input/Output Files.
Lecture 15 Files.
Text and Binary File Processing
Fundamental of Programming (C)
CS111 Computer Programming
C Input / Output Prabhat Kumar Padhy
ECE 103 Engineering Programming Chapter 51 Random Numbers
EPSII 59:006 Spring 2004.
File I/O.
Professor Jodi Neely-Ritz University of Florida
Presentation transcript:

CS1061: C Programming Lecture 19: Random Access Files A. O’Riordan, 2004, 2007 updated.

Random access files Problems with sequential access files: cannot be modified without the risk of destroying data. cannot jump straight to a particular location in a file. Solution: Random access files. Characteristics of random access: Data in random access files is unformatted (stored as "raw bytes"). All data of the same type (e.g. int) - uses the same amount of memory. Actually random access files are streams of chars since this works with every type. Data not human readable.

fwrite() Unformatted output function: fwrite() - transfer bytes from a location in memory to a file Prototype: int fwrite ( const void * array, size_t size, size_t count, FILE * stream ); Example: write an array of ints fwrite(arr, sizeof(int), num, myPtr); arr – location of variable to transfer bytes from sizeof(int) – size of data type num – for arrays, number of elements to transfer myPtr – output stream

fread() Unformatted output function: fread() - transfer bytes from a file to a location in memory Prototype: int fread ((void*) ptr, int size, int nitems, FILE* stream); fread() takes four arguments: The first is a void * - this could be an actual char pointer or a char array. The second argument is the size of data type, e.g. char is 1. The third argument in the number of elements to read. The last argument is the stream.

fread() (2) fread() returns the number of characters read. Simple example: reading 1 characters from file (inPtr) and storing in char c. fread(&c, 1, 1, inPtr); ~~~~~~~ For detecting the end of the file use feof() pass it a FILE pointer, and if you've reached the end of the file, feof() returns 1, else it returns 0.

fread() (3) #include int main() { FILE *file; char c[30]; /* make sure it is large enough */ int n; if(!(file = fopen("numbers.txt", "r") == NULL)){ n = fread(c, 1, 10, file); c[n] = '\0'; printf("%s\n", c); printf("Characters read: %d\n\n", n); fclose(file); }

Example Program: Count characters in file // count total number of character in a file #include int main(){ FILE * pFile; char c; int n = 0; while ((pFile = fopen("myfile.txt", "rb") != NULL){ fread(&c,1,1,pFile); n++; } fclose(pFile); printf("Total number of bytes: %d\n", n); }

fseek() fseek() sets file pointer to a specific position: fseek(stream, offset, symbolic_constant); stream – pointer to FILE offset – file position pointer (0 is first location) symbolic_constant – specifies from where in file offset is applied SEEK_SET – seek starts at beginning of file SEEK_CUR – seek starts at current location in file SEEK_END – seek starts at end of file

fseek() example Example – writes “This is a string” and changes it to “This is 1 string”. #include int main(){ FILE * pFile; char myString= “This is a string”; char one_literal = ‘1’; while ((pFile = fopen("myfile.txt", "wb")) != NULL){ fwrite(myString, 1, 17, pFile,); fseek(pFile, 9, SEEK_SET); fwrite(&one_literal,1,1,pFile); } fclose(pFile); return 0; }

sprintf() and sscanf() Formatted Input Output with Strings: These are the third set of the printf() and scanf() families. They are called sprintf() and sscanf(). int sprintf(char *s, const char *format,...); int sscanf(char *s, const char *format,...); sprintf() puts formatted data into a string which must have sufficient space allocated to hold it. The data is formatted according to a control string of the same form as that for printf(). sprintf() takes an extra first argument has to be a char * variable. sprintf() returns the number of characters in the string (not included the null character).

sscanf() sscanf() takes data from a string and stores it in other variables as specified by the control string. This is done in the same way that scanf() reads input data into variables. sscanf() is very useful for converting strings into numeric values - an alternative to atoi(). Example: char my_date[]="March 17"; char str[20]; int i; sscanf(my_date,"%s %d",str,&i); printf("%s %d\n",str,i);