Programming in C Input / Output.

Slides:



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

Programming in C #2 Input / Output.
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
Chapter 18 I/O in C. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Standard C Library I/O commands.
CMPE13 Cyrus Bazeghi Chapter 18 I/O in C. CMPE Standard C Library I/O commands are not included as part of the C language. Instead, they are part.
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.
Chapter 18 I/O in C.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
File IO and command line input CSE 2451 Rong Shi.
3. FORMATTED INPUT/OUTPUT. The printf Function The first argument in a call of printf is a string, which may contain both ordinary characters and conversion.
Chapter 3 Input and Output
Basic I/O in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Stream Model of I/O header file: A stream provides a connection.
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()”
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
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.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic properties and characteristics of external files ❏ To.
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.
Characters and Strings
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.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
Chapter 18 I/O in C Original slides from Gregory Byrd, North Carolina State University Modified slides by C. Wilcox, Y. Malaiya Colorado State University.
Formatted I/O ä ä Standard Output ä ä printf() family of functions ä ä Standard Input ä ä scanf() family of functions.
Introduction to Computing Lecture 03: Basic input / output operations Introduction to Computing Lecture 03: Basic input / output operations Assist.Prof.Dr.
C Formatted Input/Output
Formatted Input and Output
Lesson #2 Introduction to C.
Lesson #2 Introduction to C.
Chapter 7 Text Input/Output Objectives
Chapter 7 Text Input/Output Objectives
Chapter 9 C Formatted Input/Output
Characters and Strings
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.
Input/output.
Chapter 7 Text Input/Output Objectives
File Access (7.5) CSE 2031 Fall July 2018.
Chapter 18 I/O in C.
Introduction to C CSE 2031 Fall /3/ :33 AM.
Plan for the Day: I/O (beyond scanf and printf)
CS111 Computer Programming
File Input/Output.
Programming in C Input / Output.
Input and Output Lecture 4.
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Formatted Input/Output
Programming in C Input / Output.
CSI 121 Structured Programming Language Lecture 7: Input/Output
Lecture 13 Input/Output Files.
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Characters and Strings
Chapter 18 I/O in C.
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
File Input and Output.
File Handling.
Chapter 18 I/O in C.
Formatted Input/Output
Formatted Input/Output
Lesson #2 Introduction to C.
Module 12 Input and Output
Introduction to C EECS May 2019.
Introduction to C Programming
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Professor Jodi Neely-Ritz University of Florida
Files Chapter 8.
Chapter 18 I/O in C.
Presentation transcript:

Programming in C Input / Output

stdin, stdout, stderr When your C program begins to execute, three input/output devices are opened automatically. stdin The “standard input” device, usually your keyboard stdout The “standard output” device, usually your monitor stderr The “standard error” device, usually your monitor Some C library I/O functions automatically use these devices 7/28/09

Formatted Console Output In Java, formatted output is created using System.out.printf( ). In C formatted formatted output is created using the printf( ) function. printf( ) outputs text to stdout The basic function call to printf( ) is of the form printf( format, arg1, arg2, … ) where the format is a string containing literals to be printed and conversion specifications 7/28/09

printf( ) conversions Conversions specifications begin with % and end with a conversion character. Between the % and the conversion character MAY be, in order A minus sign specifying left-justification The minimum field width A period separating the field width and precision The precision that specifies Maximum characters for a string Number of digits after the decimal for a floating point Minimum number of digits for an integer An h for “short” or an l (letter ell) for long See K&R section 7.2 and appendix section B1.2 7/28/09

Common printf( ) Conversions %d -- the int argument is printed as a decimal number %u -- the int argument is printed as an unsigned number %s -- prints characters from the string until ‘\0’ is seen or the number of characters in the (optional) precision have been printed %f -- the double argument is printed as a floating point number %x, %X -- the int argument is printed as a hexadecimal number %c - the int argument is printed as a single character %p - the pointer argument is printed (implementation dependent) 7/28/09

printf( ) Examples #define NAME “Bob” int anInt = 5678; double aDouble = 4.123; /* what is the output from each printf( ) statement? */ printf(“Jim said %s\n”, “Hello Bob”); printf(“Jim said Hello %s\n”, NAME); printf(“Jim said %s\n”, “Hello NAME”); printf (“%8d is a large number\n”, anInt); printf (“%-8d is a large number\n”, anInt); printf( “The sum of %d and %8.4f is %5.2f\n”, anInt, aDouble, anInt + aDouble); 7/28/09

Formatted Output Example Use field widths to align output in columns int i; for (i = 1 ; i < 5; i++) printf("%2d %10.6f %20.15f\n", i,sqrt(i),sqrt(i)); 12 1234567890 12345678901234567890 1 1.000000 1.000000000000000 2 1.414214 1.414213562373095 3 1.732051 1.732050807568877 4 2.000000 2.000000000000000 7/28/09

Keyboard Input In C, keyboard input is accomplished using the scanf( ) function. scanf reads user input from stdin Calling scanf( ) is similar to calling printf( ) scanf( format string, arg1, arg2, ... ) The format string has a similar structure to the format string in printf( ). The arguments are the addresses of the variables into which the input is store. Recall that the name of an array is synonymous with the array’s address. See K & R section 7.4 and Appendix section B1.3 for a detailed description of scanf( ) 7/28/09

scanf( ) format string The scanf( ) format string usually contains conversion specifications that tell scanf( ) how to interpret the next input field. An “input field” is a string of non-whitespace characters. The format string usually contains Blanks or tabs which are ignored Ordinary characters which are expected to match the next (non-whitespace) character input by the user Conversion specifications usually consisting % character indicating the beginning of the conversion An optional h, l (ell) or L A conversion character which indicates how the input field is to be interpreted. 7/28/09

Common scanf( ) conversions %d -- a decimal (integer) number %u - an unsigned decimal (integer) number %x -- a hexadecimal number The matching argument is the address of an int May be preceded by h to indicate that the argument is the address of a short or by l (ell) to indicate that the argument is the address of a long rather than an int %s -- a word (a string delimited by white space, not a line) The matching argument is the address of a char or the name of a char array The caller must insure the array is large enough to for the input string and the terminating \0 character %f, %e -- a floating point number with optional sign, optional decimal point, and optional exponent The matching argument is the address of a float May be preceded by l (ell) to indicate the argument is of the address of a double rather than a float %c - a single character The matching arguments is the address of a char Does not skip over white-space 7/28/09

scanf( ) examples int age; double gpa; char name[42]; printf(“Input your age: “); scanf( “%d”, &age ); /* note & */ printf(“ input your gpa: “); scanf (“%lf”, &gpa ); printf(“Input your name: “); scanf( “%s”, name ); /* no “&” */ 7/28/09

gets( ) to read a line The gets( ) function is used to read a line of input from stdin until the \n character is encountered. Caller must insure that the char array is big enough to hold the input string #include <stdio.h> char myString[ 101 ]; gets( myString ); 7/28/09

Unix input redirection By default, stdin is associated with the user’s keyboard, but Unix allows us to redirect stdin to read data from a file when your program is executed. All scanf( ) statements in your program read from this file instead of the user’s keyboard, otherwise your program is unaffected. Redirecting input from a file is useful for debugging -- you don’t have to continually retype your input. Suppose your program’s name is Project1 and you wish to get your input from a file named data1. To redirect stdin to read from data1, use the command Project1 < data1 at the Unix prompt 7/28/09

Unix output redirection By default, stdout is associated with the user’s console, but Unix allows us to redirect stdout to output text to a file when your program is executed. All printf( ) statements in your program output to this file instead of the user’s console, otherwise your program is unaffected. Suppose your program’s name is Project1 and you wish to write your output to a file named logfile1. To redirect stdout to write to logfile1, use the command Project1 > logfile at the Unix prompt Can you redirect both input and output? Yes. For this exampe, the command would be Project 1 < data1 > logfile1 7/28/09

Text File I/O Reading and writing from/to a text file is similar to getting input from stdin (with scanf) and writing to stdout (with printf). Reading data from a text file is accomplished with the function fscanf( ). This function works the same as scanf( ), but requires an additional parameter which is a “handle” to the file. Reading a line from a text file is accomplished using the fgets( ) function. This function is similar to gets( ) but requires a “handle” to a file and a max character count. Similarly, writing to a text file is accomplished with the function fprintf() which works the same as printf( ), but also requires a “handle” to the file to be read. In fact, printf( ) and scanf( ) are just specialized versions of fprintf( ) and fscanf( ) respectively. 7/28/09

Opening and Closing To read or write from a text file using fscanf( ), fegets( ) or fprintf( ), the file must first be opened using fopen( ). The file should be closed using fclose( ) when all I/O is complete. fopen( ) returns a handle to the file as the type FILE* (a pointer to a FILE struct) which is then used as the argument to fscanf(), fgets( ), fprintf( ) and fclose( ). The return value from fopen( ) should be checked to insure that the file was in fact opened. 7/28/09

fopen( ) fopen( ) requires two parameters See K & R appendix B1.1 The name of the text file to be opened The text file open “mode” “r” - open the file for reading only “w” - create the file for writing; if the file exists, discard the its contents “a” - append; open or create the file for writing at the end “r+” - open the file for reading and writing “w+” - create the file for reading and writing; if the file exists, discard its contents “a+” - open or create the file for reading or writing at the end See K & R appendix B1.1 1/22/10

Using fopen( ) Open the file named “bob.txt” for reading FILE * myFile = fopen( “bob.txt”, “r”); If fopen( ) fails, the special value NULL is returned. All calls to fopen should be checked FILE *myFIle = fopen (“bob.txt”, “r”) If (myFile == NULL) { /* handle the error */ } 1/22/10

fscanf.c #include <stdio.h> #include <stdlib.h> /* exit */ int main ( ) { double x ; FILE *ifp ; /* try to open the file for reading, check if successful */ /* if it wasn't opened exit gracefully */ ifp = fopen("test_data.dat", "r") ; if (ifp == NULL) { printf ("Error opening test_data.dat\n"); exit (-1); } fscanf(ifp, "%lf", &x) ; /* read one double from the file */ fclose(ifp); /* close the file when finished */ /* check to see what you read */ printf("x = %.2f\n", x) ; return 0; 7/28/09

fgets( ) #include <stdio.h> #include <stdlib.h> /* exit */ int main ( ) { double x ; FILE *ifp ; char myLine[42 + 1 ]; /* + 1 for terminating \0 */ ifp = fopen("test_data.dat", "r") ; fgets(myLine, 42, ifp ); /* read up to 42 chars*/ fclose(ifp); /* close the file when finished */ /* check to see what you read */ printf(”myLine = %s\n”, myLine); return 0; } 7/28/09

Detecting end-of-file with fscanf When reading an unknown number of data elements from a file using fscanf( ), we need a way to determine when the file has no more data to read, i.e, we have reached the “end of file”. Fortunately, the return value from fscanf( ) holds the key. fscanf( ) returns an integer which is the number of data elements read from the file. If end-of-file is detected the integer return value is the special value EOF 7/28/09

EOF example code /* code snippet that reads an undetermined number of integer student ages from a file and prints them out as an example of detecting EOF*/ FILE *inFile; inFile = fopen( “myfile”, “r” ); /* check that the file was opened */ int age; while ( fscanf(infile, “%d”, &age ) != EOF ) printf( “%d\n”, age ); fclose( inFile ); 7/28/09

Detecting EOF with fgets( ) fgets( ) returns the memory address in which the line was stored (the char array provided). However, when fgets( ) encounters EOF, the special value NULL is returned. FILE *inFile; inFile = fopen( “myfile”, “r” ); /* check that the file was opened */ char string[120 + 1]; while ( fgets(string, 120, inFile ) != NULL ) printf( “%s\n”, string ); fclose( inFile ); 7/28/09

fprintf.c /* fprintf.c */ #include <stdio.h> #include <stdlib.h> /* exit */ int main ( ) { double pi = 3.14159 ; FILE *ofp ; /* try to open the file for writing, check if successful */ /* if it wasn't exit gracefully */ ofp = fopen("test.out", “w") ; if (ofp == NULL) { printf ("Error opening test.out\n"); exit (-1); } /* write to the file using printf formats */ fprintf(ofp, “Hello World\n”); fprintf(ofp, “PI is defined as %6.5lf\n”, pi); fclose(ofp); /* close the file when finished reading */ return 0; 7/28/09

fprintf vs printf fscanf vs scanf Function prototypes are identical except that fprintf and fscanf require FILE* parameter Format strings identical fscanf, fprintf are more general printf can be written using fprintf fprintf( stdout, ....) Similarly, scanf can be written using fscanf fscanf( stdin, .... ) 7/28/09