Line at a time I/O with fgets() and fputs()

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
Character String Manipulation. Overview Character string functions sscanf() function snprintf() function.
Lecture 20 Arrays and Strings
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Strings CS240 Dick Steflik. What is a string A null terminated array of characters: char thisIsAString[10]; \0 The “\0” (null character)
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.
As previously noted, a byte can contain a numeric value in the range Computers don't understand Latin, Cyrillic, Hindi, Arabic character sets! Alphanumeric.
CS1061 C Programming Lecture 17: Steams and Character I/O A. O’Riordan, 2004.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
C-strings Array with base type char One character per indexed variable
An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
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.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
CGS 3460 More On File IO. CGS 3460 What is a File n A file is a package of information with a name attached to it. n Files are used for various purposes:
File IO and command line input CSE 2451 Rong Shi.
Chapter 8 File-Oriented Input and Output. 8.1 INTRODUCTION a file can also be designed to store data. We can easily update files, A data file as input.
File I/O, Project 1: List ADT Bryce Boe 2013/07/02 CS24, Summer 2013 C.
Introduction As programmers, we don’t want to have to implement functions for every possible task we encounter. The Standard C library contains functions.
Memory Layout, File I/O Bryce Boe 2013/06/27 CS24, Summer 2013 C.
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.
CS 261 – Recitation 7 Spring 2015 Oregon State University School of Electrical Engineering and Computer Science.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
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.
ECE 103 Engineering Programming Chapter 29 C Strings, Part 2 Herbert G. Mayer, PSU CS Status 7/30/2014 Initial content copied verbatim from ECE 103 material.
6/9/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 10 (C-4)
Real Numbers Device driver process within the operating system that interacts with I/O controller logical record 1 logical record 2 logical record 3.
Silberschatz and Galvin  C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education.
Chapter 4.
Strings CSCI 112: Programming in C.
Chapter 9 – File Input Output
ECE Application Programming
Functions, Part 2 of 2 Topics Functions That Return a Value
An Introduction to C Programming
File I/O.
CS 261 – Recitation 7 Fall 2013 Oregon State University
CSC215 Lecture Input and Output.
Plan for the Day: I/O (beyond scanf and printf)
CSE1320 Files in C Dr. Sajib Datta
File Input/Output.
CSE1320 Files in C Dr. Sajib Datta
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Input and Output Lecture 4.
Files I/O, Streams, I/O Redirection, Reading with fscanf
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
CSE1320 Files in C Dr. Sajib Datta
רשימות מקושרות עבודה עם קבצים דוגמה
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
CSC215 Lecture Input and Output.
Functions, Part 2 of 3 Topics Functions That Return a Value
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
C What you Know* Objective: To introduce some of the features of C. This assumes that you are familiar with C++ or java and concentrates on the features.
File Input and Output.
C-strings In general, a string is a series of characters treated as a unit. Practically all string implementations treat a string as a variable-length.
EECE.2160 ECE Application Programming
Module 12 Input and Output
File I/O & UNIX System Interface
EECE.2160 ECE Application Programming
Block I/O fread and fwrite functions are the most efficient way to read or write large amounts of data. fread() – reads a specified number of bytes from.
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
Intro to the Shell with Fork, Exec, Wait
EECE.2160 ECE Application Programming
I/O CS580U - Fall 2018.
Professor Jodi Neely-Ritz University of Florida
Chapter 18 I/O in C.
Presentation transcript:

Line at a time I/O with fgets() and fputs() The fgets(buffer_address, buf_size, file) function can be used to read from a stream until either: a newline character '\n' = 10 = 0x0a is encountered, or the specified number of characters - 1 has been read, or end of file is reached. fgets() will append the NULL character to whatever it reads in.

Line at a time input and output Since fgets() will read in multiple characters it is not possible to assign what it reads to a single variable of type char. Thus a pointer to a buffer must be passed (as was the case with scanf()). The fputs() function writes a NULL terminated string to a stream (after stripping off the NULL). The following example is yet another cat program, but this one works one line at a time.

Line at a time input and output #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { unsigned char *buff = NULL; int line = 0; buff = malloc(1024); // alloc storage to hold data if (buff == 0) exit(1); while (fgets(buff, 1024, stdin) != 0) fputs(buff, stdout); fprintf(stderr, "%d %d \n", line, strlen(buff)); line += 1; } free(buff); // release the storage malloc allocated

Line at a time input and output Here buff is declared a pointer and the storage which will hold the data is allocated with malloc() Alternatively we could have declared unsigned char buff[1024] and not used malloc()

Line at a time input and output Here is the output that went to stderr. Note that each line that appeared empty has length 1 (the newline character itself) and the lines that appear to have length 1 actually have length 2. 0 12 1 1 2 19 3 20 4 1 5 7 6 2 7 24 8 18 9 1 10 24 11 18 12 15 13 1 14 41 15 5 16 27 17 55 18 17 19 4 20 2