Download presentation
Presentation is loading. Please wait.
1
Computer Science 210 Computer Organization
Strings and Text Files in C
2
Representing Strings printf("Hi Ken!\n"); 'H' 'i' ' ' 'K' 'e' 'n' '!' '\n' null A string is a set of ASCII values that inhabit a sequence of bytes A string should always end in a null character (ASCII 0) Many string functions use null as a sentinel
3
The String “Type” and Variables
char *greeting = "Hi Ken!\n"; printf("%s", greeting); greeting 'H' 'i' ' ' 'K' 'e' 'n' '!' '\n' null 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*
4
The String “Type” and Variables
char *greeting = "Hi Ken!\n"; printf("%s", greeting); greeting 'H' 'i' ' ' 'K' 'e' 'n' '!' '\n' null GREETING .STRINGz "Hi Ken!\n" LEA R0, GREETING PUTS
5
View a String as an Array of char
char *greeting = "Hi Ken!\n"; for (int i = 0;; greeting[i] != 0; i++) putchar(greeting[i]); greeting 'H' 'i' ' ' 'K' 'e' 'n' '!' '\n' null A string looks like an array in memory So, we can use an index to access or modify a character in any cell
6
The string Library and strlen
#include <string.h> char *greeting = "Hi Ken!\n"; for (int i = 0; i < strlen(greeting); i++) putchar(greeting[i]); greeting 'H' 'i' ' ' 'K' 'e' 'n' '!' '\n' null The string library includes several common string functions (length, concatenation, comparison, etc.) strlen is O(n), because it searches for the null character
7
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
8
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' null The ctype library includes several common character functions (test for letter or digit, convert to uppercase, etc.)
9
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
10
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
11
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
12
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 argc, char *argv[]){ int i; for (i = 0; i < argc; i++) printf("Argument %d: %s\n", i, argv]); }
13
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)
14
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)
15
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
16
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*
17
Example: Print to File or Terminal
int main(int argc, char *argv[]){ if (argc == 1) // no argument provided (?) return; FILE *infile = fopen(argv[1], "r"); if (infile == NULL){ printf("The input file does not exist in this directory\n"); } FILE *outfile; if (argc == 3) outfile = fopen(argv[1], "w"); // Using an output file else outfile = stdout; // Using the terminal processFile(infile, outfile); fclose(infile); if (outfile != stdout) fclose(outfile);
18
Pointers and Dynamic Storage
For Monday Pointers and Dynamic Storage
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.