C Programming:Part 3 Characters and Strings File Processing Exercise.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
Character String Manipulation. Overview Character string functions sscanf() function snprintf() function.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Data files –Can be created, updated,
Chapter 8 Characters and Strings Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
CS1061: C Programming Lecture 19: Random Access Files A. O’Riordan, 2004, 2007 updated.
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
Strings in C. Strings are Character Arrays Strings in C are simply arrays of characters. – Example:char s [10]; This is a ten (10) element array that.
Lone Leth Thomsen Input / Output and Files. April 2006Basis-C-8/LL2 sprintf() and sscanf() The functions sprintf() and sscanf() are string versions of.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
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.
Introduction to the C Programming Language Michael Griffiths
22. FILE INPUT/OUTPUT. File Pointers and Streams Declarations of functions that perform file I/O appear in. Each function requires a file pointer as a.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
1 Lecture09: File I/O 5/6/2013 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Characters and Strings File Processing Exercise C Programming:Part 3.
File IO and command line input CSE 2451 Rong Shi.
Chapter 8 Characters and Strings Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
1 Lecture09: File I/O 11/19/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Chapter 11: Data Files and File Processing Files and streams Creating a sequential access file Reading data from a sequential access file Using fgetc()
Chapter 11 File Processing. Objectives In this chapter, you will learn: –To be able to create, read, write and update files. –To become familiar with.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Introduce some standard library functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 11 – File Processing Outline 11.1Introduction.
chap8 Chapter 12 Files (reference: Deitel ’ s chap 11)
C File Processing. Objectives To be able to create, write and update files. To become familiar with sequential access file processing. To become familiar.
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
 2007 Pearson Education, Inc. All rights reserved. 1 C File Processing.
FILES IN C. File Operations  Creation of a new file  Opening an existing file  Reading from a file  Writing to a file  Moving to a specific location.
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.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
6/9/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 10 (C-4)
11 C File Processing.
C Characters and Strings
INC 161 , CPE 100 Computer Programming
C Characters and Strings
Chapter 4 File Processing
TMF1414 Introduction to Programming
Functions, Part 2 of 2 Topics Functions That Return a Value
Chapter 18 I/O in C.
Introduction to Computer Programming Lecture 18 Binary Files
No Objects, No Type Safety
Programming Languages -1 (Introduction to C) strings
A First Book of ANSI C Fourth Edition
CSE 303 Lecture 14 Strings in C
CSC215 Lecture Input and Output.
Plan for the Day: I/O (beyond scanf and printf)
Chapter 8 - Characters and Strings
CS111 Computer Programming
Introduction to Programming
Chapter 11 – File Processing
C Stuff CS 2308.
CSC215 Lecture Input and Output.
CSC215 Lecture Input and Output.
Text and Binary File Processing
File Input and Output.
Chapter: 7-12 Final exam review.
Fundamental of Programming (C)
C Input / Output Prabhat Kumar Padhy
EPSII 59:006 Spring 2004.
Module 12 Input and Output
C Characters and Strings
Professor Jodi Neely-Ritz University of Florida
Presentation transcript:

C Programming:Part 3 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

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 1st order ODEs with well defined BC’s Shooting Method Compile and run the code startshooting.c 20

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 { int index; float data;} 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