File Access (7.5) CSE 2031 Fall 2010 22 July 2018.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Using Files Declare a variable, called file pointer, of type FILE * Use function fopen to open a named file and associate this file with the file pointer.
I/O means Input and Output. One way: use standard input and standard output. To read in data, use scanf() (or a few other functions) To write out data,
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.
C File Processing - I Math 130 B Smith: Eval: 3. Energy and excitement helped for avg graphics. Gen interest. A bit wordy, henced redundant in some parts.
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.
Display a 12-Month Calendar CS-2301 D-term Programming Assignment #2 12-Month Calendar CS-2301 System Programming C-term 2009 (Slides include materials.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
1 CSE1301 Computer Programming: Lecture 9 Input/Output.
Assignment #2, 12- month Calendar CS-2301, B-Term Programming Assignment #2 12-Month Calendar CS-2301, System Programming for Non-Majors (Slides.
Programming in C #2 Input / Output.
The printf Method The printf method is another way to format output. It is based on the printf function of the C language. System.out.printf(,,,..., );
Chapter 18 I/O in C. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Standard C Library I/O commands.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
Binary Search Tree For a node: The left subtree contains nodes with keys less than the node's key. The right subtree contains nodes with keys greater than.
20/03/01 Sudeshna Sarkar, CSE, IIT Kharagpur1 File Handling in C Lecture 17c 20/3/01.
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:
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:
CSE1301 Computer Programming: Lecture 6 Input/Output.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
CS 1704 Introduction to Data Structures and Software Engineering.
Files A collection of related data treated as a unit. Two types Text
CSCI N305: C Programming Copyright ©2006  Department of Computer & Information Science File Handling in C.
Lecture 20: C File Processing. Why Using Files? Storage of data in variables and arrays is temporary Data lost when a program terminates. Files are used.
1 Computer Programming Lecture 15 Text File I/O Assist. Prof Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering
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.
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
Formatted I/O ä ä Standard Output ä ä printf() family of functions ä ä Standard Input ä ä scanf() family of functions.
Formatted Input and Output
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.
Chapter 18 I/O in C.
File I/O.
Introduction to C CSE 2031 Fall /3/ :33 AM.
Plan for the Day: I/O (beyond scanf and printf)
CS111 Computer Programming
CSE1320 Files in C Dr. Sajib Datta
File Input/Output.
CSE1320 Files in C Dr. Sajib Datta
Computer Programming Lecture 15 Text File I/O
Programming in C Input / Output.
Input and Output Lecture 4.
Programming in C Input / Output.
Files I/O, Streams, I/O Redirection, Reading with fscanf
CSE1320 Files in C Dr. Sajib Datta
CSI 121 Structured Programming Language Lecture 7: Input/Output
Lecture 13 Input/Output Files.
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
File Input and Output.
File Handling.
File Access (7.5) CSE 2031 Fall January 2019.
File Access (7.5) CSE 2031 Fall February 2019.
Programming in C Input / Output.
File Handling in C.
Module 12 Input and Output
Introduction to C EECS May 2019.
CS1100 Computational Engineering
CSc 352 File I/O Saumya Debray Dept. of Computer Science
File I/O.
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
Professor Jodi Neely-Ritz University of Florida
Professor Jodi Neely-Ritz University of Florida
Files Chapter 8.
Presentation transcript:

File Access (7.5) CSE 2031 Fall 2010 22 July 2018

Declaring and Opening Files FILE *fp; /* file pointer */ FILE *fopen(char *name, char *mode); Example: FILE *ifp, *ofp; char iname[50], oname[50]; ifp = fopen( iname, "r" ); if ( ifp == NULL ) { ... } ofp = fopen( oname, "w" );

Modes fp = fopen( name, "r" ); Returns NULL if file does not exist, or has no read permission. fp = fopen( name, “w" ); If file does not exist, one will be created for writing. If file already exists, the content will be erased when the file is opened. So be careful! Returns NULL if file has no write permission.

Modes (cont.) fp = fopen( name, “a" ); /* append */ If file does not exist, one will be created for writing. If file already exists, the content will be preserved. Returns NULL if file has no write permission. May combine multiple modes. fp = fopen( name, "rw" ); File may be read first, but the old content will be erased as soon as something is written to the file. fp = fopen( name, "ra" ); fp = fopen( name, “aw" ); /* same as “a” */

Reading and Writing Files int getc( FILE *fp ) int putc( int c, FILE *fp ) int fscanf( FILE *fp, char *format, ... ) int fprintf( FILE *fp, char *format, ... ) int c; while ( (c = getc( ifp )) != EOF ) putc( c, ofp ); char ch; while ( fscanf( ifp, “%c”, &ch ) != EOF ) fprintf( ofp, “%c”, ch );

Closing Files int fclose( FILE *fp ) fclose( ifp ); fclose( ofp ); Most operating systems have some limit on the number of files that a program may have open simultaneously  free the file pointers when they are no longer needed. fclose is called automatically for each open file when a program terminates normally. For output files: fclose flushes the buffer in which putc is collecting output.

Reminder: I/O Redirection In many cases, I/O redirection is simpler than using file pointers. a.out < input_file > outout_file a.out < input_file >> outout_file

Review: printf()and scanf()

printf( ) (7.2) int printf(char *format, arg1, arg2, ...); converts, formats, and prints its arguments on the standard output under control of the format. returns the number of characters printed (usually we are not interested in the returned value).

printf( ) Examples printf(“:%s:”, “hello, world”); printf(“:%10s:”, “hello, world”); printf(“:%.10s:”, “hello, world”); printf(“:%-10s:”, “hello, world”); printf(“:%.15s:”, “hello, world”); printf(“:%-15s:”, “hello, world”); printf(“:%15.10s:”, “hello, world”); printf(“:%-15.10s:”, “hello, world”);

printf Conversions

Output Formatting with printf( ) A minus sign, which specifies left adjustment of the converted argument. A number that specifies the minimum field width. The converted argument will be printed in a field at least this wide. If necessary it will be padded on the left (or right, if left adjustment is called for) to make up the field width. A period, which separates the field width from the precision. A number, the precision, that specifies the maximum number of characters to be printed from a string, or the number of digits after the decimal point of a floating-point value, or the minimum number of digits for an integer.

scanf Conversions (7.4)

scanf( ) Examples int num; scanf("%d”, &num); char c; float f; scanf("%c %f”, &c, &f); long number; scanf("%ld”, &number) double dnum; scanf("%lf”, &dnum) char strg[100]; scanf(“%s”, strg); Notes: no & in front of variable (why?) ‘\0’ is added automatically to end of string.

scanf( ) int scanf(char *format, arg1, arg2, ...); reads characters from the standard input, interprets them according to the specification in format, and stores the results through the remaining arguments. stops when it exhausts its format string, or when some input fails to match the control specification. returns the number of successfully matched and assigned input items (e.g., to decide how many items were found). returns 0 if the next input character does not match the first specification in the format string (i.e., an error). On the end of file, EOF is returned. Note: arg1, arg2, ... must be pointers!

Final Exam December 15, 9am – 12pm. Location: check Registrar’s office web site. Written exam only.