Hi,
I'm trying to understand the following example program from C++ Without Fear. Basically it gets to strings from the user, one for the path of a text file, one for the name of a text file, then it creates that text file.
What I don't understand is this line:
Why would you only want to copy the following number of characters: strlen(path) - 80? Path is only like, 81 characters long! Isn't using that going to make it copy only 1 character?Code:strncat(path, filename, strlen(path) - 80); // concatenate
Thanks.
Code:#include <iostream> #include <fstream> #include <string.h> // Add this include using namespace std; int main() { char filename[81]; char path[81]; cout << "Enter directory path: "; // Prompt for path cin.getline(path, 80); cout << "Enter a file name and press ENTER: "; cin.getline(filename, 80); strncat(path, filename, strlen(path) - 80); // concatenate ofstream file_out(path); // Use path string to open if (! file_out) { cout << "File " << filename << " could not be opened."; return -1; } cout << "File " << filename << " was opened."; file_out << "I am Blaxxon," << endl; file_out << "the cosmic computer." << endl; file_out << "Fear me."; file_out.close(); return 0; }



LinkBack URL
About LinkBacks



