Computer Science 210 Computer Organization

Slides:



Advertisements
Similar presentations
Lecture 20 Arrays and Strings
Advertisements

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.
C Programming Strings. Array of characters – most common type of array in C  Let’s make them easier for use Denote the end of array using a special character.
Computer Science 210 Computer Organization Strings in C.
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.
In Addition... To the string class from the standard library accessed by #include C++ also has another library of string functions for C strings that can.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
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.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
File IO and command line input CSE 2451 Rong Shi.
APS105 Strings. C String storage We have used strings in printf format strings –Ex: printf(“Hello world\n”); “Hello world\n” is a string (of characters)
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 4.
Chapter 3 Input and Output
Representing Strings and String I/O. Introduction A string is a sequence of characters and is treated as a single data item. A string constant, also termed.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
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).
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
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.
CSE 251 Dr. Charles B. Owen Programming in C1 Strings and File I/O.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
Strings CSCI 112: Programming in C.
Computer Science 210 Computer Organization
ECE Application Programming
C Characters and Strings
Fundamentals of Characters and Strings
ICS103 Programming in C Lecture 15: 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.
Lecture 8 String 1. Concept of strings String and pointers
C Programming Tutorial – Part I
Computer Science 210 Computer Organization
Chapter 18 I/O in C.
CSC215 Lecture Input and Output.
Plan for the Day: I/O (beyond scanf and printf)
Strings A string is a sequence of characters treated as a group
Arrays in C.
Chapter 8 - Characters and Strings
CS111 Computer Programming
Programming in C Input / Output.
Input and Output Lecture 4.
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Programming in C Input / Output.
C Stuff CS 2308.
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Computer Science 210 Computer Organization
Strings.
Dale Roberts, Lecturer IUPUI
Arrays Strings and Parameter Passing CSCI N305
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.
Strings.
ICS103 Programming in C Lecture 15: Strings
Programming in C Input / Output.
EECE.2160 ECE Application Programming
Exercise Arrays.
ICS103 Programming in C Lecture 15: Strings
Arrays.
Strings Adapted from Dr. Mary Eberlein, UT Austin.
Introduction to C EECS May 2019.
Strings #include <stdio.h>
Characters and Strings Functions
Strings Adapted from Dr. Mary Eberlein, UT Austin.
C Characters and Strings
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
Chapter 18 I/O in C.
Presentation transcript:

Computer Science 210 Computer Organization Strings and Text Files in C

Representing Strings printf("Hi Ken!\n"); 'H' 'i' ' ' 'K' 'e' 'n' '!' '\n' nul A string is a set of ASCII values that inhabit a sequence of bytes A string should always end in a nul character (ASCII 0) Many string functions use nul as a sentinel

The String “Type” and Variables char *greeting = "Hi Ken!\n"; printf("%s", greeting); greeting 'H' 'i' ' ' 'K' 'e' 'n' '!' '\n' nul The pointer to a char named greeting holds the address of the first byte The standard string processing functions view strings as of type char*

The String “Type” and Variables char *greeting = "Hi Ken!\n"; printf("%s", greeting); greeting 'H' 'i' ' ' 'K' 'e' 'n' '!' '\n' nul GREETING .STRINGz "Hi Ken!\n" LEA R0, GREETING PUTS

View a String as an Array of char char *greeting = "Hi Ken!\n"; int i = 0; for (; greeting[i] != 0; i++) putchar(greeting[i]); greeting 'H' 'i' ' ' 'K' 'e' 'n' '!' '\n' nul A string looks like an array in memory So, we can use an index to access or modify a character in any cell

The string Library and strlen #include <string.h> char *greeting = "Hi Ken!\n"; int i = 0; for (; i < strlen(greeting); i++) putchar(greeting[i]); greeting 'H' 'i' ' ' 'K' 'e' 'n' '!' '\n' nul The string library includes several common string functions (length, concatenation, comparison, etc.) strlen is O(n), because it searches for the nul character

The string Library Function What it does int strlen(char *str) Returns the number of characters in the string, not including the nul character. strcmp(char *str1, char *str2) Returns 0 if str1 equals str2, a negative integer if str1 < str2, or a positive integer if str1 > str2. strcat(char *str1, char *str2) Adds the characters in str2 to the end of str1, overwriting its nul character. strcpy (char *str1, char *str2) Copies the characters of str2 into the storage of str1. There must be enough storage! You can pass either a char* or a char array to these functions

The ctype Library #include <string.h> #include <ctype.h> char *greeting = "Hi Ken!\n"; int i = 0; for (; i < strlen(greeting); i++) greeting[i] = toupper(greeting[i]); greeting 'H' 'I' ' ' 'K' 'E' 'N' '!' '\n' nul The ctype library includes several common character functions (test for letter or digit, convert to uppercase, etc.)

String Input with scanf #include <stdio.h> int main(){ char name[20]; printf("Enter your first name: "); scanf("%s", name); printf("%s\n", name); } scanf stops at the first whitespace character and adds a nul character (works for one-word inputs) Must pass scanf the storage, usually an array of char

String Input with gets #include <stdio.h> int main(){ char name[20]; printf("Enter your first name: "); gets(name); printf("%s\n", name); } gets stops at the first newline character, which is not included, and adds a nul character (works for multi-word inputs) Must pass gets the storage, usually an array of char

Other Points about Strings Can be the element type of an array Can be the type of a field in a struct

Example: The Argument Vector The argument vector is an array of strings that are gathered up when a C program is run at the command prompt The first string is the name of the command The remaining strings are the arguments, if any The argument vector and its length are optional formal parameters of the main function

Print the Command Line Arguments /* Prints contents of the argument vector (the name of the program and any command-line arguments). */ #include <stdio.h> int main(int length, char *argVec[]){ int i; for (i = 0; i < length; i++) printf("Argument %d: %s\n", i, argVec[i]); }

Opening Files for I/O Syntax: fopen(fileName, modeString) #include <stdio.h> int main(){ FILE *infile = fopen("test.asm", "r"); FILE *outfile = fopen("test.bin", "w"); if (infile == NULL) printf("The input file does not exist.\n"); else{ processFile(infile, outfile); fclose(infile); fclose(outfile); } Syntax: fopen(fileName, modeString)

Text File I/O Functions What it does char* fgets(char* array, int arraySize, FILE *infile) Returns NULL if end of file. Otherwise, loads the next line, including the newline and nul characters, into array. Note: there must be storage for array! int fputs(char* array, FILE *outfile) Returns EOF if the string cannot be written to outfile. Otherwise, writes the string to outfile. A newline is not automatically written. fprintf(FILE *outfile, char* formatString, arg1, . . ., argn) Works just like printf with the standard output file (the terminal)

Example: Create a Numbered Listing void processFile(FILE *infile, FILE *outfile){ int max = 80; int fieldWidth = 4; char line[max]; int lineNumber = 1; while (fgets(line, max, infile)){ fprintf(outfile, "%*d%s", fieldWidth, lineNumber, "> "); fputs(line, outfile); lineNumber++; } "%*d" allows a variable to be used as a field width for a datum The variable for the field width precedes the datum being formatted

The Standard I/O Files stdin names the standard input file, which is the keyboard stdout names the standard output file, which is the terminal screen Both variables are of type FILE*

Example: Print to File or Terminal int main(int length, char *argVec[]){ if (length == 1) return; FILE *infile = fopen(argVec[1], "r"); if (infile == NULL){ printf("The input file does not exist in this directory\n"); } FILE *outfile; if (length == 3) outfile = fopen(argVec[2], "w"); // Using an output file else outfile = stdout; // Using the terminal processFile(infile, outfile); fclose(infile); if (outfile != stdout) fclose(outfile);

Pointers and Dynamic Storage For Monday Pointers and Dynamic Storage