0% found this document useful (0 votes)
73 views19 pages

String Functions and File Processing

This document discusses string functions and file processing in C++. It covers: - Using functions like strcat(), strcpy(), strlen() to manipulate C-style character strings - Character classification and conversion functions like isalpha(), tolower(), toupper() - Opening, reading, writing, and closing files using ifstream, ofstream and fstream - Checking for errors when opening files and examples of writing data to files

Uploaded by

domm proz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views19 pages

String Functions and File Processing

This document discusses string functions and file processing in C++. It covers: - Using functions like strcat(), strcpy(), strlen() to manipulate C-style character strings - Character classification and conversion functions like isalpha(), tolower(), toupper() - Opening, reading, writing, and closing files using ifstream, ofstream and fstream - Checking for errors when opening files and examples of writing data to files

Uploaded by

domm proz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 19

INTRODUCTION TO PROGRAMMING

TECHNIQUE

STRING FUNCTIONS AND FILE PROCESSING

By

DATIN NURAZLINA MD SANUSI

[email protected]
1
Learning Outcomes
• At the end of this class, you should be able to
– use functions in cctype, cstring and
string classes to manipulate character and
string
– explain the use of files in storing data
– describe the important components needed in
writing C++ code for file processing
C-String Style Character String
• C-string: sequence of characters stored in
adjacent memory locations and terminated by
NULL character
• The C-string
"Hi there!"
would be stored in memory as shown:
• Following is the memory presentation of above
defined string in C++:

H i t h e r e ! \0

3
Character Testing
• require cctype header file

Character Classification Functions


FUNCTION MEANING
isalpha true if arg. is a letter, false otherwise
isalnum true if arg. is a letter or digit, false otherwise
isdigit true if arg. is a digit 0-9, false otherwise
islower true if arg. is lowercase letter, false otherwise
isprint true if arg. is a printable character, false otherwise
ispunct true if arg. is a punctuation character, false otherwise
isupper true if arg. is an uppercase letter, false otherwise
isspace true if arg. is a whitespace character, false otherwise

4
Character Conversion Functions
Two functions that convert between letter cases:

tolower Convert uppercase letter to


lowercase
toupper Convert lowercase letter to
uppercase

Functions:
toupper: if char argument is lowercase letter, return uppercase
equivalent; otherwise, return input unchanged

char ch1 = 'H';


char ch2 = 'e';
char ch3 = '!';
cout << toupper(ch1); // displays 'H'
cout << toupper(ch2); // displays 'E'
cout << toupper(ch3); // displays '!'
Character Conversion Functions
1 /* tolower example */
2 #include <stdio.h>
3 #include <ctype.h>
4 int main ()
5 {
6 int i=0;
7 char str[]="Test String.\n";
8 char c;
9 while (str[i])
10 {
11 c=str[i];
12 putchar (tolower(c));
13 i++;
14 }
15 return 0;
16 }

Output:
test string.
Library Functions for Working with C-
Strings
• Require the cstring header file
Functions:
– strcat(slocation, sstate): appends
str2 to the end of str1
char location[SIZE] = "Missoula, ";
char state[3] = "MT";
strcat(location, state);
// location now has "Missoula, MT"

7
Other Definitions of C++ strings

Definition Meaning
string name; defines an empty string object
string myname("Chris"); defines a string and initializes it
string yourname(myname); defines a string and initializes it
string aname(myname, 3); defines a string and initializes it with first 3
characters of myname

string verb(myname,3,2); defines a string and initializes it with 2


characters from myname starting at position
3

string noname('A', 5); defines string and initializes it to 5 'A's

10
Using Files for Data Storage
• Can use files instead of keyboard, monitor
screen for program input, output
• Allows data to be retained between program
runs
• Steps:
– Open the file
– Use the file (read from, write to, or both)
– Close the file

11
Files: What is Needed
• Use fstream header file for file access
• File stream types:
ifstream for input from a file
ofstream for output to a file
fstream for input from or output to a file
• Define file stream objects:
ifstream infile;
ofstream outfile;

12
Opening Files
• Create a link between file name (outside the program) and
file stream object (inside the program)
• Use the open member function:
infile.open("inventory.dat");
outfile.open("report.txt");
• Filename may include drive, path info.
• Output file will be created if necessary; existing file will be
erased first
• Input file must exist for open to work

13
Testing for File Open Errors
• Can test a file stream object to detect if an open
operation failed:
infile.open("test.txt");
if (!infile)
{
cout << "File open failure!";
}
• Can also use the fail member function

14
Closing Files
• Use the close member function:
infile.close();
outfile.close();
• Don’t wait for operating system to close files
at program end:
– may be limit on number of open files
– may be buffered output data waiting to send to
file

15
Examples of Questions
• What is the output of the following code?
char str1[25] = "Try your best!";
char str2[25] = "Good Luck...";
int length;
cout << str1 << str2 << endl;
strncpy(str2, str1,13);
cout << str1 << str2 << endl;
strupr(str2);
cout << str2 << endl;
length = strlen(str2);
cout << "The length of this sentence is ";
cout << length << "\n";
Answers
Examples of Questions
int number1 = 3, number2 = 8, number3 = 9;
fstream outFile("addTable.txt", ios::out);
number1 = number1 + number2;
outFile << number1;
number2 = number2 + number3;
outFile << setw(5) << number2;
number3 = number2 + number1;
outFile << "\n" << number3;
outFile.close();
cout << "Number is saved in file addTable.txt\n";
i) What will be displayed when the code is executed?
ii) What is the content of addTable file?
Answers
• i) Number is saved in addTable.txt
• ii)

You might also like