Thread: cout a fixed length string?

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    6

    cout a fixed length string?

    Is there any clean way to print out a fixed-length string (no null terminator)? I realize I can copy the fixed length to a string and print out the null terminated one, but that seems execive. I was thinking if there was a way to cout a set amount of chars? Any help would be appreciated!

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    char szBuf[] = "Hello World!\n";
    
    	std::cout.write(szBuf, sizeof(szBuf) - 1);
    	std::cout.write(szBuf, strlen(szBuf));
    -1 for NULL terminator. strlen(..) just if it's NULL-terminated anyways. However you are implementing this.

  3. #3
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597

    Thumbs up

    Quote Originally Posted by Tonto
    Code:
    char szBuf[] = "Hello World!\n";
    
    	std::cout.write(szBuf, sizeof(szBuf) - 1);
    will only work for local arrays, not char* pointers, which is how most code uses string arrays.

    Code:
    void PrintStr(const char* str)
    {
        std::cout.write(str, sizeof(str) - 1);
    }
    
    char szBuf[] = "Hello World!\n";
    
    // outputs "Hello World!"
    std::cout.write(szBuf, sizeof(szBuf) - 1);
    
    // outputs "Hel" on a 32bit platform
    PrintStr(szBuf);
    fireonyx, unfortunately you're gonna have to pass the size with the string
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  4. #4
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    maybe this isn't what you're looking for, but what about...
    Code:
    int main()
    {
        char buf[] = "Hello World!\n";
    
        for (int i = 0; i < 14; i++)
        {
            cout << buf[i];
        }
    
        cin.get();
        return 0;
    }
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    6
    thanks all. I have a class that has several public char[] arrays. The method posted by Tonto works fine. Its been forever since I have done c++, and still getting the hang of it again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Find String length and Arraysearch
    By s.rajaram in forum C Programming
    Replies: 5
    Last Post: 10-03-2007, 02:28 AM
  4. Replies: 4
    Last Post: 03-03-2006, 02:11 AM