File Input/Output. File information Name z:\Visual Studio 2005\Projects\fileio\fileio\myinput.txt \\file-svr-1\class\ECE-160\pviall\myoutput.txt Read/Write.

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

UNIT 15 File Processing.
BITS Pilani, Pilani Campus TA C252 Computer Programming - II Vikas Singh File Handling.
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.
Lab 8 User Defined Function.
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.
File Input/Output. External Files Batch –Requires use of data files (save to disk) –Batch can be run during off peak use –allows things to be complete.
1 Text File I/O Chapter 6 Pages File I/O in an Object-Oriented Language Compare to File I/O in C. Instantiate an ofstream object. Like opening.
KUKUM Sem2-05/06EKT120: Computer Programming1 Week 11 – Files.
File processing with functions H&K Chapter 3 Instructor – Gokcen Cilingir Cpt S 121 (June 27, 2011) Washington State University.
1 ICS103 Programming in C Lecture 8: Data Files. 2 Outline Why data files? Declaring FILE pointer variables Opening data files for input/output Scanning.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
1 CSE1301 Computer Programming: Lecture 21 File I/O.
Input/Output Functions Selim Aksoy Bilkent University Department of Computer Engineering
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
CSE1301 Computer Programming: Lecture 19 File I/O
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
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.
Lecture 11 – Files Operation. Introduction Almost all of the program developed before this is interactive In interactive environment, input is via keyboard.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students C File Input and Output Checking input for errors.
Introduction to Perl Practical Extraction and Report Language or Pathologically Eclectic Rubbish Lister or …
1 CSE1301 Computer Programming: Lecture 19 File I/O.
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.
File I/O 11_file_processing.ppt
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 7P. 1Winter Quarter File I/O in C Lecture.
Topics 1.File Basics 2.Output Formatting 3.Passing File Stream Objects to Functions 4.More Detailed Error Testing 5.Member Functions for Reading and 6.Writing.
1 Lecture09: File I/O 5/6/2013 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
1 ICS103 Programming in C Ch6: Pointers and Modular Programming.
CSE1301 Computer Programming: Lecture 14 I/O and Files.
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 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:
Lecture 11: Files & Arrays B Burlingame 18 November 2015.
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:
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Characters and Strings Functions.
1 CSC103: Introduction to Computer and Programming Lecture No 28.
Files A collection of related data treated as a unit. Two types Text
UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming1 Files Operations.
1 Computer Programming Lecture 15 Text File I/O Assist. Prof Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering
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.
CSC111 Quick Revision.
DKT121: Fundamental of Computer Programming
ECE Application Programming
Formatted Input/Output
ECE Application Programming
Chapter 7 Text Input/Output Objectives
Structured Programming II
CS1010 Programming Methodology
Chapter 18 I/O in C.
File I/O.
Formatted Input/Output
File Input/Output.
Computer Programming Lecture 15 Text File I/O
Programming in C 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.
Standard Input/Output Stream
Input/Output Functions
EECE.2160 ECE Application Programming
Input/Output Functions
Characters and Strings Functions
Files Operations.
Introduction to C Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
ICS103: Programming in C 6: Pointers and Modular Programming
Files Chapter 8.
Presentation transcript:

File Input/Output

File information Name z:\Visual Studio 2005\Projects\fileio\fileio\myinput.txt \\file-svr-1\class\ECE-160\pviall\myoutput.txt Read/Write Type (binary or ASCII text) Access (security; single/multiple user) Position in file All above info is stored in a FILE type variable, pointed to by a file handle

File i/o function calls fopen( filename, file_access ) filename: is name of file file_access is: “r+t” for reading text file “w+t” for writing text file many other options available returns: file_handle, that is the address of FILE (a FILE * ) on success NULL (zero) on failure

File i/o function calls fclose(file_handle) Closes a file This is recommended for input files (to free up system resources) This is required for output files (as often times the O/S does not write the last bit of a file out to the disk until the file is closed

File i/o function calls fprintf( file_handle, format_specifier, 0 or more variables ) file_handle: is address returned by openf() format_specifier: same as for printf() 0 or more variables: same as printf()

Sample program Read three integer values from the file myinput.txt Determine sum and average Write the original three values as well as the sum and average to the file myoutput.txt

The program (part 1) #define _CRT_SECURE_NO_DEPRECATE #include void main() { FILE *infile; FILE *outfile; int x,y,z,sum; float avg; // Open input file, exit if error infile=fopen("myinput.txt","r+t"); if (infile==NULL) { printf("Error opening myinput.txt\n"); exit(0); } // Generally file opens are done as below if ((outfile=fopen("myoutput.txt","w+t"))==NULL) { printf("Error opening myoutput.txt\n"); exit(0); }

The program (part 2) // read the three values // its a good idea to account for \n's in the file fscanf(infile,"%d\n",&x); fscanf(infile,"%d\n",&y); fscanf(infile,"%d\n",&z); // sum and avg sum = x+y+z; avg = (float)sum/3.0; // print out values fprintf(outfile,"Values: %d, %d, %d\n",x,y,z); fprintf(outfile,"Sum: %d\n",sum); fprintf(outfile,"Avg: %7.2f\n",avg); // close the files fclose(infile); fclose(outfile); }

Adding a text file to the project (way 1) Use a text editor (notepad, wordpad, etc) to create a text file. Save the file in the proper directory. If your project is named fileio, the proper directory and filename would be: Z:\Visual Studio 2005\Projects\fileio\fileio\myinput.txt

Adding a text file to the project (way 2) 1.Project 2.Add new item 3.Utility 4.Text file 5.Enter file name This creates another tab where you may input your myinput.txt file

Viewing your myoutput.txt file (way 2) 1.Project 2.Add existing item 3.Change file type to all files 4.Click myoutput.txt – this will cause myoutput.txt to appear in the file list on the left 5.Enter file name This creates another tab where you may input your myinput.txt file