UNIVERSITY OF CALIFORNIA, SANTA CRUZ BOARD OF STUDIES IN COMPUTER ENGINEERING CMPE-13/L: COMPUTER SYSTEMS AND C PROGRAMMING SPRING 2012

Size: px
Start display at page:

Download "UNIVERSITY OF CALIFORNIA, SANTA CRUZ BOARD OF STUDIES IN COMPUTER ENGINEERING CMPE-13/L: COMPUTER SYSTEMS AND C PROGRAMMING SPRING 2012"

Transcription

1 UNIVERSITY OF CALIFORNIA, SANTA CRUZ BOARD OF STUDIES IN COMPUTER ENGINEERING CMPE-13/L: COMPUTER SYSTEMS AND C PROGRAMMING SPRING 2012 NOTES TO ACCOMPANY THE C PROGRAMMING LANGUAGE, BY KERNIGHAN AND RITCHIE ( K&R ) Chapter 7: Input and Output BY STEVE SUMMIT Page 151: By Input and output facilities are not part of the C language itself, we mean that things like printf are just function calls like any other. C has no built-in input or output statements. For our purposes, the implications of this fact--that I/O is not built in--is mainly that the compiler may not do as much checking as we might like it to. If we accidentally write double d = 1.23; printf("%d\n", d); the compiler says, Hmm, a function named printf is being called with a string and a double. Okay by me. The compiler does not (and, in general, could not even if it wanted to) notice that the %d format requires an int. Although the title of this chapter is Input and Output, it appears that we ll also be meeting a few other routines from the standard library. If you start to do any serious programming on a particular system, you ll undoubtedly discover that it has a number of more specialized input/output (and other system-related) routines available, which promise better performance or nicer functionality than the pedestrian routines of C s standard library. You should resist the temptation to use these nonstandard routines. Because the standard library routines are defined precisely and exist in compatible form on any system where C exists, there are some real advantages to using them. (On the other hand, when you need to do something which C s standard library routines don t provide, you ll generally turn to your machine s system-specific routines right away, as they may be your only choice. One common example is when you d like to read one character immediately, without waiting for the RETURN key. How you do that depends on what system you re using; it is not defined by C.) Section 7.1: Standard Input and Output Note that a text stream might refer to input (to the program) from the keyboard or output to the screen, or input and output from files on disk. (For that matter, it can also refer to input and output from other peripheral devices, or the network.) Note that the stdio library generally does newline translation for you. If you know that lines are terminated by a linefeed on Unix and a carriage return on the Macintosh and a carriage-return/linefeed combination on MS-DOS, you don t have to worry about these things in C, because the line termination will always appear to a C program to be a single \n. (That is, when reading, a single \n represents the

2 end of the line being read, and when writing, writing a \n causes the underlying system s actual end-ofline representation to be written.) Pages : The lower program is an example of a filter: it reads its standard input, filters (that is, processes) it in some way, and writes the result to its standard output. Filters are designed for (and are only really useful under) a command-line interface such as the Unix shells or the MS-DOS command.com interface. Obviously, you would rarely invoke a program like lower by itself, because you would have to type the input text at it and you could only see the output ephemerally on your screen. To do any real work, you would always redirect the input: lower < inputfile and perhaps the output: lower < inputfile > outputfile (notice that spaces may precede and follow the < and > characters). Or, a filter program like lower might appear in a longer pipeline: or oneprogram lower anotherprogram anotherprogram < inputfile lower thirdprogram > outputfile Filters like these are not terribly useful, though, under a Graphical User Interface such as the Macintosh or Microsoft Windows. Section 7.2: Formatted Output -- Printf Pages : To summarize the important points of this section: printf s output goes to the standard output, just like putchar. Everything in printf s format string is either a plain character to be printed as-is, or a %- specifier which generally causes one argument to be consumed, formatted, and printed. (Occasionally, a single %-specifier consumes two or three arguments if the width or precision is *, or zero arguments if the specifier is %%.) There s a fairly long list of conversion specifiers; see the table on page 154. Always be careful that the conversions you request (in the format string) match the arguments you supply. You can print to a string (instead of the standard output) with sprintf. (This is the usual way of converting numbers to strings in C; the itoa function we were playing with in section 3.6 on page 64 is nonstandard, and unnecessary.) Section 7.3: Variable-length Argument Lists

3 This is an advanced section which you don t need to read. Section 7.4: Formatted Input -- Scanf Page 157: Somehow we ve managed to make it through six chapters without meeting scanf, which it turns out is just as well. In the examples in this book so far, all input (from the user, or otherwise) has been done with getchar or getline. If we needed to input a number, we did things like char line[maxline]; int number; getline(line, MAXLINE); number = atoi(line); Using scanf, we could simplify this to int number; scanf("%d", &number); This simplification is convenient and superficially attractive, and it works, as far as it goes. The problem is that scanf does not work well in more complicated situations. In section 7.1, we said that calls to putchar and printf could be interleaved. The same is not always true of scanf: you can have baffling problems if you try to intermix calls to scanf with calls to getchar or getline. Worse, it turns out that scanf s error handling is inadequate for many purposes. It tells you whether a conversion succeeded or not (more precisely, it tells you how many conversions succeeded), but it doesn t tell you anything more than that (unless you ask very carefully). Like atoi and atof, scanf stops reading characters when it s processing a %d or %f input and it finds a non-numeric character. Suppose you ve prompted the user to enter a number, and the user accidentally types the letter `x. scanf might return 0, indicating that it couldn t convert a number, but the unconvertable text (the `x ) remains on the input stream unless you figure out some other way to remove it. For these reasons (and several others, which I won t bother to mention) it s generally recommended that scanf not be used for unstructured input such as user prompts. It s much better to read entire lines with something like getline (as we ve been doing all along) and then process the line somehow. If the line is supposed to be a single number, you can use atoi or atof to convert it. If the line has more complicated structure, you can use sscanf (which we ll meet in a minute) to parse it. (It s better to use sscanf than scanf because when sscanf fails, you have complete control over what you do next. When scanf fails, on the other hand, you re at the mercy of where in the input stream it has left you.) With that little diatribe against scanf out of the way, here are a few comments on individual points made in section 7.4. We ve met a few functions (e.g. getline, month_day in section 5.7 on page 111) which return more than one value; the way they do so is to accept a pointer argument that tells them where (in the caller) to write the returned value. scanf is the epitome of such functions: it returns potentially many values (one for each %-specifier in its format string), and for each value converted and returned, it needs a pointer argument.

4 The statement on page 157 that blanks or tabs in the format string are ignored (which is repeated on page 159) is a simplification: in actuality, a blank or tab (or newline; actually any whitespace) in the format string causes scanf to skip whitespace (blanks, tabs, etc.) in the input stream. A * character in a scanf conversion specifier means something completely different than it does for printf: for scanf, it means to suppress assignment (i.e. for that conversion specifier, there isn t a pointer in the argument list to receive the converted value, so the converted value is discarded). With scanf, there is no direct way of taking a field width from the argument list, as * does for printf. Conversion specifiers like %d and %f automatically skip leading whitespace while looking for something to convert. This means that the format strings "%d %d" and "%d%d" act exactly the same-- the whitespace in the first format string causes whitespace to be skipped before the second %d, but the second %d would have skipped that whitespace anyway. (Yet another scanf foible is that the innocuouslooking format string "%d\n" converts a number and then skips whitespace, which means that it will gobble up not only a newline following the number it converts, but any number of newlines or whitespace, and in fact it will keep reading until it finds a non-whitespace character, which it then won t read. This sounds confusing, but so is scanf s behavior when given a format string like "%d\n". The moral is simple: don t use trailing \n s in scanf format strings.) Page 158: Notice that, for scanf, the %e, %f, and %g formats are all the same, and signify conversion of a float value (they accept a pointer argument of type float *). To convert a double, you need to use %le, %lf, or %lg. (This is quite different from the printf family, which uses %e, %f, and %g for floats and doubles, though all three request different formats. Furthermore, %le, %lf, and %lg are technically incorrect for printf, though most compilers probably accept them.) Page 159: More precisely, the reason that you don t need to use a & with monthname is that an array, when it appears in an expression like this, is automatically converted to a pointer. The dual-format date conversion example in the middle of page 159 is a nice example of the advantages of calling getline and then sscanf. At the beginning of this section, I said that when sscanf fails, you have complete control over what you do next. Here, what you do next is try calling sscanf again, on the very same input string (thus effectively backing up to the very beginning of it), using a different format string, to try parsing the input a different way. Section 7.5: File Access Page 160: We ve come an amazingly long way without ever having to open a file (we ve been relying exclusively on those predefined standard input and output streams) but now it s time to take the plunge. The concept of a file pointer is an important one. It would theoretically be possible to mention the name of a file each time it was desired to read from or write to it. But such an approach would have a number of drawbacks. Instead, the usual approach (and the one taken in C s stdio library) is that you mention the name of the file once, at the time you open it. Thereafter, you use some little token--in this case, the file pointer--which keeps track (both for your sake and the library s) of which file you re talking about. Whenever you want to read from or write to one of the files you re working with, you identify

5 that file by using the file pointer you obtained from fopen when you opened the file. (It is possible to have several files open, as long as you use distinct variables to store the file pointers.) Not only do you not need to know the details of a FILE structure, you don t even need to know what the buffer is that the structure contains the location of. In general, the only declaration you need for a file pointer is the declaration of the file pointer: FILE *fp; You should never need to type the line FILE *fopen(char *name, char *mode); because it s provided for you in <stdio.h>. If you skipped section 6.7, you don t know about typedef, but don t worry. Just assume that FILE is a type, like int, except one that is defined by <stdio.h> instead of being built into the language. Furthermore, note that you will never be using variables of type FILE; you will always be using pointers to this type, or FILE *. A binary file is one which is treated as an arbitrary series of byte values, as opposed to a text file. We won t be working with binary files, but if you ever do, remember to use fopen modes like "rb" and "wb" when opening them. Page 161: We won t worry too much about error handling for now, but if you start writing production programs, it s something you ll want to learn about. It s extremely annoying for a program to say can t open file without saying why. (Some particularly unhelpful programs don t even tell you which file they couldn t open.) On this page we learn about four new functions, getc, putc, fprintf, and fscanf, which are just like functions that we ve already been using except that they let you specify a file pointer to tell them which file (or other I/O stream) to read from or write to. (Note that for putc, the extra FILE * argument comes last, while for fprintf and fscanf, it comes first.) Page 162: cat is about the most basic and important file-handling program there is (even if its name is a bit obscure). The cat program on page 162 is a bit like the hello, world program on page 6--it may seem trivial, but if you can get it to work, you re over the biggest first hurdle when it comes to handling files at all. Compare the cat program (and especially its filecopy function) to the file copying program on page 16 of section cat is essentially the same program, except that it accepts filenames on the command line. Since the authors advise calling fclose in part to flush the buffer in which putc is collecting output, you may wonder why the program at the top of the page does not call fclose on its output stream. The reason can be found in the next sentence: an implicit fclose happens automatically for any streams which remain open when the program exits normally.

6 In general, it s a good idea to close any streams you open, but not to close the preopened streams such as stdin and stdout. (Since the system opened them for you as your program was starting up, it s appropriate to let it close them for you as your program exits.) Section 7.6: Error Handling -- Stderr and Exit Page 163: stdout and stderr are both predefined output streams; for our purposes, the only difference between them is that stderr is not likely to be redirected by the user, so the error messages printed to stderr will always appear on the screen, where they can be seen. Page 164: The cryptic note about a pattern-matching program simply means that if you want to search the source code of a program for all the exit status values it can return, exit might be an easier string to search for than return. (Every call to exit represents an exit from the program, but not every return statement does.) The feof and ferror functions can be used to check for error conditions more carefully. In general, input routines (such as getchar and getline) return some special value to tell you that they couldn t read any more. Often, this value is EOF, reinforcing the notion that the only possible reason they couldn t read any more was because end-of-file had been reached. However, it s also possible that there was a read error, and you can call feof or ferror to determine whether this was the case. On the output side, though the output routines generally do return an error indication, few programs bother to check the return values from every call to functions such as putchar and printf. One way to check for output errors, without having to check the return value of every function, is to call ferror on the output stream (which might be stdout) at key points. Section 7.7: Line Input and Output Pages : To summarize, puts is like fputs except that the stream is assumed to be the standard output (stdout), and a newline ( \n ) is automatically appended. gets is like fgets except that the stream is assumed to be stdin, and the newline ( \n ) is deleted, and there s no way to specify the maximum line length. This last fact means that you almost never want to use gets at all: since you can t tell it how big the array it s to read into is, there s no way to guarantee that some unexpectedly-long input line won t overflow the array, with dire results. (When discussing the drawbacks of gets, it s customary to point out that the Internet worm, a program that wreaked havoc in 1988 by breaking into computers all over the net, was able to do so in part because a key network utility on many Unix systems used gets, and the worm was able to overflow the buffer in a particularly low, cunning way, with the dire result that the worm achieved superuser access to the attacked machine.) Section 7.8.1: String Operations Page 166: One thing to beware of is that strcpy s arguments--more precisely, the strings pointed to by its arguments--must not overlap. Another string function we ve seen is strstr: strstr(s,t) return pointer to first t in s, or NULL if not present

7 Section 7.8.2: Character Class Testing and Conversion One quirk of these functions, which the authors mention briefly, is that although they accept arguments of type int, it is not legal to pass just any int value to them. If you were to attempt to call isupper(12345), it might do something bizarre. You should only call these functions with arguments which represent valid character values. (Also, they are guaranteed to accept the value EOF gracefully.) Section 7.8.3: Ungetc There s not much more to say about ungetc, but two more stdio functions which might deserve mention are fread and fwrite. getc and putc (and getchar and putchar) allow you to read and write a character at a time, while fgets and fputs read and write a line at a time. The printf family of routines does formatted output, and the scanf family does formatted input. But what if you want to read or write a big block of unformatted characters, not necessarily one line long? You could use getc or putc in a loop, but another solution is to use the fread and fwrite functions, which are (briefly) described in appendix B1.5 on page 247. Section 7.8.4: Command Execution Page 167: The only thing to add to this brief description of system concerns the disposition of the executed command s output. (Similar arguments apply to its input.) The output generally goes wherever the calling program s output goes, though if the calling program has done anything with stdout (such as closing it, or redirecting it within the program with freopen), those changes will probably not affect the output of system. One way to achieve redirection of the command executed by system, if the operating system permits it, is to use redirection notation within the command line passed to system: system("date > outfile"); Note also that the exit status returned by the program (and hence perhaps by system) does not necessarily have anything to do with anything printed by the program. One way to capture the output printed by the program is to use redirection, as above, then open and read the output file. Section 7.8.5: Storage Management The important thing to know about malloc and free and friends is to be careful when calling them. It is easy to abuse them, either by using more space than you ask for (that is, writing beyond the ends of an allocated block) or by continuing to use a pointer after the memory it points to has been freed (perhaps because you had several pointers to the same block of memory, and you forgot that when you freed one pointer they all became invalid). malloc-related bugs can be difficult and frustrating to track down, so it s good to use programming practices which help to assure that the bugs don t happen in the first place. (One such practice is to make sure that pointer variables are set to NULL when they don t point anywhere, and to occasionally check pointer values--for instance at entry to an important pointerusing function--to make sure that they re not NULL.) As we mentioned on page 142 in section 6.5, it is no longer necessary (that is, in ANSI C) to cast malloc s value to the appropriate type, though it doesn t hurt to do so.

8 Section 7.8.6: Mathematical Functions Page 168: Note that the pow function is how you do exponentiation in C--C does not have a built-in exponentiation operator (such as ** or ^ in some other languages). Before calling these functions, remember to #include <math.h>. (It s always a good idea to #include the appropriate header(s) before using any library functions, but the math functions are particularly unlikely to work correctly if you forget.) Also, under Unix, you may have to explicitly request the math library by adding the -lm option at the end of the command line when compiling/linking. Section 7.8.7: Random Number Generation There is a typo in some printings; the code for returning a floating-point random number in the interval [0,1) should be #define frand() ((double) rand() / (RAND_MAX+1.0)) If you want to get random integers from M to N, you can use something like M + (int)(frand() * (N-M+1)) [Setting] the seed for rand refers to the fact that, by default, the sequence of pseudo-random numbers returned by rand is the same each time your program runs. To randomize it, you can call srand at the beginning of the program, handing it some truly random number, such as a value having to do with the time of day. (One way is with code like #include <stdlib.h> #include <time.h> srand((unsigned int)time((time_t *)NULL)); which uses the time function mentioned on page 256 in appendix B10.) One other caveat about rand: don t try to generate random 0/1 values (to simulate a coin flip, perhaps) with code like rand() % 2 This looks like it ought to work, but it turns out that on some systems rand isn t always perfectly random, and returns values which consistently alternate even, odd, even, odd, etc. (In fact, for similar reasons, you shouldn t usually use rand() % N for any value of N.) A good way to get random 0/1 values would be (int)(frand() * 2) based on the other frand() examples above.

Standard File Pointers

Standard File Pointers 1 Programming in C Standard File Pointers Assigned to console unless redirected Standard input = stdin Used by scan function Can be redirected: cmd < input-file Standard output = stdout Used by printf

More information

UNIT IV-2. The I/O library functions can be classified into two broad categories:

UNIT IV-2. The I/O library functions can be classified into two broad categories: UNIT IV-2 6.0 INTRODUCTION Reading, processing and writing of data are the three essential functions of a computer program. Most programs take some data as input and display the processed data, often known

More information

Standard C Library Functions

Standard C Library Functions Demo lecture slides Although I will not usually give slides for demo lectures, the first two demo lectures involve practice with things which you should really know from G51PRG Since I covered much of

More information

Input / Output Functions

Input / Output Functions CSE 2421: Systems I Low-Level Programming and Computer Organization Input / Output Functions Presentation G Read/Study: Reek Chapter 15 Gojko Babić 10-03-2018 Input and Output Functions The stdio.h contain

More information

Lecture 12 CSE July Today we ll cover the things that you still don t know that you need to know in order to do the assignment.

Lecture 12 CSE July Today we ll cover the things that you still don t know that you need to know in order to do the assignment. Lecture 12 CSE 110 20 July 1992 Today we ll cover the things that you still don t know that you need to know in order to do the assignment. 1 The NULL Pointer For each pointer type, there is one special

More information

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files CS113: Lecture 7 Topics: The C Preprocessor I/O, Streams, Files 1 Remember the name: Pre-processor Most commonly used features: #include, #define. Think of the preprocessor as processing the file so as

More information

Advanced C Programming Topics

Advanced C Programming Topics Introductory Medical Device Prototyping Advanced C Programming Topics, http://saliterman.umn.edu/ Department of Biomedical Engineering, University of Minnesota Operations on Bits 1. Recall there are 8

More information

File Access. FILE * fopen(const char *name, const char * mode);

File Access. FILE * fopen(const char *name, const char * mode); File Access, K&R 7.5 Dealing with named files is surprisingly similar to dealing with stdin and stdout. Start by declaring a "file pointer": FILE *fp; /* See Appendix B1.1, pg. 242 */ header

More information

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #47. File Handling

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #47. File Handling Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #47 File Handling In this video, we will look at a few basic things about file handling in C. This is a vast

More information

Computer Programming: Skills & Concepts (CP) Files in C

Computer Programming: Skills & Concepts (CP) Files in C CP 20 slide 1 Tuesday 21 November 2017 Computer Programming: Skills & Concepts (CP) Files in C Julian Bradfield Tuesday 21 November 2017 Today s lecture Character oriented I/O (revision) Files and streams

More information

THE C STANDARD LIBRARY & MAKING YOUR OWN LIBRARY. ISA 563: Fundamentals of Systems Programming

THE C STANDARD LIBRARY & MAKING YOUR OWN LIBRARY. ISA 563: Fundamentals of Systems Programming THE C STANDARD LIBRARY & MAKING YOUR OWN LIBRARY ISA 563: Fundamentals of Systems Programming Announcements Homework 2 posted Homework 1 due in two weeks Typo on HW1 (definition of Fib. Sequence incorrect)

More information

Quick review of previous lecture Ch6 Structure Ch7 I/O. EECS2031 Software Tools. C - Structures, Unions, Enums & Typedef (K&R Ch.

Quick review of previous lecture Ch6 Structure Ch7 I/O. EECS2031 Software Tools. C - Structures, Unions, Enums & Typedef (K&R Ch. 1 Quick review of previous lecture Ch6 Structure Ch7 I/O EECS2031 Software Tools C - Structures, Unions, Enums & Typedef (K&R Ch.6) Structures Basics: Declaration and assignment Structures and functions

More information

CSCI 171 Chapter Outlines

CSCI 171 Chapter Outlines Contents CSCI 171 Chapter 1 Overview... 2 CSCI 171 Chapter 2 Programming Components... 3 CSCI 171 Chapter 3 (Sections 1 4) Selection Structures... 5 CSCI 171 Chapter 3 (Sections 5 & 6) Iteration Structures

More information

Systems Programming. 08. Standard I/O Library. Alexander Holupirek

Systems Programming. 08. Standard I/O Library. Alexander Holupirek Systems Programming 08. Standard I/O Library Alexander Holupirek Database and Information Systems Group Department of Computer & Information Science University of Konstanz Summer Term 2008 Last lecture:

More information

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program CMPT 102 Introduction to Scientific Computer Programming Input and Output Janice Regan, CMPT 102, Sept. 2006 0 Your first program /* My first C program */ /* make the computer print the string Hello world

More information

Accessing Files in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Accessing Files in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts Accessing Files in C Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie, Absolute C++, by Walter

More information

Topic 8: I/O. Reading: Chapter 7 in Kernighan & Ritchie more details in Appendix B (optional) even more details in GNU C Library manual (optional)

Topic 8: I/O. Reading: Chapter 7 in Kernighan & Ritchie more details in Appendix B (optional) even more details in GNU C Library manual (optional) Topic 8: I/O Reading: Chapter 7 in Kernighan & Ritchie more details in Appendix B (optional) even more details in GNU C Library manual (optional) No C language primitives for I/O; all done via function

More information

Basic I/O. COSC Software Tools. Streams. Standard I/O. Standard I/O. Formatted Output

Basic I/O. COSC Software Tools. Streams. Standard I/O. Standard I/O. Formatted Output Basic I/O COSC2031 - Software Tools C - Input/Output (K+R Ch. 7) We know how to do some basic input and output: getchar - reading characters putchar - writing characters printf - formatted output Input

More information

Content. Input Output Devices File access Function of File I/O Redirection Command-line arguments

Content. Input Output Devices File access Function of File I/O Redirection Command-line arguments File I/O Content Input Output Devices File access Function of File I/O Redirection Command-line arguments UNIX and C language C is a general-purpose, high-level language that was originally developed by

More information

Chapter 10: File Input / Output

Chapter 10: File Input / Output C: Chapter10 Page 1 of 6 C Tutorial.......... File input/output Chapter 10: File Input / Output OUTPUT TO A FILE Load and display the file named formout.c for your first example of writing data to a file.

More information

CSci 4061 Introduction to Operating Systems. Input/Output: High-level

CSci 4061 Introduction to Operating Systems. Input/Output: High-level CSci 4061 Introduction to Operating Systems Input/Output: High-level I/O Topics First, cover high-level I/O Next, talk about low-level device I/O I/O not part of the C language! High-level I/O Hide device

More information

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code CS102: Standard I/O Our next topic is standard input and standard output in C. The adjective "standard" when applied to "input" or "output" could be interpreted to mean "default". Typically, standard output

More information

C Basics And Concepts Input And Output

C Basics And Concepts Input And Output C Basics And Concepts Input And Output Report Working group scientific computing Department of informatics Faculty of mathematics, informatics and natural sciences University of Hamburg Written by: Marcus

More information

IO = Input & Output 2

IO = Input & Output 2 Input & Output 1 IO = Input & Output 2 Idioms 3 Input and output in C are simple, in theory, because everything is handled by function calls, and you just have to look up the documentation of each function

More information

Course organization. Course introduction ( Week 1)

Course organization. Course introduction ( Week 1) Course organization Course introduction ( Week 1) Code editor: Emacs Part I: Introduction to C programming language (Week 2-9) Chapter 1: Overall Introduction (Week 1-3) Chapter 2: Types, operators and

More information

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 18 I/O in C Standard C Library I/O commands are not included as part of the C language. Instead, they are part of the Standard C Library. A collection of functions and macros that must be implemented

More information

C Input/Output. Before we discuss I/O in C, let's review how C++ I/O works. int i; double x;

C Input/Output. Before we discuss I/O in C, let's review how C++ I/O works. int i; double x; C Input/Output Before we discuss I/O in C, let's review how C++ I/O works. int i; double x; cin >> i; cin >> x; cout

More information

8. Characters, Strings and Files

8. Characters, Strings and Files REGZ9280: Global Education Short Course - Engineering 8. Characters, Strings and Files Reading: Moffat, Chapter 7, 11 REGZ9280 14s2 8. Characters and Arrays 1 ASCII The ASCII table gives a correspondence

More information

SWEN-250 Personal SE. Introduction to C

SWEN-250 Personal SE. Introduction to C SWEN-250 Personal SE Introduction to C A Bit of History Developed in the early to mid 70s Dennis Ritchie as a systems programming language. Adopted by Ken Thompson to write Unix on a the PDP-11. At the

More information

LANGUAGE OF THE C. C: Part 6. Listing 1 1 #include <stdio.h> 2 3 int main(int argc, char *argv[]) PROGRAMMING

LANGUAGE OF THE C. C: Part 6. Listing 1 1 #include <stdio.h> 2 3 int main(int argc, char *argv[]) PROGRAMMING C: Part 6 LANGUAGE OF THE C In part 6 of Steve Goodwins C tutorial we continue our look at file handling and keyboard input File handling Most software will at some time need to read from (or perhaps write

More information

Computer Programming: Skills & Concepts (CP1) Files in C. 18th November, 2010

Computer Programming: Skills & Concepts (CP1) Files in C. 18th November, 2010 Computer Programming: Skills & Concepts (CP1) Files in C 18th November, 2010 CP1 26 slide 1 18th November, 2010 Today s lecture Character oriented I/O (revision) Files and streams Opening and closing files

More information

C programming basics T3-1 -

C programming basics T3-1 - C programming basics T3-1 - Outline 1. Introduction 2. Basic concepts 3. Functions 4. Data types 5. Control structures 6. Arrays and pointers 7. File management T3-2 - 3.1: Introduction T3-3 - Review of

More information

C Libraries. Bart Childs Complementary to the text(s)

C Libraries. Bart Childs Complementary to the text(s) C Libraries Bart Childs Complementary to the text(s) 2006 C was designed to make extensive use of a number of libraries. A great reference for student purposes is appendix B of the K&R book. This list

More information

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved. C How to Program, 6/e 1992-2010 by Pearson Education, Inc. An important part of the solution to any problem is the presentation of the results. In this chapter, we discuss in depth the formatting features

More information

Lecture 8: Structs & File I/O

Lecture 8: Structs & File I/O ....... \ \ \ / / / / \ \ \ \ / \ / \ \ \ V /,----' / ^ \ \.--..--. / ^ \ `--- ----` / ^ \. ` > < / /_\ \. ` / /_\ \ / /_\ \ `--' \ /. \ `----. / \ \ '--' '--' / \ / \ \ / \ / / \ \ (_ ) \ (_ ) / / \ \

More information

Slide Set 8. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Slide Set 8. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng Slide Set 8 for ENCM 339 Fall 2017 Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary October 2017 ENCM 339 Fall 2017 Section 01 Slide

More information

Contents. A Review of C language. Visual C Visual C++ 6.0

Contents. A Review of C language. Visual C Visual C++ 6.0 A Review of C language C++ Object Oriented Programming Pei-yih Ting NTOU CS Modified from www.cse.cuhk.edu.hk/~csc2520/tuto/csc2520_tuto01.ppt 1 2 3 4 5 6 7 8 9 10 Double click 11 12 Compile a single source

More information

CS240: Programming in C

CS240: Programming in C CS240: Programming in C Lecture 13 si 14: Unix interface for working with files. Cristina Nita-Rotaru Lecture 13/Fall 2013 1 Working with Files (I/O) File system: specifies how the information is organized

More information

printf( Please enter another number: ); scanf( %d, &num2);

printf( Please enter another number: ); scanf( %d, &num2); CIT 593 Intro to Computer Systems Lecture #13 (11/1/12) Now that we've looked at how an assembly language program runs on a computer, we're ready to move up a level and start working with more powerful

More information

C mini reference. 5 Binary numbers 12

C mini reference. 5 Binary numbers 12 C mini reference Contents 1 Input/Output: stdio.h 2 1.1 int printf ( const char * format,... );......................... 2 1.2 int scanf ( const char * format,... );.......................... 2 1.3 char

More information

Continued from previous lecture

Continued from previous lecture The Design of C: A Rational Reconstruction: Part 2 Jennifer Rexford Continued from previous lecture 2 Agenda Data Types Statements What kinds of operators should C have? Should handle typical operations

More information

Files. Programs and data are stored on disk in structures called files Examples. a.out binary file lab1.c - text file term-paper.

Files. Programs and data are stored on disk in structures called files Examples. a.out binary file lab1.c - text file term-paper. File IO part 2 Files Programs and data are stored on disk in structures called files Examples a.out binary file lab1.c - text file term-paper.doc - binary file Overview File Pointer (FILE *) Standard:

More information

CSCI-243 Exam 2 Review February 22, 2015 Presented by the RIT Computer Science Community

CSCI-243 Exam 2 Review February 22, 2015 Presented by the RIT Computer Science Community CSCI-43 Exam Review February, 01 Presented by the RIT Computer Science Community http://csc.cs.rit.edu C Preprocessor 1. Consider the following program: 1 # include 3 # ifdef WINDOWS 4 # include

More information

CS 220: Introduction to Parallel Computing. Input/Output. Lecture 7

CS 220: Introduction to Parallel Computing. Input/Output. Lecture 7 CS 220: Introduction to Parallel Computing Input/Output Lecture 7 Input/Output Most useful programs will provide some type of input or output Thus far, we ve prompted the user to enter their input directly

More information

DATA STRUCTURES USING C

DATA STRUCTURES USING C DATA STRUCTURES USING C File Handling in C Goals By the end of this unit you should understand how to open a file to write to it. how to open a file to read from it. how to open a file to append data to

More information

The Design of C: A Rational Reconstruction: Part 2

The Design of C: A Rational Reconstruction: Part 2 The Design of C: A Rational Reconstruction: Part 2 1 Continued from previous lecture 2 Agenda Data Types Operators Statements I/O Facilities 3 Operators Issue: What kinds of operators should C have? Thought

More information

Day14 A. Young W. Lim Tue. Young W. Lim Day14 A Tue 1 / 15

Day14 A. Young W. Lim Tue. Young W. Lim Day14 A Tue 1 / 15 Day14 A Young W. Lim 2017-12-26 Tue Young W. Lim Day14 A 2017-12-26 Tue 1 / 15 Outline 1 Based on 2 C Strings (1) Characters and Strings Unformatted IO Young W. Lim Day14 A 2017-12-26 Tue 2 / 15 Based

More information

C for C++ Programmers

C for C++ Programmers C for C++ Programmers CS230/330 - Operating Systems (Winter 2001). The good news is that C syntax is almost identical to that of C++. However, there are many things you're used to that aren't available

More information

Printable View of: Week 13: Miscelaneous cool features. Returns from standard functions. returns from standard functions: scanf(), fopen()

Printable View of: Week 13: Miscelaneous cool features. Returns from standard functions. returns from standard functions: scanf(), fopen() 1 of 6 9/11/2009 12:57 PM Printable View of: Week 13: Miscelaneous cool features Print Save to File File: returns from standard functions: scanf(), fopen() returns from standard functions: scanf(), fopen()

More information

PROGRAMMAZIONE I A.A. 2017/2018

PROGRAMMAZIONE I A.A. 2017/2018 PROGRAMMAZIONE I A.A. 2017/2018 INPUT/OUTPUT INPUT AND OUTPUT Programs must be able to write data to files or to physical output devices such as displays or printers, and to read in data from files or

More information

Computer Security. Robust and secure programming in C. Marius Minea. 12 October 2017

Computer Security. Robust and secure programming in C. Marius Minea. 12 October 2017 Computer Security Robust and secure programming in C Marius Minea marius@cs.upt.ro 12 October 2017 In this lecture Write correct code minimizing risks with proper error handling avoiding security pitfalls

More information

Announcements. Strings and Pointers. Strings. Initializing Strings. Character I/O. Lab 4. Quiz. July 18, Special character arrays

Announcements. Strings and Pointers. Strings. Initializing Strings. Character I/O. Lab 4. Quiz. July 18, Special character arrays Strings and Pointers Announcements Lab 4 Why are you taking this course? Lab 5 #, 8: Reading in data from file using fscanf Quiz Quiz Strings Special character arrays End in null character, \ char hi[6];

More information

Mode Meaning r Opens the file for reading. If the file doesn't exist, fopen() returns NULL.

Mode Meaning r Opens the file for reading. If the file doesn't exist, fopen() returns NULL. Files Files enable permanent storage of information C performs all input and output, including disk files, by means of streams Stream oriented data files are divided into two categories Formatted data

More information

Advanced C Programming and Introduction to Data Structures

Advanced C Programming and Introduction to Data Structures FYBCA Semester II (Advanced C Programming and Introduction to Data Structures) Question Bank Multiple Choice Questions Unit-1 1. Which operator is used with a pointer to access the value of the variable

More information

ENG120. Misc. Topics

ENG120. Misc. Topics ENG120 Misc. Topics Topics Files in C Using Command-Line Arguments Typecasting Working with Multiple source files Conditional Operator 2 Files and Streams C views each file as a sequence of bytes File

More information

Fundamentals of Programming. Lecture 11: C Characters and Strings

Fundamentals of Programming. Lecture 11: C Characters and Strings 1 Fundamentals of Programming Lecture 11: C Characters and Strings Instructor: Fatemeh Zamani f_zamani@ce.sharif.edu Sharif University of Technology Computer Engineering Department The lectures of this

More information

Lecture 7: Files. opening/closing files reading/writing strings reading/writing numbers (conversion to ASCII) command line arguments

Lecture 7: Files. opening/closing files reading/writing strings reading/writing numbers (conversion to ASCII) command line arguments Lecture 7: Files opening/closing files reading/writing strings reading/writing numbers (conversion to ASCII) command line arguments Lecture 5: Files, I/O 0IGXYVI*MPIW 0 opening/closing files reading/writing

More information

Day14 A. Young W. Lim Thr. Young W. Lim Day14 A Thr 1 / 14

Day14 A. Young W. Lim Thr. Young W. Lim Day14 A Thr 1 / 14 Day14 A Young W. Lim 2017-11-02 Thr Young W. Lim Day14 A 2017-11-02 Thr 1 / 14 Outline 1 Based on 2 C Strings (1) Characters and Strings Unformatted IO Young W. Lim Day14 A 2017-11-02 Thr 2 / 14 Based

More information

Input/Output and the Operating Systems

Input/Output and the Operating Systems Input/Output and the Operating Systems Fall 2015 Jinkyu Jeong (jinkyu@skku.edu) 1 I/O Functions Formatted I/O printf( ) and scanf( ) fprintf( ) and fscanf( ) sprintf( ) and sscanf( ) int printf(const char*

More information

This lists all known errors in The C Programming Language, Second Edition, by Brian Kernighan and Dennis Ritchie (Prentice-Hall, 1988).

This lists all known errors in The C Programming Language, Second Edition, by Brian Kernighan and Dennis Ritchie (Prentice-Hall, 1988). Errata for The C Programming Language, Second Edition This lists all known errors in The C Programming Language, Second Edition, by Brian Kernighan and Dennis Ritchie (Prentice-Hall, 1988). The pagination

More information

CSI 402 Lecture 2 Working with Files (Text and Binary)

CSI 402 Lecture 2 Working with Files (Text and Binary) CSI 402 Lecture 2 Working with Files (Text and Binary) 1 / 30 AQuickReviewofStandardI/O Recall that #include allows use of printf and scanf functions Example: int i; scanf("%d", &i); printf("value

More information

7/21/ FILE INPUT / OUTPUT. Dong-Chul Kim BioMeCIS UTA

7/21/ FILE INPUT / OUTPUT. Dong-Chul Kim BioMeCIS UTA 7/21/2014 1 FILE INPUT / OUTPUT Dong-Chul Kim BioMeCIS CSE @ UTA What s a file? A named section of storage, usually on a disk In C, a file is a continuous sequence of bytes Examples for the demand of a

More information

UNIT-V CONSOLE I/O. This section examines in detail the console I/O functions.

UNIT-V CONSOLE I/O. This section examines in detail the console I/O functions. UNIT-V Unit-5 File Streams Formatted I/O Preprocessor Directives Printf Scanf A file represents a sequence of bytes on the disk where a group of related data is stored. File is created for permanent storage

More information

Slide Set 3. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Slide Set 3. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary Slide Set 3 for ENCM 339 Fall 2016 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary September 2016 ENCM 339 Fall 2016 Slide Set 3 slide 2/46

More information

UNIX input and output

UNIX input and output UNIX input and output Disk files In UNIX a disk file is a finite sequence of bytes, usually stored on some nonvolatile medium. Disk files have names, which are called paths. We won t discuss file naming

More information

Intermediate Programming / C and C++ CSCI 6610

Intermediate Programming / C and C++ CSCI 6610 Intermediate Programming / C... 1/16 Intermediate Programming / C and C++ CSCI 6610 Streams and Files February 22, 2016 Intermediate Programming / C... 2/16 Lecture 4: Using Streams and Files in C and

More information

Slide Set 3. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Slide Set 3. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng Slide Set 3 for ENCM 339 Fall 2017 Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary September 2017 ENCM 339 Fall 2017 Section 01

More information

CSE 333 Midterm Exam July 24, Name UW ID#

CSE 333 Midterm Exam July 24, Name UW ID# Name UW ID# There are 6 questions worth a total of 100 points. Please budget your time so you get to all of the questions. Keep your answers brief and to the point. The exam is closed book, closed notes,

More information

Organization of a file

Organization of a file File Handling 1 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example: Consider keeping students records 100 students

More information

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University ITC213: STRUCTURED PROGRAMMING Bhaskar Shrestha National College of Computer Studies Tribhuvan University Lecture 07: Data Input and Output Readings: Chapter 4 Input /Output Operations A program needs

More information

بسم اهلل الرمحن الرحيم

بسم اهلل الرمحن الرحيم بسم اهلل الرمحن الرحيم Fundamentals of Programming C Session # 10 By: Saeed Haratian Fall 2015 Outlines Examples Using the for Statement switch Multiple-Selection Statement do while Repetition Statement

More information

The Design of C: A Rational Reconstruction (cont.)

The Design of C: A Rational Reconstruction (cont.) The Design of C: A Rational Reconstruction (cont.) 1 Goals of this Lecture Recall from last lecture Help you learn about: The decisions that were available to the designers of C The decisions that were

More information

Programming. Functions and File I/O

Programming. Functions and File I/O Programming Functions and File I/O Summary Functions Definition Declaration Invocation Pass by value vs. pass by reference File I/O Reading Writing Static keyword Global vs. local variables 2 Functions

More information

File (1A) Young Won Lim 11/25/16

File (1A) Young Won Lim 11/25/16 File (1A) Copyright (c) 2010-2016 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version

More information

C Programming. Unit 9. Manipulating Strings File Processing.

C Programming. Unit 9. Manipulating Strings File Processing. Introduction to C Programming Unit 9 Manipulating Strings File Processing skong@itt-tech.edu Unit 8 Review Unit 9: Review of Past Material Unit 8 Review Arrays Collection of adjacent memory cells Each

More information

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection 1 Files 1.1 File functions Opening Files : The function fopen opens a file and returns a FILE pointer. FILE *fopen( const char * filename, const char * mode ); The allowed modes for fopen are as follows

More information

Lecture 7: file I/O, more Unix

Lecture 7: file I/O, more Unix CIS 330: / / / / (_) / / / / _/_/ / / / / / \/ / /_/ / `/ \/ / / / _/_// / / / / /_ / /_/ / / / / /> < / /_/ / / / / /_/ / / / /_/ / / / / / \ /_/ /_/_/_/ _ \,_/_/ /_/\,_/ \ /_/ \ //_/ /_/ Lecture 7: file

More information

Writing to and reading from files

Writing to and reading from files Writing to and reading from files printf() and scanf() are actually short-hand versions of more comprehensive functions, fprintf() and fscanf(). The difference is that fprintf() includes a file pointer

More information

Chapter 12. Files (reference: Deitel s chap 11) chap8

Chapter 12. Files (reference: Deitel s chap 11) chap8 Chapter 12 Files (reference: Deitel s chap 11) 20061025 chap8 Introduction of File Data files Can be created, updated, and processed by C programs Are used for permanent storage of large amounts of data

More information

Pointers cause EVERYBODY problems at some time or another. char x[10] or char y[8][10] or char z[9][9][9] etc.

Pointers cause EVERYBODY problems at some time or another. char x[10] or char y[8][10] or char z[9][9][9] etc. Compound Statements So far, we ve mentioned statements or expressions, often we want to perform several in an selection or repetition. In those cases we group statements with braces: i.e. statement; statement;

More information

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6 1. What is File management? In real life, we want to store data permanently so that later on we can retrieve it and reuse it. A file is a collection of bytes stored on a secondary storage device like hard

More information

Should you know scanf and printf?

Should you know scanf and printf? C-LANGUAGE INPUT & OUTPUT C-Language Output with printf Input with scanf and gets_s and Defensive Programming Copyright 2016 Dan McElroy Should you know scanf and printf? scanf is only useful in the C-language,

More information

Pointers and File Handling

Pointers and File Handling 1 Pointers and File Handling From variables to their addresses Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR 2 Basics of Pointers INDIAN INSTITUTE OF TECHNOLOGY

More information

2009 S2 COMP File Operations

2009 S2 COMP File Operations 2009 S2 COMP1921 9. File Operations Oliver Diessel odiessel@cse.unsw.edu.au Last updated: 16:00 22 Sep 2009 9. File Operations Topics to be covered: Streams Text file operations Binary file operations

More information

C for Engineers and Scientists: An Interpretive Approach. Chapter 14: File Processing

C for Engineers and Scientists: An Interpretive Approach. Chapter 14: File Processing Chapter 14: File Processing Files and Streams C views each file simply as a sequential stream of bytes. It ends as if there is an end-of-file marker. The data structure FILE, defined in stdio.h, stores

More information

Library Functions. General Questions

Library Functions. General Questions 1 Library Functions General Questions 1. What will the function rewind() do? A. Reposition the file pointer to a character reverse. B. Reposition the file pointer stream to end of file. C. Reposition the

More information

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language 1 History C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC

More information

CSCI S-Q Lecture #12 7/29/98 Data Structures and I/O

CSCI S-Q Lecture #12 7/29/98 Data Structures and I/O CSCI S-Q Lecture #12 7/29/98 Data Structures and I/O Introduction The WRITE and READ ADT Operations Case Studies: Arrays Strings Binary Trees Binary Search Trees Unordered Search Trees Page 1 Introduction

More information

Goals of C "" The Goals of C (cont.) "" Goals of this Lecture"" The Design of C: A Rational Reconstruction"

Goals of C  The Goals of C (cont.)  Goals of this Lecture The Design of C: A Rational Reconstruction Goals of this Lecture The Design of C: A Rational Reconstruction Help you learn about: The decisions that were available to the designers of C The decisions that were made by the designers of C Why? Learning

More information

File Handling. 21 July 2009 Programming and Data Structure 1

File Handling. 21 July 2009 Programming and Data Structure 1 File Handling 21 July 2009 Programming and Data Structure 1 File handling in C In C we use FILE * to represent a pointer to a file. fopen is used to open a file. It returns the special value NULL to indicate

More information

CS 261 Fall Mike Lam, Professor. Structs and I/O

CS 261 Fall Mike Lam, Professor. Structs and I/O CS 261 Fall 2018 Mike Lam, Professor Structs and I/O Typedefs A typedef is a way to create a new type name Basically a synonym for another type Useful for shortening long types or providing more meaningful

More information

File I/O. Last updated 10/30/18

File I/O. Last updated 10/30/18 Last updated 10/30/18 Input/Output Streams Information flow between entities is done with streams Keyboard Text input stream data stdin Data Text output stream Monitor stdout stderr printf formats data

More information

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved. C How to Program, 6/e Storage of data in variables and arrays is temporary such data is lost when a program terminates. Files are used for permanent retention of data. Computers store files on secondary

More information

Binghamton University. CS-220 Spring Includes & Streams

Binghamton University. CS-220 Spring Includes & Streams Includes & Streams 1 C Pre-Processor C Program Pre-Processor Pre-processed C Program Header Files Header Files 2 #include Two flavors: #include Replace this line with the contents of file abc.h

More information

Stream States. Formatted I/O

Stream States. Formatted I/O C++ Input and Output * the standard C++ library has a collection of classes that can be used for input and output * most of these classes are based on a stream abstraction, the input or output device is

More information

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017 C Concepts - I/O Lecture 19 COP 3014 Fall 2017 November 29, 2017 C vs. C++: Some important differences C has been around since around 1970 (or before) C++ was based on the C language While C is not actually

More information

File IO and command line input CSE 2451

File IO and command line input CSE 2451 File IO and command line input CSE 2451 File functions Open/Close files fopen() open a stream for a file fclose() closes a stream One character at a time: fgetc() similar to getchar() fputc() similar to

More information

CpSc 111 Lab 5 Conditional Statements, Loops, the Math Library, and Redirecting Input

CpSc 111 Lab 5 Conditional Statements, Loops, the Math Library, and Redirecting Input CpSc Lab 5 Conditional Statements, Loops, the Math Library, and Redirecting Input Overview For this lab, you will use: one or more of the conditional statements explained below scanf() or fscanf() to read

More information

Stream Model of I/O. Basic I/O in C

Stream Model of I/O. Basic I/O in C Stream Model of I/O 1 A stream provides a connection between the process that initializes it and an object, such as a file, which may be viewed as a sequence of data. In the simplest view, a stream object

More information

Unit 6 Files. putchar(ch); ch = getc (fp); //Reads single character from file and advances position to next character

Unit 6 Files. putchar(ch); ch = getc (fp); //Reads single character from file and advances position to next character 1. What is File management? In real life, we want to store data permanently so that later on we can retrieve it and reuse it. A file is a collection of bytes stored on a secondary storage device like hard

More information