Thread: Understanding a Simple Strcat Example

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    127

    Question Understanding a Simple Strcat Example

    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:

    Code:
    strncat(path, filename, strlen(path) - 80); // concatenate
    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?

    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;
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by bengreenwood View Post
    What I don't understand is this line:

    Code:
    strncat(path, filename, strlen(path) - 80); // concatenate
    This only appends as many chars of filename that still fit into a buffer of 80.
    Kurt

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Why are you using messy C-style strings in a C++ program?

    I get that you need them for opening files (bizarrely), but still there's ways round that.
    Code:
        string filename;
        string path;
    
        cout << "Enter directory path: ";   // Prompt for path
        cin.getline(path);
    
        cout << "Enter a file name and press ENTER: ";
        cin.getline(filename);
    
        path += filename;  // nice and safe
    
        ofstream file_out(path.c_str());     // Use path string to open
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    127
    Zuk, thanks for your quick reply, but I don't understand what you mean..

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Shouldn't that be
    Code:
    strncat(path, filename, 80 - strlen(path)); // concatenate
    It ensures that no more than 80 characters end up in path so you don't go out of bounds with the buffer.

    The bad thing is that it may sometimes leave a part of filename uncopied and you'll be left with an incomplete filename.

    This is particularly problematic because filename is allowed to be 80 characters long, so cases where the full path would get truncated are foreseen. (Path should be longer: Windows allows full paths to be something like 266 characters).

    Considering how much trouble it is with char arrays and how easy it is with std::string, your choice should be obvious...
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Also, if it is like it states now, if strlen(path) is less than 80, it would be a negative number, which in unsigned [as size_t is] means "really huge" - so the code probably works OK until you get a long path or filename that would fill the entire string size... There's something wrong with this code...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 05:26 PM
  2. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 11:48 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM