String Functions and File Processing
String Functions and File Processing
TECHNIQUE
By
[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
4
Character Conversion Functions
Two functions that convert between letter cases:
Functions:
toupper: if char argument is lowercase letter, return uppercase
equivalent; otherwise, return input unchanged
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
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)