CSE1320 Files in C Dr. Sajib Datta CSE@UTA.

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.
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.
Chapter 11: Data Files & File Processing In this chapter, you will learn about Files and streams Creating a sequential access file Reading data from a.
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.
CS1061 C Programming Lecture 17: Steams and Character I/O A. O’Riordan, 2004.
File I/O.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students C File Input and Output Checking input for errors.
1 Homework Introduction to HW7 –Complexity similar to HW6 –Don’t wait until last minute to start on it File Access will be needed in HW8.
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 Handling Spring 2013Programming and Data Structure1.
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.
1 Lecture09: File I/O 5/6/2013 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
File IO and command line input CSE 2451 Rong Shi.
CSEB114: Principle of programming Chapter 11: Data Files & File Processing.
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.
Lecture 8a: File I/O BJ Furman 21MAR2011. Learning Objectives Explain what is meant by a data stream Explain the concept of a ‘file’ Open and close files.
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 7 : File Processing1 File-Oriented Input & Output CHAPTER 7.
1 CHAPTER6 CHAPTER 6. Objectives: You’ll learn about;  Introduction  Files and streams  Creating a sequential access file  Reading data from a sequential.
Lecture 11: Files & Arrays B Burlingame 18 November 2015.
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.
CNG 140 C Programming (Lecture set 10) Spring Chapter 10 Data Files.
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:
Structured Programming Approach Module VIII - Additional C Data Types File Handling Prof: Muhammed Salman Shamsi.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
Files A collection of related data treated as a unit. Two types Text
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.
IO revisited CSE 2451 Rong Shi. stdio.h Functions – printf – scanf(normally stops at whitespace) – fgets – sscanf Standard streams – stdin(defaults to.
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.
6/9/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 10 (C-4)
RECURSION Recursion is a process where a function calls itself. main()
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.
File Access (7.5) CSE 2031 Fall July 2018.
File I/O.
Input/Output (Continued)
CSC215 Lecture Input and Output.
Plan for the Day: I/O (beyond scanf and printf)
CS111 Computer Programming
File Input/Output.
CSE1320 Files in C Dr. Sajib Datta
Computer Programming Lecture 15 Text File I/O
Programming in C Input / Output.
What you need for the 1st phase of project
File Management in C.
Files I/O, Streams, I/O Redirection, Reading with fscanf
CSE1320 Files in C Dr. Sajib Datta
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
CSC215 Lecture Input and Output.
CSE1320 Strings Dr. Sajib Datta
CSC215 Lecture Input and Output.
Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
FILE HANDLING IN C.
Programming and Data Structure
File Input and Output.
File Handling.
Line at a time I/O with fgets() and fputs()
Programming in C Input / Output.
File Handling in C.
Module 12 Input and Output
ETE 132 Lecture 8 By Munirul Haque.
CS1100 Computational Engineering
CSc 352 File I/O Saumya Debray Dept. of Computer Science
Professor Jodi Neely-Ritz University of Florida
Presentation transcript:

CSE1320 Files in C Dr. Sajib Datta CSE@UTA

Files in C A file is viewed as a stream of bytes by a C program. When a C program is executed, three files are automatically opened: Standard input, input from the keyboard Standard output, output to the monitor screen Standard error, be associated with the terminal output They are referenced with the identifiers stdin, stdout and stderr, respectively, having the FILE * type.

Standard files – Standard I/O standard input (stdin) – the normal input device for your system, e.g., keyboard scanf(), getchar(), gets() standard output (stdout) – the normal output device for your system, e.g., display screen printf(), putchar(), puts()

#include <stdio.h> int main(void) { char mych[2]; scanf("%c", &mych[0]); mych[1] = getchar(); printf("The char arry has %c and %c.\n", mych[0], mych[1]); return 0; }

Files Operation in C Program When working with files in C, declare a FILE * variable – file identifier FILE * fp; scanf(), getchar(), gets() Then building a connection for the identifier with a file using fopen( )

Associate the variable with a file FILE * fopen (char * filename, char * mode ); Specify the file path and the mode If successful, fopen returns a file pointer; otherwise, NULL is returned: a file we wish to read may not exist a file we wish to write to may be in use by another program

fclose() Use fclose() when you are finished working with the files int fclose ( FILE * stream ); Return Value If the stream is successfully closed, a zero value is returned. On failure, EOF is returned.

#include <stdio.h> #include <stdlib.h> int main(void) { FILE *fp; if((fp = fopen("info", "r")) == NULL) printf("Input file could not be opened.\n"); exit(1); } if(fclose(fp) == EOF) printf("File couldn't be closed.\n"); return 0;

exit() exit() function causes the program to terminate, closing any open files. Passing a value of zero for programs that terminate normally, and a non-zero value for abnormal termination Different from return, for a recursive program, return will pass flow control to the previous level of recursion until the original level is reached. exit() still terminates the program.

Character input and output To read in or write out text by character, use getc( ) and putc( ) char getc ( FILE * stream ); returns the char, if successful; otherwise, EOF (the end of the file) ch = getc(stdin) is equivalent to ch = getchar() Before use getc(), a call to fopen() should be made to open a file for reading. char putc ( char character, FILE * stream ); If successful, the character is returned; otherwise, EOF is returned putc(ch,stdout) is equivalent to putchar(ch), where ch is a character, for example, char ch = ‘a’; Before use putc(), the file must have been opened for writing. Both in stdio.h

getc() and putc() #include <stdio.h> int main(void) { char mych; mych = getc(stdin);// mych = getchar(); putc(mych, stdout);// putchar(mych); return 0; }

The End of File EOF EOF indicates the end of the file To check for the end of the file, two approaches use (ch = getc(fp)) == EOF int feof ( FILE * stream ); A non-zero value is returned in the case that the End-of-File indicator associated with the stream is set. Otherwise, a zero value is returned. Included in stdio.h You can learn more about EOF from http://en.wikipedia.org/wiki/End-of-file

#include <stdio.h> #include <stdlib.h> int main(void) { char ch; FILE * fp; fp = fopen("myfiles.txt","r"); ch = getc(fp); while(ch != EOF) putchar(ch); } return 0;

Find the number of a specific character in a file #include <stdio.h> #include <stdlib.h> int main(void) { char mych,ch; int counter = 0; FILE * fp = fopen("myfiles.txt", "r"); if (fp == NULL) printf("Can't open the file.\n"); exit(1); } printf("Please input a character:"); scanf("%c", &mych); ch = getc(fp); while(ch != EOF) if (ch == mych) counter++; printf("Totally, there are %d characters of '%c'.\n", counter, mych); fclose(fp); return 0;

String input and output char * fgets ( char * str, int num, FILE * stream ); Reads characters from stream and stores them as a C string into str until (num-1) characters have been read or either newline or the End-of-File (EOF) is reached, whichever comes first. A newline character is a valid character! Different from gets() function. A null character is automatically appended to str. Return data On success, the function returns str. If the end-of-file is encountered while attempting to read a character, the eof indicator is set (feof). If this happens before any characters could be read, the pointer returned is a null pointer (and the contents of str remain unchanged). If a read error occurs, the error indicator (ferror) is set and a null pointer is also returned (but the contents pointed by str may have changed).

Count characters on each line #include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { char chbuffer[100];// assume the number of characters in each line < 100 int counterline = 0, counterch; FILE * fp = fopen("myfiles.txt", "r"); if (fp == NULL) printf("Can't open the file.\n"); exit(1); } while (fgets(chbuffer, 100, fp) != NULL) counterline++; counterch = strlen(chbuffer); printf("Line %d has %d characters.\n", counterline, counterch); fclose(fp); return 0; Soft assignment: How to code the program to remove the assumption!!!!