C PROGRAMMING. Characters and Strings File Processing Exercise

Similar documents
Fundamentals of Programming. Lecture 11: C Characters and Strings

Chapter 8 - Characters and Strings

Chapter 8 C Characters and Strings

C mini reference. 5 Binary numbers 12

ENG120. Misc. Topics

C: How to Program. Week /May/28

Characters and Strings

Standard C Library Functions

Input/Output and the Operating Systems

Chapter 8: Character & String. In this chapter, you ll learn about;

C programming basics T3-1 -

CS240: Programming in C

PROGRAMMAZIONE I A.A. 2017/2018

SWEN-250 Personal SE. Introduction to C

C Programming. Unit 9. Manipulating Strings File Processing.

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

C File Processing: One-Page Summary

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

System Software Experiment 1 Lecture 7

Files and Streams Opening and Closing a File Reading/Writing Text Reading/Writing Raw Data Random Access Files. C File Processing CS 2060

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

File IO and command line input CSE 2451

Lecture 9: File Processing. Quazi Rahman

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

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

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

today cs3157-fall2002-sklar-lect05 1

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

Arrays, Strings, & Pointers

UNIX System Programming

25.2 Opening and Closing a File

EECS2031. Modifiers. Data Types. Lecture 2 Data types. signed (unsigned) int long int long long int int may be omitted sizeof()

Appendix A Developing a C Program on the UNIX system

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

Simple Output and Input. see chapter 7

Computer Language. It is a systematical code for communication between System and user. This is in two categories.

Text Output and Input; Redirection

A function is a named group of statements developed to solve a sub-problem and returns a value to other functions when it is called.

2009 S2 COMP File Operations

Computer Programming Unit v

Ch 11. C File Processing (review)

CMPE-013/L. File I/O. File Processing. Gabriel Hugh Elkaim Winter File Processing. Files and Streams. Outline.

211: Computer Architecture Summer 2016

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

Input / Output Functions

File Processing. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

File I/O. Preprocessor Macros

C-Refresher: Session 10 Disk IO

Intermediate Programming, Spring 2017*

8. Characters, Strings and Files

Introduction to string

Standard File Pointers

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

Lecture 8. Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

Course organization. Course introduction ( Week 1)

Lecture6 File Processing

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

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

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

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

CSE2301. Introduction. Streams and Files. File Access Random Numbers Testing and Debugging. In this part, we introduce

Fundamentals of Programming. Lecture 10 Hamed Rasifard

Programming in C. Session 8. Seema Sirpal Delhi University Computer Centre

Chapter 11 File Processing

CSCE150A. Introduction. Basics. String Library. Substrings. Line Scanning. Sorting. Command Line Arguments. Misc CSCE150A. Introduction.

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 9. Strings. Notes. Notes. Notes. Lecture 07 - Strings

Computer Science & Engineering 150A Problem Solving Using Computers

Operating System Labs. Yuanbin Wu

Introduction to Computer Programming Lecture 18 Binary Files

Computer programming

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

File Handling. Reference:

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

Intermediate Programming, Spring 2017*

Contents. Preface. Introduction. Introduction to C Programming

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

10/6/2015. C Input/Output. stdin, stdout, stderr. C Language II. CMSC 313 Sections 01, 02. C opens three input/output devices automatically:

Introduction to Computer and Program Design. Lesson 6. File I/O. James C.C. Cheng Department of Computer Science National Chiao Tung University

upper and lower case English letters: A-Z and a-z digits: 0-9 common punctuation symbols special non-printing characters: e.g newline and space.

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

C Characters and Strings

C Programming 1. File Access. Goutam Biswas. Lect 29

Standard I/O in C, Computer System and programming in C

C Basics And Concepts Input And Output

Lecture 05 Pointers ctd..

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

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

Input/Output: Advanced Concepts

PRINCIPLES OF OPERATING SYSTEMS

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

Model Viva Questions for Programming in C lab

Characters, c-strings, and the string Class. CS 1: Problem Solving & Program Design Using C++

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

Lecture 10 Arrays (2) and Strings. UniMAP SEM II - 11/12 DKT121 1

Main Program Revisited. Reading Assignment. K.N. King Chapter 3, 22. K.N. King Sections Example of Argument Processing

File I/O. Arash Rafiey. November 7, 2017

MARKS: Q1 /20 /15 /15 /15 / 5 /30 TOTAL: /100

CSI 402 Lecture 2 (More on Files) 2 1 / 20

DS: CS Computer Sc & Engg: IIT Kharagpur 1. File Access. Goutam Biswas. ect 29

Programming & Data Structure

Transcription:

C PROGRAMMING Characters and Strings File Processing Exercise

CHARACTERS AND STRINGS A single character defined using the char variable type Character constant is an int value enclosed by single quotes E.g. a represents the integer value of the character a A string is a series of characters String, string literals and string constants enclosed by double quotes

DEFINING CHARACTERS AND STRINGS Declaring and assigning a single character char c= a ; Strings are arrays of characters A pointer to the first character in the array The last element of the string character array is the null termination charcter \0 \0 Denotes theend of a string

DEFINING STRINGS char node[]= iceberg ; char *nodeptr= iceberg ; char nodename[180]; For the first two definitions the null termination is added by the compiler

CHARACTER INPUT AND OUTPUT include <stdio.h> int getchar(void) Input the next character from standard input, return it as an integer. int putchar(int c) Display character stored in c Also use printf and scanf with the %c format specifier

STRING INPUT AND OUTPUT char *gets(char *s) Input characters from standard inout in to the array s until newline or EOF character is reached. A NULL termination character is placed at the end of the string. int puts(char *s) Display array of characters in s follow with a newline. Also use printf and scanf with the %s format specifier

CODE EXAMPLE USING PUTS AND GETCHAR char c, nodename1[80], nodename2[80]; int i=0; puts("enter a line of text"); while((c=getchar())!='\n') nodename1[i++]=c; nodename1[i]='\0';

FORMATTED STRING INPUT AND OUTPUT sprintf(char *s, const char *format,..) Equivalent to printf with the exception that its output is stored in the array s specified in the sprintf function. The prototype for sscanf is ; sscanf(char *s, const char *format, ). Equivalent to scanf reads input from the string s specified in the sscanf function.

SPRINTF AND SSCANF EXAMPLES char node[20], s2[80]; char s1[] ="Titania 3.78 7"; float fload, floadout; int nusers, nusersout; /*Using sscanf to read data from a string*/ sscanf(s1, "%s%f%d", node, &floadout, &nusersout); sprintf(s2, "%s %f %d", node, fload, nusers);

FUNCTIONS FOR CHARACTER MANIPULATION library ctype.h isdigit, isalpha, islower, isupper, toupper, tolower and isspace. These functions can be used to perform conversions on a single character or for testing that a character is of a certain type.

STRING CONVERSION FUNCTIONS String conversion functions from the general utilities library stdlib convert strings to float, int long int, double, long, and unsigned long data types respectively. atof, atoi, atol, strtod, strtol, strtoul

STRING MANIPULATION The string handling library string.h provides a range of string manipulation functions for copying, concatenating, comparison, tokenizing and for identifying the occurrence and positions of particular characters in a string. E.g. strcpy, strlen, strcmp and strtok. See the examples

FILE PROCESSING file as a sequential stream of bytes with each file terminated by an end-of file marker When a file is opened a stream is associated with the file Streams open during program execution stdin stdout stderr

SEQUENTIAL FILE MANAGEMENT Streams channels of communication between files and programs. Range of functions for streaming data to files fprintf fscanf fgetc fputc

OPENING A FILE When opening a file it is necessary to declare a variable that will be used to reference that file, the standard library provides the FILE structure. So a pointer to a FILE is declared using: FILE *myfile; File opened using the function fopen returns a pointer to the opened file

FOPEN USAGE if((myfile=fopen("myfilename", "w"))==null) printf("the file could not be opened!\n"); else { file was opened and is read or written here }

FILE OPEN MODES Mode Description r Open for reading w Open for writing a Append, open or create a file for writing at the end of the file r+ Open a file for update (reading and writing) w+ Create a file for update. If the file already exists discard the contents a+ Append, open or create a file for update, writing is done at the end of the file

WRITING DATA USING FPRINTF fprintf(fileptr, format specifiers, data list); fprintf(mfptr, "%6d %20s %6d\n", irunid, sname, inode); Closing the file fclose(mfptr);

READING DATA USING FSCANF fscanf(fileptr, format specifiers, data list); while(!feof(mfptr)) { printf("%6d %20s %6d\n", sim.id, sim.name, sim.node); fscanf(mfptr, "%d%s%d", &sim.id, sim.name, &sim.node); }

PRACTICAL CODING EXAMPLE Method for solving 1 st order ODEs with well defined BC s Shooting Method Compile and run the code startshooting.c

EXERCISE Adapt the program startshooting.c to read the input parameters from an input file. Adapt the program so that it reads the guess q froom the command line To read parameters from the command line we use the parameters argc and argv passed into the main function Use the following line to convert the command line parameter Hint look at vecdp.c in the functions folder If(argc>1) q=atof(argv[1]);

RANDOM ACCESS FILES Transaction processing systems Individual records of same length accessed at random fwrite Write fixed number of bytes to a file fread Read fixed number of bytes from a file

DATA DECLARATION Example data structure struct mydata { int index; float data;} Typical declaration struct mydata blankdata={0, 3.141};

FWRITE EXAMPLE CALL fwrite(&blankdata, sizeof(struct mydata), 1, fileptr) Write data structure myblankdata Specify correct field size Specify number of data items to write (in this case 1) Provide a valid pointer to the file that is opened for writing

FREAD EXAMPLE CALL fread(&blankdata, sizeof(struct mydata), 1, fileptr) Read data structure myblankdata Specify correct field size Specify number of data items to read (in this case 1) Provide a valid pointer to the file that is opened for reading

FSEEK Fseek sets file pointer to specific position in file int fseek(file *stream, long int offset, int whence) Offset is number of bytes from location whence Whence has one of three values SEEK_SET (beginning of file) SEEK_CUR (current location) SEEK_END (end of file) Example call fseek(myfileptr, sizeof(struct mydata)*(index-1),seek_set);

EXAMPLE Study and run the program fileio.c in the extras directory